示例#1
0
func TestInsert_StructListNoPtr(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	bz := &StructZ{Which: air.Z_Which_zvec, Zvec: []Z{
		{Which: air.Z_Which_i64, I64: 123},
	}}
	err = Insert(air.Z_TypeID, z.Struct, bz)
	if err != nil {
		t.Errorf("Insert(%s) error: %v", zpretty.Sprint(bz), err)
	}
	want := &Z{Which: air.Z_Which_zvec, Zvec: []*Z{
		{Which: air.Z_Which_i64, I64: 123},
	}}
	if equal, err := zequal(want, z); err != nil {
		t.Errorf("Insert(%s) compare err: %v", zpretty.Sprint(bz), err)
	} else if !equal {
		t.Errorf("Insert(%s) produced %v", zpretty.Sprint(bz), z)
	}
}
示例#2
0
func TestExtract(t *testing.T) {
	for _, test := range goodTests {
		_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Errorf("NewMessage for %s: %v", zpretty.Sprint(test), err)
			continue
		}
		z, err := air.NewRootZ(seg)
		if err != nil {
			t.Errorf("NewRootZ for %s: %v", zpretty.Sprint(test), err)
			continue
		}
		if err := zfill(z, &test); err != nil {
			t.Errorf("zfill for %s: %v", zpretty.Sprint(test), err)
			continue
		}
		out := new(Z)
		if err := Extract(out, air.Z_TypeID, z.Struct); err != nil {
			t.Errorf("Extract(%v) error: %v", z, err)
		}
		if !reflect.DeepEqual(out, &test) {
			t.Errorf("Extract(%v) produced %s; want %s", z, zpretty.Sprint(out), zpretty.Sprint(test))
		}
	}
}
示例#3
0
func TestExtract_StructListNoPtr(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	err = zfill(z, &Z{Which: air.Z_Which_zvec, Zvec: []*Z{
		{Which: air.Z_Which_i64, I64: 123},
	}})
	if err != nil {
		t.Fatalf("zfill: %v", err)
	}
	out := new(StructZ)
	if err := Extract(out, air.Z_TypeID, z.Struct); err != nil {
		t.Errorf("Extract(%v) error: %v", z, err)
	}
	want := &StructZ{Which: air.Z_Which_zvec, Zvec: []Z{
		{Which: air.Z_Which_i64, I64: 123},
	}}
	if !reflect.DeepEqual(out, want) {
		t.Errorf("Extract(%v) produced %s; want %s", z, zpretty.Sprint(out), zpretty.Sprint(want))
	}
}
func zdateFilledMessage(t testing.TB, n int32) *capnp.Message {
	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatal(err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatal(err)
	}
	list, err := air.NewZdate_List(seg, n)
	if err != nil {
		t.Fatal(err)
	}

	for i := 0; i < int(n); i++ {
		d, err := air.NewZdate(seg)
		if err != nil {
			t.Fatal(err)
		}
		d.SetMonth(12)
		d.SetDay(7)
		d.SetYear(int16(2004 + i))
		list.Set(i, d)
	}
	z.SetZdatevec(list)

	return msg
}
func zdataFilledSegment(n int) (*capnp.Segment, []byte) {
	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		panic(err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		panic(err)
	}
	d, err := air.NewZdata(seg)
	if err != nil {
		panic(err)
	}

	b := make([]byte, n)
	for i := 0; i < len(b); i++ {
		b[i] = byte(i)
	}
	d.SetData(b)
	z.SetZdata(d)

	buf, err := msg.Marshal()
	if err != nil {
		panic(err)
	}
	return seg, buf
}
示例#6
0
func TestExtract_Tags(t *testing.T) {
	tests := []struct {
		name string
		z    Z
		tagz TagZ
	}{
		{
			name: "renamed field",
			z:    Z{Which: air.Z_Which_f64, F64: 3.5},
			tagz: TagZ{Which: air.Z_Which_f64, Float64: 3.5},
		},
		{
			name: "omitted field",
			z:    Z{Which: air.Z_Which_i64, I64: 42},
			tagz: TagZ{Which: air.Z_Which_i64},
		},
		{
			name: "field with overlapping name",
			z:    Z{Which: air.Z_Which_bool, Bool: true},
			tagz: TagZ{Which: air.Z_Which_bool, U8: true},
		},
	}
	for _, test := range tests {
		_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Errorf("%s: NewMessage: %v", test.name, err)
			continue
		}
		z, err := air.NewRootZ(seg)
		if err != nil {
			t.Errorf("%s: NewRootZ: %v", test.name, err)
			continue
		}
		if err := zfill(z, &test.z); err != nil {
			t.Errorf("%s: zfill: %v", test.name, err)
			continue
		}
		out := new(TagZ)
		if err := Extract(out, air.Z_TypeID, z.Struct); err != nil {
			t.Errorf("%s: Extract error: %v", test.name, err)
		}
		if !reflect.DeepEqual(out, &test.tagz) {
			t.Errorf("%s: Extract produced %s; want %s", test.name, zpretty.Sprint(out), zpretty.Sprint(test.tagz))
		}
	}
}
示例#7
0
func TestExtract_WhichTagMismatch(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	if err := zfill(z, &Z{Which: air.Z_Which_i64, I64: 42}); err != nil {
		t.Fatalf("zfill: %v", err)
	}
	out := new(ZBool)
	if err := Extract(out, air.Z_TypeID, z.Struct); err == nil {
		t.Error("Extract did not return an error")
	}
}
示例#8
0
func TestInsert_Tags(t *testing.T) {
	tests := []struct {
		name string
		tagz TagZ
		z    Z
	}{
		{
			name: "renamed field",
			tagz: TagZ{Which: air.Z_Which_f64, Float64: 3.5},
			z:    Z{Which: air.Z_Which_f64, F64: 3.5},
		},
		{
			name: "omitted field",
			tagz: TagZ{Which: air.Z_Which_i64, I64: 42},
			z:    Z{Which: air.Z_Which_i64, I64: 0},
		},
		{
			name: "field with overlapping name",
			tagz: TagZ{Which: air.Z_Which_bool, U8: true},
			z:    Z{Which: air.Z_Which_bool, Bool: true},
		},
	}
	for _, test := range tests {
		_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Errorf("%s: NewMessage: %v", test.name, err)
			continue
		}
		z, err := air.NewRootZ(seg)
		if err != nil {
			t.Errorf("%s: NewRootZ: %v", test.name, err)
			continue
		}
		err = Insert(air.Z_TypeID, z.Struct, &test.tagz)
		if err != nil {
			t.Errorf("%s: Insert(%s) error: %v", test.name, zpretty.Sprint(test.tagz), err)
		}
		if equal, err := zequal(&test.z, z); err != nil {
			t.Errorf("%s: Insert(%s) compare err: %v", test.name, zpretty.Sprint(test.tagz), err)
		} else if !equal {
			t.Errorf("%s: Insert(%s) produced %v", test.name, zpretty.Sprint(test.tagz), z)
		}
	}
}
示例#9
0
func TestExtract_WhichTag(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	if err := zfill(z, &Z{Which: air.Z_Which_bool, Bool: true}); err != nil {
		t.Fatalf("zfill: %v", err)
	}
	out := new(ZBool)
	if err := Extract(out, air.Z_TypeID, z.Struct); err != nil {
		t.Errorf("Extract error: %v", err)
	}
	if !out.Bool {
		t.Errorf("Extract produced %s; want %s", zpretty.Sprint(out), zpretty.Sprint(&ZBool{Bool: true}))
	}
}
示例#10
0
func TestInsert_StructNoPtr(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	bz := &StructZ{Which: air.Z_Which_planebase, Planebase: PlaneBase{Name: "foo"}}
	err = Insert(air.Z_TypeID, z.Struct, bz)
	if err != nil {
		t.Errorf("Insert(%s) error: %v", zpretty.Sprint(bz), err)
	}
	want := &Z{Which: air.Z_Which_planebase, Planebase: &PlaneBase{Name: "foo"}}
	if equal, err := zequal(want, z); err != nil {
		t.Errorf("Insert(%s) compare err: %v", zpretty.Sprint(bz), err)
	} else if !equal {
		t.Errorf("Insert(%s) produced %v", zpretty.Sprint(bz), z)
	}
}
示例#11
0
func TestInsert_StringListBytes(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	bz := &BytesZ{Which: air.Z_Which_textvec, Textvec: [][]byte{[]byte("Holmes"), []byte("Watson")}}
	err = Insert(air.Z_TypeID, z.Struct, bz)
	if err != nil {
		t.Errorf("Insert(%s) error: %v", zpretty.Sprint(bz), err)
	}
	want := &Z{Which: air.Z_Which_textvec, Textvec: []string{"Holmes", "Watson"}}
	if equal, err := zequal(want, z); err != nil {
		t.Errorf("Insert(%s) compare err: %v", zpretty.Sprint(bz), err)
	} else if !equal {
		t.Errorf("Insert(%s) produced %v", zpretty.Sprint(bz), z)
	}
}
示例#12
0
func TestInsert_StringBytes(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	bz := &BytesZ{Which: air.Z_Which_text, Text: []byte("Hello, World!")}
	err = Insert(air.Z_TypeID, z.Struct, bz)
	if err != nil {
		t.Errorf("Insert(%s) error: %v", zpretty.Sprint(bz), err)
	}
	want := &Z{Which: air.Z_Which_text, Text: "Hello, World!"}
	if equal, err := zequal(want, z); err != nil {
		t.Errorf("Insert(%s) compare err: %v", zpretty.Sprint(bz), err)
	} else if !equal {
		t.Errorf("Insert(%s) produced %v", zpretty.Sprint(bz), z)
	}
}
func zdataFilledMessage(t testing.TB, n int) *capnp.Message {
	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatal(err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatal(err)
	}
	d, err := air.NewZdata(seg)
	if err != nil {
		t.Fatal(err)
	}
	b := make([]byte, n)
	for i := 0; i < len(b); i++ {
		b[i] = byte(i)
	}
	d.SetData(b)
	z.SetZdata(d)
	return msg
}
示例#14
0
func TestInsert_GroupNoPtr(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	bz := &StructZ{Which: air.Z_Which_grp, Grp: ZGroup{First: 123, Second: 456}}
	err = Insert(air.Z_TypeID, z.Struct, bz)
	if err != nil {
		t.Errorf("Insert(%s) error: %v", zpretty.Sprint(bz), err)
	}
	want := &Z{Which: air.Z_Which_grp, Grp: &ZGroup{First: 123, Second: 456}}
	if equal, err := zequal(want, z); err != nil {
		t.Errorf("Insert(%s) compare err: %v", zpretty.Sprint(bz), err)
	} else if !equal {
		t.Errorf("Insert(%s) produced %v", zpretty.Sprint(bz), z)
	}
}
示例#15
0
func (blt bitListTest) makeMessage() (*capnp.Message, error) {
	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		return nil, err
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		return nil, err
	}
	list, err := capnp.NewBitList(seg, int32(len(blt.list)))
	if err != nil {
		return nil, err
	}
	for i := range blt.list {
		list.Set(i, blt.list[i])
	}
	if err := z.SetBoolvec(list); err != nil {
		return nil, err
	}
	return msg, nil
}
示例#16
0
func TestInsert_WhichTag(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	zb := &ZBool{Bool: true}
	err = Insert(air.Z_TypeID, z.Struct, zb)
	if err != nil {
		t.Errorf("Insert(%s) error: %v", zpretty.Sprint(zb), err)
	}
	want := &Z{Which: air.Z_Which_bool, Bool: true}
	if equal, err := zequal(want, z); err != nil {
		t.Errorf("Insert(%s) compare err: %v", zpretty.Sprint(zb), err)
	} else if !equal {
		t.Errorf("Insert(%s) produced %v", zpretty.Sprint(zb), z)
	}
}
示例#17
0
func TestExtract_GroupNoPtr(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	err = zfill(z, &Z{Which: air.Z_Which_grp, Grp: &ZGroup{First: 123, Second: 456}})
	if err != nil {
		t.Fatalf("zfill: %v", err)
	}
	out := new(StructZ)
	if err := Extract(out, air.Z_TypeID, z.Struct); err != nil {
		t.Errorf("Extract(%v) error: %v", z, err)
	}
	want := &StructZ{Which: air.Z_Which_grp, Grp: ZGroup{First: 123, Second: 456}}
	if !reflect.DeepEqual(out, want) {
		t.Errorf("Extract(%v) produced %s; want %s", z, zpretty.Sprint(out), zpretty.Sprint(want))
	}
}
示例#18
0
func TestExtraFields(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	zb := &ZBoolWithExtra{Bool: true, ExtraField: 42}
	err = Insert(air.Z_TypeID, z.Struct, zb)
	if err == nil {
		t.Errorf("Insert(%s) did not return error", zpretty.Sprint(zb))
	}
	err = Extract(zb, air.Z_TypeID, z.Struct)
	if err == nil {
		t.Errorf("Extract(%v) did not return error", z)
	}
	if zb.ExtraField != 42 {
		t.Errorf("zb.ExtraField modified to %d; want 42", zb.ExtraField)
	}
}
示例#19
0
func TestExtract_StringListBytes(t *testing.T) {
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		t.Fatalf("NewMessage: %v", err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		t.Fatalf("NewRootZ: %v", err)
	}
	err = zfill(z, &Z{Which: air.Z_Which_textvec, Textvec: []string{"Holmes", "Watson"}})
	if err != nil {
		t.Fatalf("zfill: %v", err)
	}
	out := new(BytesZ)
	if err := Extract(out, air.Z_TypeID, z.Struct); err != nil {
		t.Errorf("Extract(%v) error: %v", z, err)
	}
	want := &BytesZ{Which: air.Z_Which_textvec, Textvec: [][]byte{[]byte("Holmes"), []byte("Watson")}}
	if !reflect.DeepEqual(out, want) {
		t.Errorf("Extract(%v) produced %s; want %s", z, zpretty.Sprint(out), zpretty.Sprint(want))
	}
}
func zdateFilledSegment(n int32, packed bool) (*capnp.Segment, []byte) {
	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		panic(err)
	}
	z, err := air.NewRootZ(seg)
	if err != nil {
		panic(err)
	}
	list, err := air.NewZdate_List(seg, n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < int(n); i++ {
		d, err := air.NewZdate(seg)
		if err != nil {
			panic(err)
		}
		d.SetMonth(12)
		d.SetDay(7)
		d.SetYear(int16(2004 + i))
		list.Set(i, d)
	}
	z.SetZdatevec(list)

	if packed {
		b, err := msg.MarshalPacked()
		if err != nil {
			panic(err)
		}
		return seg, b
	}
	b, err := msg.Marshal()
	if err != nil {
		panic(err)
	}
	return seg, b
}
示例#21
0
func TestInsert(t *testing.T) {
	for _, test := range goodTests {
		_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
		if err != nil {
			t.Errorf("NewMessage for %s: %v", zpretty.Sprint(test), err)
			continue
		}
		z, err := air.NewRootZ(seg)
		if err != nil {
			t.Errorf("NewRootZ for %s: %v", zpretty.Sprint(test), err)
			continue
		}
		err = Insert(air.Z_TypeID, z.Struct, &test)
		if err != nil {
			t.Errorf("Insert(%s) error: %v", zpretty.Sprint(test), err)
		}
		if equal, err := zequal(&test, z); err != nil {
			t.Errorf("Insert(%s) compare err: %v", zpretty.Sprint(test), err)
		} else if !equal {
			t.Errorf("Insert(%s) produced %v", zpretty.Sprint(test), z)
		}
	}
}
示例#22
0
func Example() {
	// Make a brand new empty message.
	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))

	// If you want runtime-type identification, this is easily obtained. Just
	// wrap everything in a struct that contains a single anoymous union (e.g. struct Z).
	// Then always set a Z as the root object in you message/first segment.
	// The cost of the extra word of storage is usually worth it, as
	// then human readable output is easily obtained via a shell command such as
	//
	// $ cat binary.cpz | capnp decode aircraft.capnp Z
	//
	// If you need to conserve space, and know your content in advance, it
	// isn't necessary to use an anonymous union. Just supply the type name
	// in place of 'Z' in the decode command above.

	// There can only be one root.  Subsequent NewRoot* calls will set the root
	// pointer and orphan the previous root.
	z, err := air.NewRootZ(seg)
	if err != nil {
		panic(err)
	}

	// then non-root objects:
	aircraft, err := z.NewAircraft()
	if err != nil {
		panic(err)
	}
	b737, err := aircraft.NewB737()
	if err != nil {
		panic(err)
	}
	planebase, err := b737.NewBase()
	if err != nil {
		panic(err)
	}

	// Set primitive fields
	planebase.SetCanFly(true)
	planebase.SetName("Henrietta")
	planebase.SetRating(100)
	planebase.SetMaxSpeed(876) // km/hr
	// if we don't set capacity, it will get the default value, in this case 0.
	//planebase.SetCapacity(26020) // Liters fuel

	// Creating a list
	homes, err := air.NewAirport_List(seg, 2)
	if err != nil {
		panic(err)
	}
	homes.Set(0, air.Airport_jfk)
	homes.Set(1, air.Airport_lax)
	// Setting a list field
	planebase.SetHomes(homes)

	// Ready to write!

	// You can write to memory...
	buf, err := msg.Marshal()
	if err != nil {
		panic(err)
	}
	_ = buf

	// ... or write to an io.Writer.
	file, err := ioutil.TempFile("", "go-capnproto")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	defer os.Remove(file.Name())
	err = capnp.NewEncoder(file).Encode(msg)
	if err != nil {
		panic(err)
	}
}