コード例 #1
-5
ファイル: pogs_test.go プロジェクト: zombiezen/go-capnproto2
func zequal(g *Z, c air.Z) (bool, error) {
	if g.Which != c.Which() {
		return false, nil
	}
	listeq := func(has bool, n int, l capnp.List, f func(i int) (bool, error)) (bool, error) {
		if has != l.IsValid() {
			return false, nil
		}
		if !has {
			return true, nil
		}
		if l.Len() != n {
			return false, nil
		}
		for i := 0; i < l.Len(); i++ {
			if ok, err := f(i); !ok || err != nil {
				return ok, err
			}
		}
		return true, nil
	}
	switch g.Which {
	case air.Z_Which_f64:
		return g.F64 == c.F64(), nil
	case air.Z_Which_f32:
		return g.F32 == c.F32(), nil
	case air.Z_Which_i64:
		return g.I64 == c.I64(), nil
	case air.Z_Which_i32:
		return g.I32 == c.I32(), nil
	case air.Z_Which_i16:
		return g.I16 == c.I16(), nil
	case air.Z_Which_i8:
		return g.I8 == c.I8(), nil
	case air.Z_Which_u64:
		return g.U64 == c.U64(), nil
	case air.Z_Which_u32:
		return g.U32 == c.U32(), nil
	case air.Z_Which_u16:
		return g.U16 == c.U16(), nil
	case air.Z_Which_u8:
		return g.U8 == c.U8(), nil
	case air.Z_Which_bool:
		return g.Bool == c.Bool(), nil
	case air.Z_Which_text:
		text, err := c.Text()
		if err != nil {
			return false, err
		}
		return g.Text == text, nil
	case air.Z_Which_blob:
		blob, err := c.Blob()
		if err != nil {
			return false, err
		}
		if (blob == nil) != (g.Blob == nil) {
			return false, nil
		}
		return bytes.Equal(g.Blob, blob), nil
	case air.Z_Which_f64vec:
		fv, err := c.F64vec()
		if err != nil {
			return false, err
		}
		return listeq(g.F64vec != nil, len(g.F64vec), fv.List, func(i int) (bool, error) {
			return fv.At(i) == g.F64vec[i], nil
		})
	case air.Z_Which_f32vec:
		fv, err := c.F32vec()
		if err != nil {
			return false, err
		}
		return listeq(g.F32vec != nil, len(g.F32vec), fv.List, func(i int) (bool, error) {
			return fv.At(i) == g.F32vec[i], nil
		})
	case air.Z_Which_i64vec:
		iv, err := c.I64vec()
		if err != nil {
			return false, err
		}
		return listeq(g.I64vec != nil, len(g.I64vec), iv.List, func(i int) (bool, error) {
			return iv.At(i) == g.I64vec[i], nil
		})
	case air.Z_Which_i8vec:
		iv, err := c.I8vec()
		if err != nil {
			return false, err
		}
		return listeq(g.I8vec != nil, len(g.I8vec), iv.List, func(i int) (bool, error) {
			return iv.At(i) == g.I8vec[i], nil
		})
	case air.Z_Which_u64vec:
		uv, err := c.U64vec()
		if err != nil {
			return false, err
		}
		return listeq(g.U64vec != nil, len(g.U64vec), uv.List, func(i int) (bool, error) {
			return uv.At(i) == g.U64vec[i], nil
		})
	case air.Z_Which_u8vec:
		uv, err := c.U8vec()
		if err != nil {
			return false, err
		}
		return listeq(g.U8vec != nil, len(g.U8vec), uv.List, func(i int) (bool, error) {
			return uv.At(i) == g.U8vec[i], nil
		})
	case air.Z_Which_boolvec:
		bv, err := c.Boolvec()
		if err != nil {
			return false, err
		}
		return listeq(g.Boolvec != nil, len(g.Boolvec), bv.List, func(i int) (bool, error) {
			return bv.At(i) == g.Boolvec[i], nil
		})
	case air.Z_Which_datavec:
		dv, err := c.Datavec()
		if err != nil {
			return false, err
		}
		return listeq(g.Datavec != nil, len(g.Datavec), dv.List, func(i int) (bool, error) {
			di, err := dv.At(i)
			return bytes.Equal(di, g.Datavec[i]), err
		})
	case air.Z_Which_textvec:
		tv, err := c.Textvec()
		if err != nil {
			return false, err
		}
		return listeq(g.Textvec != nil, len(g.Textvec), tv.List, func(i int) (bool, error) {
			s, err := tv.At(i)
			return s == g.Textvec[i], err
		})
	case air.Z_Which_zvec:
		vec, err := c.Zvec()
		if err != nil {
			return false, err
		}
		return listeq(g.Zvec != nil, len(g.Zvec), vec.List, func(i int) (bool, error) {
			return zequal(g.Zvec[i], vec.At(i))
		})
	case air.Z_Which_zvecvec:
		vv, err := c.Zvecvec()
		if err != nil {
			return false, err
		}
		return listeq(g.Zvecvec != nil, len(g.Zvecvec), vv.List, func(i int) (bool, error) {
			p, err := vv.PtrAt(i)
			if err != nil {
				return false, err
			}
			v := air.Z_List{List: p.List()}
			return listeq(g.Zvecvec[i] != nil, len(g.Zvecvec[i]), v.List, func(j int) (bool, error) {
				return zequal(g.Zvecvec[i][j], v.At(j))
			})
		})
	case air.Z_Which_planebase:
		pb, err := c.Planebase()
		if err != nil {
			return false, err
		}
		if (g.Planebase != nil) != pb.IsValid() {
			return false, nil
		}
		if g.Planebase == nil {
			return true, nil
		}
		name, err := pb.Name()
		if err != nil {
			return false, err
		}
		if g.Planebase.Name != name {
			return false, nil
		}
		homes, err := pb.Homes()
		if err != nil {
			return false, err
		}
		homeseq, _ := listeq(g.Planebase.Homes != nil, len(g.Planebase.Homes), homes.List, func(i int) (bool, error) {
			return g.Planebase.Homes[i] == homes.At(i), nil
		})
		if !homeseq {
			return false, nil
		}
		return g.Planebase.Rating == pb.Rating() && g.Planebase.CanFly == pb.CanFly() && g.Planebase.Capacity == pb.Capacity() && g.Planebase.MaxSpeed == pb.MaxSpeed(), nil
	case air.Z_Which_airport:
		return g.Airport == c.Airport(), nil
	case air.Z_Which_grp:
		if g.Grp == nil {
			return false, nil
		}
		return g.Grp.First == c.Grp().First() && g.Grp.Second == c.Grp().Second(), nil
	default:
		return false, fmt.Errorf("zequal: unknown type: %v", g.Which)
	}
}