func zfill(c air.Z, g *Z) error { switch g.Which { case air.Z_Which_f64: c.SetF64(g.F64) case air.Z_Which_f32: c.SetF32(g.F32) case air.Z_Which_i64: c.SetI64(g.I64) case air.Z_Which_i32: c.SetI32(g.I32) case air.Z_Which_i16: c.SetI16(g.I16) case air.Z_Which_i8: c.SetI8(g.I8) case air.Z_Which_u64: c.SetU64(g.U64) case air.Z_Which_u32: c.SetU32(g.U32) case air.Z_Which_u16: c.SetU16(g.U16) case air.Z_Which_u8: c.SetU8(g.U8) case air.Z_Which_bool: c.SetBool(g.Bool) case air.Z_Which_text: return c.SetText(g.Text) case air.Z_Which_blob: return c.SetBlob(g.Blob) case air.Z_Which_f64vec: if g.F64vec == nil { return c.SetF64vec(capnp.Float64List{}) } fv, err := c.NewF64vec(int32(len(g.F64vec))) if err != nil { return err } for i, f := range g.F64vec { fv.Set(i, f) } case air.Z_Which_f32vec: if g.F32vec == nil { return c.SetF32vec(capnp.Float32List{}) } fv, err := c.NewF32vec(int32(len(g.F32vec))) if err != nil { return err } for i, f := range g.F32vec { fv.Set(i, f) } case air.Z_Which_i64vec: if g.I64vec == nil { return c.SetI64vec(capnp.Int64List{}) } iv, err := c.NewI64vec(int32(len(g.I64vec))) if err != nil { return err } for i, n := range g.I64vec { iv.Set(i, n) } case air.Z_Which_i8vec: if g.I8vec == nil { return c.SetI8vec(capnp.Int8List{}) } iv, err := c.NewI8vec(int32(len(g.I8vec))) if err != nil { return err } for i, n := range g.I8vec { iv.Set(i, n) } case air.Z_Which_u64vec: if g.U64vec == nil { return c.SetU64vec(capnp.UInt64List{}) } uv, err := c.NewU64vec(int32(len(g.U64vec))) if err != nil { return err } for i, n := range g.U64vec { uv.Set(i, n) } case air.Z_Which_u8vec: if g.U8vec == nil { return c.SetU8vec(capnp.UInt8List{}) } uv, err := c.NewU8vec(int32(len(g.U8vec))) if err != nil { return err } for i, n := range g.U8vec { uv.Set(i, n) } case air.Z_Which_boolvec: if g.Boolvec == nil { return c.SetBoolvec(capnp.BitList{}) } vec, err := c.NewBoolvec(int32(len(g.Boolvec))) if err != nil { return err } for i, v := range g.Boolvec { vec.Set(i, v) } case air.Z_Which_datavec: if g.Datavec == nil { return c.SetDatavec(capnp.DataList{}) } vec, err := c.NewDatavec(int32(len(g.Datavec))) if err != nil { return err } for i, v := range g.Datavec { if err := vec.Set(i, v); err != nil { return err } } case air.Z_Which_textvec: if g.Textvec == nil { return c.SetTextvec(capnp.TextList{}) } vec, err := c.NewTextvec(int32(len(g.Textvec))) if err != nil { return err } for i, v := range g.Textvec { if err := vec.Set(i, v); err != nil { return err } } case air.Z_Which_zvec: if g.Zvec == nil { return c.SetZvec(air.Z_List{}) } vec, err := c.NewZvec(int32(len(g.Zvec))) if err != nil { return err } for i, z := range g.Zvec { if err := zfill(vec.At(i), z); err != nil { return err } } case air.Z_Which_zvecvec: if g.Zvecvec == nil { return c.SetZvecvec(capnp.PointerList{}) } vv, err := c.NewZvecvec(int32(len(g.Zvecvec))) if err != nil { return err } for i, zz := range g.Zvecvec { v, err := air.NewZ_List(vv.Segment(), int32(len(zz))) if err != nil { return err } if err := vv.SetPtr(i, v.ToPtr()); err != nil { return err } for j, z := range zz { if err := zfill(v.At(j), z); err != nil { return err } } } case air.Z_Which_planebase: if g.Planebase == nil { return c.SetPlanebase(air.PlaneBase{}) } pb, err := c.NewPlanebase() if err != nil { return err } if err := pb.SetName(g.Planebase.Name); err != nil { return err } if g.Planebase.Homes != nil { homes, err := pb.NewHomes(int32(len(g.Planebase.Homes))) if err != nil { return err } for i := range g.Planebase.Homes { homes.Set(i, g.Planebase.Homes[i]) } } pb.SetRating(g.Planebase.Rating) pb.SetCanFly(g.Planebase.CanFly) pb.SetCapacity(g.Planebase.Capacity) pb.SetMaxSpeed(g.Planebase.MaxSpeed) case air.Z_Which_airport: c.SetAirport(g.Airport) case air.Z_Which_grp: c.SetGrp() if g.Grp != nil { c.Grp().SetFirst(g.Grp.First) c.Grp().SetSecond(g.Grp.Second) } default: return fmt.Errorf("zfill: unknown type: %v", g.Which) } return nil }
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) } }