コード例 #1
0
ファイル: typedef_test.go プロジェクト: thriftrw/thriftrw-go
func TestBinarySet(t *testing.T) {
	tests := []struct {
		x td.BinarySet
		v wire.Value
	}{
		{
			td.BinarySet{},
			wire.NewValueSet(
				wire.ValueListFromSlice(wire.TBinary, []wire.Value{}),
			),
		},
		{
			td.BinarySet{
				{1, 2, 3},
				{4, 5, 6},
			},
			wire.NewValueSet(
				wire.ValueListFromSlice(wire.TBinary, []wire.Value{
					wire.NewValueBinary([]byte{1, 2, 3}),
					wire.NewValueBinary([]byte{4, 5, 6}),
				}),
			),
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, &tt.x, tt.v, "BinarySet")
	}
}
コード例 #2
0
ファイル: typedef_test.go プロジェクト: thriftrw/thriftrw-go
func TestUnhashableSetAlias(t *testing.T) {
	tests := []struct {
		x td.FrameGroup
		v wire.Value
	}{
		{
			td.FrameGroup{},
			wire.NewValueSet(
				wire.ValueListFromSlice(wire.TStruct, []wire.Value{}),
			),
		},
		{
			td.FrameGroup{
				&ts.Frame{TopLeft: &ts.Point{X: 1, Y: 2}, Size: &ts.Size{Width: 3, Height: 4}},
				&ts.Frame{TopLeft: &ts.Point{X: 5, Y: 6}, Size: &ts.Size{Width: 7, Height: 8}},
			},
			wire.NewValueSet(
				wire.ValueListFromSlice(wire.TStruct, []wire.Value{
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 1, Value: wire.NewValueDouble(1)},
							{ID: 2, Value: wire.NewValueDouble(2)},
						}})},
						{ID: 2, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 1, Value: wire.NewValueDouble(3)},
							{ID: 2, Value: wire.NewValueDouble(4)},
						}})},
					}}),
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 1, Value: wire.NewValueDouble(5)},
							{ID: 2, Value: wire.NewValueDouble(6)},
						}})},
						{ID: 2, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 1, Value: wire.NewValueDouble(7)},
							{ID: 2, Value: wire.NewValueDouble(8)},
						}})},
					}}),
				}),
			),
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, &tt.x, tt.v, "FrameGroup")
	}
}
コード例 #3
0
ファイル: typedef_test.go プロジェクト: thriftrw/thriftrw-go
func TestTypedefContainer(t *testing.T) {
	tests := []struct {
		x td.EventGroup
		v wire.Value
	}{
		{
			td.EventGroup{
				&td.Event{
					UUID: &td.UUID{High: 100, Low: 200},
					Time: (*td.Timestamp)(int64p(42)),
				},
				&td.Event{
					UUID: &td.UUID{High: 0, Low: 42},
					Time: (*td.Timestamp)(int64p(100)),
				},
			},
			wire.NewValueList(
				wire.ValueListFromSlice(wire.TStruct, []wire.Value{
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 1, Value: wire.NewValueI64(100)},
							{ID: 2, Value: wire.NewValueI64(200)},
						}})},
						{ID: 2, Value: wire.NewValueI64(42)},
					}}),
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 1, Value: wire.NewValueI64(0)},
							{ID: 2, Value: wire.NewValueI64(42)},
						}})},
						{ID: 2, Value: wire.NewValueI64(100)},
					}}),
				}),
			),
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, &tt.x, tt.v, "EventGroup")
	}
}
コード例 #4
0
func TestListOfBinaryReadNil(t *testing.T) {
	value := wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
		ID: 1,
		Value: wire.NewValueList(
			wire.ValueListFromSlice(wire.TBinary, []wire.Value{
				wire.NewValueBinary([]byte("foo")),
				wire.NewValueBinary(nil),
				wire.NewValueBinary([]byte("bar")),
				wire.NewValueBinary([]byte("baz")),
			}),
		),
	}}})

	var c tc.PrimitiveContainers
	require.NoError(t, c.FromWire(value))

	got, err := c.ToWire()
	require.NoError(t, err)
	require.NoError(t, wire.EvaluateValue(got))
	assert.True(t, wire.ValuesAreEqual(value, got))
}
コード例 #5
0
ファイル: value_test.go プロジェクト: thriftrw/thriftrw-go
func vset(typ wire.Type, vs ...wire.Value) wire.Value {
	return wire.NewValueSet(wire.ValueListFromSlice(typ, vs))
}
コード例 #6
0
func TestCrazyTown(t *testing.T) {
	tests := []struct {
		desc string
		x    tc.ContainersOfContainers
		v    wire.Value
	}{
		{
			"ListOfLists",
			tc.ContainersOfContainers{
				ListOfLists: [][]int32{
					{1, 2, 3},
					{4, 5, 6},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueList(
					wire.ValueListFromSlice(wire.TList, []wire.Value{
						wire.NewValueList(
							wire.ValueListFromSlice(wire.TI32, []wire.Value{
								wire.NewValueI32(1),
								wire.NewValueI32(2),
								wire.NewValueI32(3),
							}),
						),
						wire.NewValueList(
							wire.ValueListFromSlice(wire.TI32, []wire.Value{
								wire.NewValueI32(4),
								wire.NewValueI32(5),
								wire.NewValueI32(6),
							}),
						),
					}),
				)},
			}}),
		},
		{
			"ListOfSets",
			tc.ContainersOfContainers{
				ListOfSets: []map[int32]struct{}{
					{
						1: struct{}{},
						2: struct{}{},
						3: struct{}{},
					},
					{
						4: struct{}{},
						5: struct{}{},
						6: struct{}{},
					},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 2, Value: wire.NewValueList(
					wire.ValueListFromSlice(wire.TSet, []wire.Value{
						wire.NewValueSet(
							wire.ValueListFromSlice(wire.TI32, []wire.Value{
								wire.NewValueI32(1),
								wire.NewValueI32(2),
								wire.NewValueI32(3),
							}),
						),
						wire.NewValueSet(
							wire.ValueListFromSlice(wire.TI32, []wire.Value{
								wire.NewValueI32(4),
								wire.NewValueI32(5),
								wire.NewValueI32(6),
							}),
						),
					}),
				)},
			}}),
		},
		{
			"ListOfMaps",
			tc.ContainersOfContainers{
				ListOfMaps: []map[int32]int32{
					{
						1: 100,
						2: 200,
						3: 300,
					},
					{
						4: 400,
						5: 500,
						6: 600,
					},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 3, Value: wire.NewValueList(
					wire.ValueListFromSlice(wire.TMap, []wire.Value{
						wire.NewValueMap(
							wire.MapItemListFromSlice(wire.TI32, wire.TI32, []wire.MapItem{
								{Key: wire.NewValueI32(1), Value: wire.NewValueI32(100)},
								{Key: wire.NewValueI32(2), Value: wire.NewValueI32(200)},
								{Key: wire.NewValueI32(3), Value: wire.NewValueI32(300)},
							}),
						),
						wire.NewValueMap(
							wire.MapItemListFromSlice(wire.TI32, wire.TI32, []wire.MapItem{
								{Key: wire.NewValueI32(4), Value: wire.NewValueI32(400)},
								{Key: wire.NewValueI32(5), Value: wire.NewValueI32(500)},
								{Key: wire.NewValueI32(6), Value: wire.NewValueI32(600)},
							}),
						),
					}),
				)},
			}}),
		},
		{
			"SetOfSets",
			tc.ContainersOfContainers{
				SetOfSets: []map[string]struct{}{
					{
						"1": struct{}{},
						"2": struct{}{},
						"3": struct{}{},
					},
					{
						"4": struct{}{},
						"5": struct{}{},
						"6": struct{}{},
					},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 4, Value: wire.NewValueSet(
					wire.ValueListFromSlice(wire.TSet, []wire.Value{
						wire.NewValueSet(
							wire.ValueListFromSlice(wire.TBinary, []wire.Value{
								wire.NewValueString("1"),
								wire.NewValueString("2"),
								wire.NewValueString("3"),
							}),
						),
						wire.NewValueSet(
							wire.ValueListFromSlice(wire.TBinary, []wire.Value{
								wire.NewValueString("4"),
								wire.NewValueString("5"),
								wire.NewValueString("6"),
							}),
						),
					}),
				)},
			}}),
		},
		{
			"SetOfLists",
			tc.ContainersOfContainers{
				SetOfLists: [][]string{
					{"1", "2", "3"},
					{"4", "5", "6"},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 5, Value: wire.NewValueSet(
					wire.ValueListFromSlice(wire.TList, []wire.Value{
						wire.NewValueList(
							wire.ValueListFromSlice(wire.TBinary, []wire.Value{
								wire.NewValueString("1"),
								wire.NewValueString("2"),
								wire.NewValueString("3"),
							}),
						),
						wire.NewValueList(
							wire.ValueListFromSlice(wire.TBinary, []wire.Value{
								wire.NewValueString("4"),
								wire.NewValueString("5"),
								wire.NewValueString("6"),
							}),
						),
					}),
				)},
			}}),
		},
		{
			"SetOfMaps",
			tc.ContainersOfContainers{
				SetOfMaps: []map[string]string{
					{
						"1": "one",
						"2": "two",
						"3": "three",
					},
					{
						"4": "four",
						"5": "five",
						"6": "six",
					},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 6, Value: wire.NewValueSet(
					wire.ValueListFromSlice(wire.TMap, []wire.Value{
						wire.NewValueMap(
							wire.MapItemListFromSlice(wire.TBinary, wire.TBinary, []wire.MapItem{
								{Key: wire.NewValueString("1"), Value: wire.NewValueString("one")},
								{Key: wire.NewValueString("2"), Value: wire.NewValueString("two")},
								{Key: wire.NewValueString("3"), Value: wire.NewValueString("three")},
							}),
						),
						wire.NewValueMap(
							wire.MapItemListFromSlice(wire.TBinary, wire.TBinary, []wire.MapItem{
								{Key: wire.NewValueString("4"), Value: wire.NewValueString("four")},
								{Key: wire.NewValueString("5"), Value: wire.NewValueString("five")},
								{Key: wire.NewValueString("6"), Value: wire.NewValueString("six")},
							}),
						),
					}),
				)},
			}}),
		},
		{
			"MapOfMapToInt",
			tc.ContainersOfContainers{
				MapOfMapToInt: []struct {
					Key   map[string]int32
					Value int64
				}{
					{
						Key:   map[string]int32{"1": 1, "2": 2, "3": 3},
						Value: 123,
					},
					{
						Key:   map[string]int32{"4": 4, "5": 5, "6": 6},
						Value: 456,
					},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 7, Value: wire.NewValueMap(
					wire.MapItemListFromSlice(wire.TMap, wire.TI64, []wire.MapItem{
						{
							Key: wire.NewValueMap(
								wire.MapItemListFromSlice(wire.TBinary, wire.TI32, []wire.MapItem{
									{Key: wire.NewValueString("1"), Value: wire.NewValueI32(1)},
									{Key: wire.NewValueString("2"), Value: wire.NewValueI32(2)},
									{Key: wire.NewValueString("3"), Value: wire.NewValueI32(3)},
								}),
							),
							Value: wire.NewValueI64(123),
						},
						{
							Key: wire.NewValueMap(
								wire.MapItemListFromSlice(wire.TBinary, wire.TI32, []wire.MapItem{
									{Key: wire.NewValueString("4"), Value: wire.NewValueI32(4)},
									{Key: wire.NewValueString("5"), Value: wire.NewValueI32(5)},
									{Key: wire.NewValueString("6"), Value: wire.NewValueI32(6)},
								}),
							),
							Value: wire.NewValueI64(456),
						},
					}),
				)},
			}}),
		},
		{
			"MapOfListToSet",
			tc.ContainersOfContainers{
				MapOfListToSet: []struct {
					Key   []int32
					Value map[int64]struct{}
				}{
					{
						Key: []int32{1, 2, 3},
						Value: map[int64]struct{}{
							1: {},
							2: {},
							3: {},
						},
					},
					{
						Key: []int32{4, 5, 6},
						Value: map[int64]struct{}{
							4: {},
							5: {},
							6: {},
						},
					},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 8, Value: wire.NewValueMap(
					wire.MapItemListFromSlice(wire.TList, wire.TSet, []wire.MapItem{
						{
							Key: wire.NewValueList(
								wire.ValueListFromSlice(wire.TI32, []wire.Value{
									wire.NewValueI32(1),
									wire.NewValueI32(2),
									wire.NewValueI32(3),
								}),
							),
							Value: wire.NewValueSet(
								wire.ValueListFromSlice(wire.TI64, []wire.Value{
									wire.NewValueI64(1),
									wire.NewValueI64(2),
									wire.NewValueI64(3),
								}),
							),
						},
						{
							Key: wire.NewValueList(
								wire.ValueListFromSlice(wire.TI32, []wire.Value{
									wire.NewValueI32(4),
									wire.NewValueI32(5),
									wire.NewValueI32(6),
								}),
							),
							Value: wire.NewValueSet(
								wire.ValueListFromSlice(wire.TI64, []wire.Value{
									wire.NewValueI64(4),
									wire.NewValueI64(5),
									wire.NewValueI64(6),
								}),
							),
						},
					}),
				)},
			}}),
		},
		{
			"MapOfSetToListOfDouble",
			tc.ContainersOfContainers{
				MapOfSetToListOfDouble: []struct {
					Key   map[int32]struct{}
					Value []float64
				}{
					{
						Key: map[int32]struct{}{
							1: {},
							2: {},
							3: {},
						},
						Value: []float64{1.0, 2.0, 3.0},
					},
					{
						Key: map[int32]struct{}{
							4: {},
							5: {},
							6: {},
						},
						Value: []float64{4.0, 5.0, 6.0},
					},
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 9, Value: wire.NewValueMap(
					wire.MapItemListFromSlice(wire.TSet, wire.TList, []wire.MapItem{
						{
							Key: wire.NewValueSet(
								wire.ValueListFromSlice(wire.TI32, []wire.Value{
									wire.NewValueI32(1),
									wire.NewValueI32(2),
									wire.NewValueI32(3),
								}),
							),
							Value: wire.NewValueList(
								wire.ValueListFromSlice(wire.TDouble, []wire.Value{
									wire.NewValueDouble(1.0),
									wire.NewValueDouble(2.0),
									wire.NewValueDouble(3.0),
								}),
							),
						},
						{
							Key: wire.NewValueSet(
								wire.ValueListFromSlice(wire.TI32, []wire.Value{
									wire.NewValueI32(4),
									wire.NewValueI32(5),
									wire.NewValueI32(6),
								}),
							),
							Value: wire.NewValueList(
								wire.ValueListFromSlice(wire.TDouble, []wire.Value{
									wire.NewValueDouble(4.0),
									wire.NewValueDouble(5.0),
									wire.NewValueDouble(6.0),
								}),
							),
						},
					}),
				)},
			}}),
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, &tt.x, tt.v, tt.desc)
	}
}
コード例 #7
0
func TestCollectionsOfPrimitives(t *testing.T) {
	tests := []struct {
		desc string
		p    tc.PrimitiveContainers
		v    wire.Value
	}{
		// Lists /////////////////////////////////////////////////////////////
		{
			"empty list",
			tc.PrimitiveContainers{ListOfInts: []int64{}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID:    2,
				Value: wire.NewValueList(wire.ValueListFromSlice(wire.TI64, []wire.Value{})),
			}}}),
		},
		{
			"list of ints",
			tc.PrimitiveContainers{ListOfInts: []int64{1, 2, 3}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 2,
				Value: wire.NewValueList(
					wire.ValueListFromSlice(wire.TI64, []wire.Value{
						wire.NewValueI64(1),
						wire.NewValueI64(2),
						wire.NewValueI64(3),
					}),
				),
			}}}),
		},
		{
			"list of binary",
			tc.PrimitiveContainers{
				ListOfBinary: [][]byte{
					[]byte("foo"), {}, []byte("bar"), []byte("baz"),
				},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 1,
				Value: wire.NewValueList(
					wire.ValueListFromSlice(wire.TBinary, []wire.Value{
						wire.NewValueBinary([]byte("foo")),
						wire.NewValueBinary([]byte{}),
						wire.NewValueBinary([]byte("bar")),
						wire.NewValueBinary([]byte("baz")),
					}),
				),
			}}}),
		},
		// Sets //////////////////////////////////////////////////////////////
		{
			"empty set",
			tc.PrimitiveContainers{SetOfStrings: map[string]struct{}{}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 3,
				Value: wire.NewValueSet(
					wire.ValueListFromSlice(wire.TBinary, []wire.Value{}),
				),
			}}}),
		},
		{
			"set of strings",
			tc.PrimitiveContainers{SetOfStrings: map[string]struct{}{
				"foo": {},
				"bar": {},
				"baz": {},
			}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 3,
				Value: wire.NewValueSet(
					wire.ValueListFromSlice(wire.TBinary, []wire.Value{
						wire.NewValueString("foo"),
						wire.NewValueString("bar"),
						wire.NewValueString("baz"),
					}),
				),
			}}}),
		},
		{
			"set of bytes",
			tc.PrimitiveContainers{SetOfBytes: map[int8]struct{}{
				-1:  {},
				1:   {},
				125: {},
			}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 4,
				Value: wire.NewValueSet(
					wire.ValueListFromSlice(wire.TI8, []wire.Value{
						wire.NewValueI8(-1),
						wire.NewValueI8(1),
						wire.NewValueI8(125),
					}),
				),
			}}}),
		},
		// Maps //////////////////////////////////////////////////////////////
		{
			"empty map",
			tc.PrimitiveContainers{MapOfStringToBool: map[string]bool{}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 6,
				Value: wire.NewValueMap(
					wire.MapItemListFromSlice(wire.TBinary, wire.TBool, []wire.MapItem{}),
				),
			}}}),
		},
		{
			"map of int to string",
			tc.PrimitiveContainers{MapOfIntToString: map[int32]string{
				-1:    "foo",
				1234:  "bar",
				-9876: "baz",
			}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 5,
				Value: wire.NewValueMap(
					wire.MapItemListFromSlice(wire.TI32, wire.TBinary, []wire.MapItem{
						{Key: wire.NewValueI32(-1), Value: wire.NewValueString("foo")},
						{Key: wire.NewValueI32(1234), Value: wire.NewValueString("bar")},
						{Key: wire.NewValueI32(-9876), Value: wire.NewValueString("baz")},
					}),
				),
			}}}),
		},
		{
			"map of string to bool",
			tc.PrimitiveContainers{MapOfStringToBool: map[string]bool{
				"foo": true,
				"bar": false,
				"baz": true,
			}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{{
				ID: 6,
				Value: wire.NewValueMap(
					wire.MapItemListFromSlice(wire.TBinary, wire.TBool, []wire.MapItem{
						{Key: wire.NewValueString("foo"), Value: wire.NewValueBool(true)},
						{Key: wire.NewValueString("bar"), Value: wire.NewValueBool(false)},
						{Key: wire.NewValueString("baz"), Value: wire.NewValueBool(true)},
					}),
				),
			}}}),
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, &tt.p, tt.v, tt.desc)
	}
}
コード例 #8
0
func TestListOfStructs(t *testing.T) {
	tests := []struct {
		s ts.Graph
		v wire.Value
	}{
		{
			ts.Graph{Edges: []*ts.Edge{}},
			singleFieldStruct(1, wire.NewValueList(
				wire.ValueListFromSlice(wire.TStruct, nil),
			)),
		},
		{
			ts.Graph{Edges: []*ts.Edge{
				{
					StartPoint: &ts.Point{X: 1.0, Y: 2.0},
					EndPoint:   &ts.Point{X: 3.0, Y: 4.0},
				},
				{
					StartPoint: &ts.Point{X: 5.0, Y: 6.0},
					EndPoint:   &ts.Point{X: 7.0, Y: 8.0},
				},
				{
					StartPoint: &ts.Point{X: 9.0, Y: 10.0},
					EndPoint:   &ts.Point{X: 11.0, Y: 12.0},
				},
			}},
			singleFieldStruct(1, wire.NewValueList(
				wire.ValueListFromSlice(wire.TStruct, []wire.Value{
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(1.0)},
								{ID: 2, Value: wire.NewValueDouble(2.0)},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(3.0)},
								{ID: 2, Value: wire.NewValueDouble(4.0)},
							}}),
						},
					}}),
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(5.0)},
								{ID: 2, Value: wire.NewValueDouble(6.0)},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(7.0)},
								{ID: 2, Value: wire.NewValueDouble(8.0)},
							}}),
						},
					}}),
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(9.0)},
								{ID: 2, Value: wire.NewValueDouble(10.0)},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(11.0)},
								{ID: 2, Value: wire.NewValueDouble(12.0)},
							}}),
						},
					}}),
				}),
			)),
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, &tt.s, tt.v, "Graph")
	}
}
コード例 #9
0
func TestEnumContainers(t *testing.T) {
	tests := []struct {
		s tc.EnumContainers
		v wire.Value
	}{
		{
			tc.EnumContainers{
				ListOfEnums: []te.EnumDefault{
					te.EnumDefaultFoo,
					te.EnumDefaultBar,
				},
			},
			singleFieldStruct(1, wire.NewValueList(
				wire.ValueListFromSlice(wire.TI32, []wire.Value{
					wire.NewValueI32(0),
					wire.NewValueI32(1),
				}),
			)),
		},
		{
			tc.EnumContainers{
				SetOfEnums: map[te.EnumWithValues]struct{}{
					te.EnumWithValuesX: {},
					te.EnumWithValuesZ: {},
				},
			},
			singleFieldStruct(2, wire.NewValueSet(
				wire.ValueListFromSlice(wire.TI32, []wire.Value{
					wire.NewValueI32(123),
					wire.NewValueI32(789),
				}),
			)),
		},
		{
			tc.EnumContainers{
				MapOfEnums: map[te.EnumWithDuplicateValues]int32{
					te.EnumWithDuplicateValuesP: 123,
					te.EnumWithDuplicateValuesQ: 456,
				},
			},
			singleFieldStruct(3, wire.NewValueMap(
				wire.MapItemListFromSlice(wire.TI32, wire.TI32, []wire.MapItem{
					{Key: wire.NewValueI32(0), Value: wire.NewValueI32(123)},
					{Key: wire.NewValueI32(-1), Value: wire.NewValueI32(456)},
				}),
			)),
		},
		{
			// this is the same as the one above except we're using "R" intsead
			// of "P" (they both have the same value)
			tc.EnumContainers{
				MapOfEnums: map[te.EnumWithDuplicateValues]int32{
					te.EnumWithDuplicateValuesR: 123,
					te.EnumWithDuplicateValuesQ: 456,
				},
			},
			singleFieldStruct(3, wire.NewValueMap(

				wire.MapItemListFromSlice(wire.TI32, wire.TI32, []wire.MapItem{
					{Key: wire.NewValueI32(0), Value: wire.NewValueI32(123)},
					{Key: wire.NewValueI32(-1), Value: wire.NewValueI32(456)},
				}),
			)),
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, &tt.s, tt.v, "EnumContainers")
	}
}
コード例 #10
0
ファイル: struct_test.go プロジェクト: thriftrw/thriftrw-go
func TestStructValidation(t *testing.T) {
	tests := []struct {
		desc        string
		serialize   thriftType
		deserialize wire.Value
		typ         reflect.Type // must be set if serialize is not
		wantError   string
	}{
		{
			desc: "Point: missing X",
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueDouble(42)},
			}}),
			typ:       reflect.TypeOf(ts.Point{}),
			wantError: "field Y of Point is required",
		},
		{
			desc: "Point: missing Y",
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 2, Value: wire.NewValueDouble(42)},
			}}),
			typ:       reflect.TypeOf(ts.Point{}),
			wantError: "field X of Point is required",
		},
		{
			desc: "Size: missing width",
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueDouble(42)},
			}}),
			typ:       reflect.TypeOf(ts.Size{}),
			wantError: "field Height of Size is required",
		},
		{
			desc: "Size: missing height",
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 2, Value: wire.NewValueDouble(42)},
			}}),
			typ:       reflect.TypeOf(ts.Size{}),
			wantError: "field Width of Size is required",
		},
		{
			desc:      "Frame: missing topLeft",
			serialize: &ts.Frame{Size: &ts.Size{Width: 1, Height: 2}},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 2,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueDouble(1)},
						{ID: 2, Value: wire.NewValueDouble(2)},
					}}),
				},
			}}),
			wantError: "field TopLeft of Frame is required",
		},
		{
			desc:      "Frame: missing Size",
			serialize: &ts.Frame{TopLeft: &ts.Point{X: 1, Y: 2}},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 1,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueDouble(1)},
						{ID: 2, Value: wire.NewValueDouble(2)},
					}}),
				},
			}}),
			wantError: "field Size of Frame is required",
		},
		{
			desc: "Frame: topLeft: missing y",
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 1,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueDouble(1)},
					}}),
				},
				{
					ID: 2,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueDouble(1)},
						{ID: 2, Value: wire.NewValueDouble(2)},
					}}),
				},
			}}),
			typ:       reflect.TypeOf(ts.Frame{}),
			wantError: "field Y of Point is required",
		},
		{
			desc:        "Graph: missing edges",
			serialize:   &ts.Graph{Edges: nil},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
			wantError:   "field Edges of Graph is required",
		},
		{
			desc: "Graph: edges: misssing end",
			serialize: &ts.Graph{
				Edges: []*ts.Edge{
					{StartPoint: &ts.Point{X: 1, Y: 2}, EndPoint: &ts.Point{X: 3, Y: 4}},
					{StartPoint: &ts.Point{X: 5, Y: 6}},
				},
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 1,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TStruct, []wire.Value{
							wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{
									ID: 1,
									Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
										{ID: 1, Value: wire.NewValueDouble(1)},
										{ID: 2, Value: wire.NewValueDouble(2)},
									}}),
								},
								{
									ID: 2,
									Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
										{ID: 1, Value: wire.NewValueDouble(3)},
										{ID: 2, Value: wire.NewValueDouble(4)},
									}}),
								},
							}}),
							wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{
									ID: 1,
									Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
										{ID: 1, Value: wire.NewValueDouble(1)},
										{ID: 2, Value: wire.NewValueDouble(2)},
									}}),
								},
							}}),
						}),
					),
				},
			}}),
			wantError: "field EndPoint of Edge is required",
		},
		{
			desc: "User: contact: missing emailAddress",
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueString("hello")},
				{
					ID:    2,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
				},
			}}),
			typ:       reflect.TypeOf(ts.User{}),
			wantError: "field EmailAddress of ContactInfo is required",
		},
		{
			desc: "PrimitiveContainersRequired: missing list",
			serialize: &tc.PrimitiveContainersRequired{
				SetOfInts: map[int32]struct{}{
					1: {},
					2: {},
					3: {},
				},
				MapOfIntsToDoubles: map[int64]float64{1: 2.3, 4: 5.6},
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 2,
					Value: wire.NewValueSet(
						wire.ValueListFromSlice(wire.TI32, []wire.Value{
							wire.NewValueI32(1),
							wire.NewValueI32(2),
							wire.NewValueI32(3),
						}),
					),
				},
				{
					ID: 3,
					Value: wire.NewValueMap(
						wire.MapItemListFromSlice(wire.TI64, wire.TDouble, []wire.MapItem{
							{
								Key:   wire.NewValueI64(1),
								Value: wire.NewValueDouble(2.3),
							},
							{
								Key:   wire.NewValueI64(4),
								Value: wire.NewValueDouble(5.6),
							},
						}),
					),
				},
			}}),
			wantError: "field ListOfStrings of PrimitiveContainersRequired is required",
		},
		{
			desc: "PrimitiveContainersRequired: missing set",
			serialize: &tc.PrimitiveContainersRequired{
				ListOfStrings:      []string{"hello", "world"},
				MapOfIntsToDoubles: map[int64]float64{1: 2.3, 4: 5.6},
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 1,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TBinary, []wire.Value{
							wire.NewValueString("hello"),
							wire.NewValueString("world"),
						}),
					),
				},
				{
					ID: 3,
					Value: wire.NewValueMap(
						wire.MapItemListFromSlice(wire.TI64, wire.TDouble, []wire.MapItem{
							{
								Key:   wire.NewValueI64(1),
								Value: wire.NewValueDouble(2.3),
							},
							{
								Key:   wire.NewValueI64(4),
								Value: wire.NewValueDouble(5.6),
							},
						}),
					),
				},
			}}),
			wantError: "field SetOfInts of PrimitiveContainersRequired is required",
		},
		{
			desc: "PrimitiveContainersRequired: missing map",
			serialize: &tc.PrimitiveContainersRequired{
				ListOfStrings: []string{"hello", "world"},
				SetOfInts: map[int32]struct{}{
					1: {},
					2: {},
					3: {},
				},
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 1,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TBinary, []wire.Value{
							wire.NewValueString("hello"),
							wire.NewValueString("world"),
						}),
					),
				},
				{
					ID: 2,
					Value: wire.NewValueSet(
						wire.ValueListFromSlice(wire.TI32, []wire.Value{
							wire.NewValueI32(1),
							wire.NewValueI32(2),
							wire.NewValueI32(3),
						}),
					),
				},
			}}),
			wantError: "field MapOfIntsToDoubles of PrimitiveContainersRequired is required",
		},
		{
			desc:        "Document: empty",
			serialize:   &tu.Document{},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
			wantError:   "Document should have exactly one field: got 0 fields",
		},
		{
			desc: "Document: multiple",
			serialize: &tu.Document{
				Pdf:       td.PDF{},
				PlainText: stringp("hello"),
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID:    1,
					Value: wire.NewValueBinary([]byte{}),
				},
				{
					ID:    2,
					Value: wire.NewValueString("hello"),
				},
			}}),
			wantError: "Document should have exactly one field: got 2 fields",
		},
		{
			desc:        "ArbitraryValue: empty",
			serialize:   &tu.ArbitraryValue{},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
			wantError:   "ArbitraryValue should have exactly one field: got 0 fields",
		},
		{
			desc: "ArbitraryValue: primitives",
			serialize: &tu.ArbitraryValue{
				BoolValue:   boolp(true),
				Int64Value:  int64p(42),
				StringValue: stringp(""),
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueBool(true)},
				{ID: 2, Value: wire.NewValueI64(42)},
				{ID: 3, Value: wire.NewValueString("")},
			}}),
			wantError: "ArbitraryValue should have exactly one field: got 3 fields",
		},
		{
			desc: "ArbitraryValue: full",
			serialize: &tu.ArbitraryValue{
				BoolValue:   boolp(true),
				Int64Value:  int64p(42),
				StringValue: stringp(""),
				ListValue: []*tu.ArbitraryValue{
					{BoolValue: boolp(true)},
					{Int64Value: int64p(42)},
					{StringValue: stringp("")},
				},
				MapValue: map[string]*tu.ArbitraryValue{
					"bool":   {BoolValue: boolp(true)},
					"int":    {Int64Value: int64p(42)},
					"string": {StringValue: stringp("")},
				},
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueBool(true)},
				{ID: 2, Value: wire.NewValueI64(42)},
				{ID: 3, Value: wire.NewValueString("")},
				{
					ID: 4,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TStruct, []wire.Value{
							wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueBool(true)},
							}}),
							wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 2, Value: wire.NewValueI64(42)},
							}}),
							wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 3, Value: wire.NewValueString("")},
							}}),
						}),
					),
				},
				{
					ID: 5,
					Value: wire.NewValueMap(
						wire.MapItemListFromSlice(wire.TBinary, wire.TStruct, []wire.MapItem{
							{
								Key: wire.NewValueString("bool"),
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
									{ID: 1, Value: wire.NewValueBool(true)},
								}}),
							},
							{
								Key: wire.NewValueString("int"),
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
									{ID: 2, Value: wire.NewValueI64(42)},
								}}),
							},
							{
								Key: wire.NewValueString("string"),
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
									{ID: 3, Value: wire.NewValueString("")},
								}}),
							},
						}),
					),
				},
			}}),
			wantError: "ArbitraryValue should have exactly one field: got 5 fields",
		},
		{
			desc: "ArbitraryValue: error inside a list",
			serialize: &tu.ArbitraryValue{
				ListValue: []*tu.ArbitraryValue{
					{BoolValue: boolp(true)},
					{},
				},
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 4,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TStruct, []wire.Value{
							wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueBool(true)},
							}}),
							wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
						})),
				},
			}}),
			wantError: "ArbitraryValue should have exactly one field: got 0 fields",
		},
		{
			desc: "ArbitraryValue: error inside a map value",
			serialize: &tu.ArbitraryValue{
				MapValue: map[string]*tu.ArbitraryValue{
					"bool":  {BoolValue: boolp(true)},
					"empty": {},
				},
			},
			deserialize: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 5,
					Value: wire.NewValueMap(
						wire.MapItemListFromSlice(wire.TBinary, wire.TStruct, []wire.MapItem{
							{
								Key: wire.NewValueString("bool"),
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
									{ID: 1, Value: wire.NewValueBool(true)},
								}}),
							},
							{
								Key:   wire.NewValueString("empty"),
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
							},
						}),
					),
				},
			}}),
			wantError: "ArbitraryValue should have exactly one field: got 0 fields",
		},
		{
			desc: "FrameGroup: error inside a set",
			serialize: &td.FrameGroup{
				&ts.Frame{
					TopLeft: &ts.Point{X: 1, Y: 2},
					Size:    &ts.Size{Width: 3, Height: 4},
				},
				&ts.Frame{TopLeft: &ts.Point{X: 5, Y: 6}},
			},
			deserialize: wire.NewValueSet(
				wire.ValueListFromSlice(wire.TStruct, []wire.Value{
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{
									ID:    1,
									Value: wire.NewValueDouble(1),
								},
								{
									ID:    2,
									Value: wire.NewValueDouble(2),
								},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{
									ID:    1,
									Value: wire.NewValueDouble(3),
								},
								{
									ID:    2,
									Value: wire.NewValueDouble(4),
								},
							}}),
						},
					}}),
					wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{
									ID:    1,
									Value: wire.NewValueDouble(5),
								},
								{
									ID:    2,
									Value: wire.NewValueDouble(6),
								},
							}}),
						},
					}}),
				}),
			),
			wantError: "field Size of Frame is required",
		},
		{
			desc: "EdgeMap: error inside a map key",
			serialize: &td.EdgeMap{
				{
					Key:   &ts.Edge{StartPoint: &ts.Point{X: 1, Y: 2}},
					Value: &ts.Edge{StartPoint: &ts.Point{X: 3, Y: 4}, EndPoint: &ts.Point{X: 5, Y: 6}},
				},
			},
			deserialize: wire.NewValueMap(
				wire.MapItemListFromSlice(wire.TStruct, wire.TStruct, []wire.MapItem{
					{
						Key: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{
								ID: 1,
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
									{ID: 1, Value: wire.NewValueDouble(1)},
									{ID: 2, Value: wire.NewValueDouble(2)},
								}}),
							},
						}}),
						Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{
								ID: 1,
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
									{ID: 1, Value: wire.NewValueDouble(3)},
									{ID: 2, Value: wire.NewValueDouble(4)},
								}}),
							},
							{
								ID: 2,
								Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
									{ID: 1, Value: wire.NewValueDouble(5)},
									{ID: 2, Value: wire.NewValueDouble(6)},
								}}),
							},
						}}),
					},
				}),
			),
			wantError: "field EndPoint of Edge is required",
		},
	}

	for _, tt := range tests {
		var typ reflect.Type
		if tt.serialize != nil {
			typ = reflect.TypeOf(tt.serialize).Elem()
			v, err := tt.serialize.ToWire()
			if err == nil {
				err = wire.EvaluateValue(v)
			}
			if assert.Error(t, err, "%v: expected failure but got %v", tt.desc, v) {
				assert.Contains(t, err.Error(), tt.wantError, tt.desc)
			}
		} else {
			typ = tt.typ
		}

		if typ == nil {
			t.Fatalf("invalid test %q: either typ or serialize must be set", tt.desc)
		}

		x := reflect.New(typ)
		args := []reflect.Value{reflect.ValueOf(tt.deserialize)}
		e := x.MethodByName("FromWire").Call(args)[0].Interface()
		if assert.NotNil(t, e, "%v: expected failure but got %v", tt.desc, x) {
			assert.Contains(t, e.(error).Error(), tt.wantError, tt.desc)
		}
	}
}
コード例 #11
0
ファイル: struct_test.go プロジェクト: thriftrw/thriftrw-go
func TestStructWithDefaults(t *testing.T) {
	enumDefaultFoo := te.EnumDefaultFoo
	enumDefaultBar := te.EnumDefaultBar
	enumDefaultBaz := te.EnumDefaultBaz

	tests := []struct {
		give     *ts.DefaultsStruct
		giveWire wire.Value

		wantToWire   wire.Value
		wantFromWire *ts.DefaultsStruct
	}{
		{
			give:     &ts.DefaultsStruct{},
			giveWire: wire.NewValueStruct(wire.Struct{}),

			wantToWire: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueI32(100)},
				{ID: 2, Value: wire.NewValueI32(200)},
				{ID: 3, Value: wire.NewValueI32(1)},
				{ID: 4, Value: wire.NewValueI32(2)},
				{
					ID: 5,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TBinary, []wire.Value{
							wire.NewValueString("hello"),
							wire.NewValueString("world"),
						}),
					),
				},
				{
					ID: 6,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TDouble, []wire.Value{
							wire.NewValueDouble(1.0),
							wire.NewValueDouble(2.0),
							wire.NewValueDouble(3.0),
						}),
					),
				},
				{
					ID: 7,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(1.0)},
								{ID: 2, Value: wire.NewValueDouble(2.0)},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(100.0)},
								{ID: 2, Value: wire.NewValueDouble(200.0)},
							}}),
						},
					}}),
				},
				{
					ID: 8,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(1.0)},
								{ID: 2, Value: wire.NewValueDouble(2.0)},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(3.0)},
								{ID: 2, Value: wire.NewValueDouble(4.0)},
							}}),
						},
					}}),
				},
			}}),
			wantFromWire: &ts.DefaultsStruct{
				RequiredPrimitive: int32p(100),
				OptionalPrimitive: int32p(200),
				RequiredEnum:      &enumDefaultBar,
				OptionalEnum:      &enumDefaultBaz,
				RequiredList:      []string{"hello", "world"},
				OptionalList:      []float64{1.0, 2.0, 3.0},
				RequiredStruct: &ts.Frame{
					TopLeft: &ts.Point{X: 1.0, Y: 2.0},
					Size:    &ts.Size{Width: 100.0, Height: 200.0},
				},
				OptionalStruct: &ts.Edge{
					StartPoint: &ts.Point{X: 1.0, Y: 2.0},
					EndPoint:   &ts.Point{X: 3.0, Y: 4.0},
				},
			},
		},
		{
			give: &ts.DefaultsStruct{
				RequiredPrimitive: int32p(0),
				OptionalEnum:      &enumDefaultFoo,
				RequiredList:      []string{},
			},
			giveWire: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueI32(0)},
				{ID: 4, Value: wire.NewValueI32(0)},
				{
					ID: 5,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TBinary, []wire.Value{}),
					),
				},
			}}),

			wantToWire: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueI32(0)},
				{ID: 2, Value: wire.NewValueI32(200)},
				{ID: 3, Value: wire.NewValueI32(1)},
				{ID: 4, Value: wire.NewValueI32(0)},
				{
					ID: 5,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TBinary, []wire.Value{}),
					),
				},
				{
					ID: 6,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TDouble, []wire.Value{
							wire.NewValueDouble(1.0),
							wire.NewValueDouble(2.0),
							wire.NewValueDouble(3.0),
						}),
					),
				},
				{
					ID: 7,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(1.0)},
								{ID: 2, Value: wire.NewValueDouble(2.0)},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(100.0)},
								{ID: 2, Value: wire.NewValueDouble(200.0)},
							}}),
						},
					}}),
				},
				{
					ID: 8,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{
							ID: 1,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(1.0)},
								{ID: 2, Value: wire.NewValueDouble(2.0)},
							}}),
						},
						{
							ID: 2,
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueDouble(3.0)},
								{ID: 2, Value: wire.NewValueDouble(4.0)},
							}}),
						},
					}}),
				},
			}}),
			wantFromWire: &ts.DefaultsStruct{
				RequiredPrimitive: int32p(0),
				OptionalPrimitive: int32p(200),
				RequiredEnum:      &enumDefaultBar,
				OptionalEnum:      &enumDefaultFoo,
				RequiredList:      []string{},
				OptionalList:      []float64{1.0, 2.0, 3.0},
				RequiredStruct: &ts.Frame{
					TopLeft: &ts.Point{X: 1.0, Y: 2.0},
					Size:    &ts.Size{Width: 100.0, Height: 200.0},
				},
				OptionalStruct: &ts.Edge{
					StartPoint: &ts.Point{X: 1.0, Y: 2.0},
					EndPoint:   &ts.Point{X: 3.0, Y: 4.0},
				},
			},
		},
	}

	for _, tt := range tests {
		if gotWire, err := tt.give.ToWire(); assert.NoError(
			t, err, "%v.ToWire() failed", tt.give) {
			assert.True(
				t, wire.ValuesAreEqual(tt.wantToWire, gotWire),
				"%v.ToWire() != %v", tt.give, tt.wantToWire)
		}

		var gotFromWire ts.DefaultsStruct
		if err := gotFromWire.FromWire(tt.giveWire); assert.NoError(t, err) {
			assert.Equal(t, tt.wantFromWire, &gotFromWire)
		}
	}
}
コード例 #12
0
ファイル: struct_test.go プロジェクト: thriftrw/thriftrw-go
func TestStructRoundTripAndString(t *testing.T) {
	tests := []struct {
		desc string
		x    interface {
			thriftType
			String() string
		}
		v wire.Value
		s string
	}{
		{
			"PrimitiveRequiredStruct",
			&ts.PrimitiveRequiredStruct{
				BoolField:   true,
				ByteField:   1,
				Int16Field:  2,
				Int32Field:  3,
				Int64Field:  4,
				DoubleField: 5.0,
				StringField: "foo",
				BinaryField: []byte("bar"),
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueBool(true)},
				{ID: 2, Value: wire.NewValueI8(1)},
				{ID: 3, Value: wire.NewValueI16(2)},
				{ID: 4, Value: wire.NewValueI32(3)},
				{ID: 5, Value: wire.NewValueI64(4)},
				{ID: 6, Value: wire.NewValueDouble(5.0)},
				{ID: 7, Value: wire.NewValueString("foo")},
				{ID: 8, Value: wire.NewValueBinary([]byte("bar"))},
			}}),
			"",
		},
		{
			"PrimitiveOptionalStruct: all fields",
			&ts.PrimitiveOptionalStruct{
				BoolField:   boolp(true),
				ByteField:   bytep(1),
				Int16Field:  int16p(2),
				Int32Field:  int32p(3),
				Int64Field:  int64p(4),
				DoubleField: doublep(5.0),
				StringField: stringp("foo"),
				BinaryField: []byte("bar"),
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueBool(true)},
				{ID: 2, Value: wire.NewValueI8(1)},
				{ID: 3, Value: wire.NewValueI16(2)},
				{ID: 4, Value: wire.NewValueI32(3)},
				{ID: 5, Value: wire.NewValueI64(4)},
				{ID: 6, Value: wire.NewValueDouble(5.0)},
				{ID: 7, Value: wire.NewValueString("foo")},
				{ID: 8, Value: wire.NewValueBinary([]byte("bar"))},
			}}),
			"",
		},
		{
			"PrimitiveOptionalStruct: bool",
			&ts.PrimitiveOptionalStruct{BoolField: boolp(true)},
			singleFieldStruct(1, wire.NewValueBool(true)),
			"",
		},
		{
			"PrimitiveOptionalStruct: byte",
			&ts.PrimitiveOptionalStruct{ByteField: bytep(1)},
			singleFieldStruct(2, wire.NewValueI8(1)),
			"",
		},
		{
			"PrimitiveOptionalStruct: int16",
			&ts.PrimitiveOptionalStruct{Int16Field: int16p(2)},
			singleFieldStruct(3, wire.NewValueI16(2)),
			"",
		},
		{
			"PrimitiveOptionalStruct: int32",
			&ts.PrimitiveOptionalStruct{Int32Field: int32p(3)},
			singleFieldStruct(4, wire.NewValueI32(3)),
			"",
		},
		{
			"PrimitiveOptionalStruct: int64",
			&ts.PrimitiveOptionalStruct{Int64Field: int64p(4)},
			singleFieldStruct(5, wire.NewValueI64(4)),
			"",
		},
		{
			"PrimitiveOptionalStruct: double",
			&ts.PrimitiveOptionalStruct{DoubleField: doublep(5.0)},
			singleFieldStruct(6, wire.NewValueDouble(5.0)),
			"",
		},
		{
			"PrimitiveOptionalStruct: string",
			&ts.PrimitiveOptionalStruct{StringField: stringp("foo")},
			singleFieldStruct(7, wire.NewValueString("foo")),
			"",
		},
		{
			"PrimitiveOptionalStruct: binary",
			&ts.PrimitiveOptionalStruct{BinaryField: []byte("bar")},
			singleFieldStruct(8, wire.NewValueBinary([]byte("bar"))),
			"",
		},
		{
			"PrimitiveContainersRequired",
			&tc.PrimitiveContainersRequired{
				ListOfStrings:      []string{"foo", "bar", "baz"},
				SetOfInts:          map[int32]struct{}{1: {}, 2: {}},
				MapOfIntsToDoubles: map[int64]float64{1: 2.0, 3: 4.0},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 1,
					Value: wire.NewValueList(
						wire.ValueListFromSlice(wire.TBinary, []wire.Value([]wire.Value{
							wire.NewValueString("foo"),
							wire.NewValueString("bar"),
							wire.NewValueString("baz"),
						})),
					),
				},
				{
					ID: 2,
					Value: wire.NewValueSet(
						wire.ValueListFromSlice(wire.TI32, []wire.Value{
							wire.NewValueI32(1),
							wire.NewValueI32(2),
						}),
					),
				},
				{
					ID: 3,
					Value: wire.NewValueMap(
						wire.MapItemListFromSlice(wire.TI64, wire.TDouble, []wire.MapItem{
							{
								Key:   wire.NewValueI64(1),
								Value: wire.NewValueDouble(2.0),
							},
							{
								Key:   wire.NewValueI64(3),
								Value: wire.NewValueDouble(4.0),
							},
						}),
					),
				},
			}}),
			"",
		},
		{
			"Frame",
			&ts.Frame{
				TopLeft: &ts.Point{X: 1, Y: 2},
				Size:    &ts.Size{Width: 100, Height: 200},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{
					ID: 1,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueDouble(1.0)},
						{ID: 2, Value: wire.NewValueDouble(2.0)},
					}}),
				},
				{
					ID: 2,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueDouble(100.0)},
						{ID: 2, Value: wire.NewValueDouble(200.0)},
					}}),
				},
			}}),
			"Frame{TopLeft: Point{X: 1, Y: 2}, Size: Size{Width: 100, Height: 200}}",
		},
		{
			"User: optional field missing",
			&ts.User{Name: "Foo Bar"},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueString("Foo Bar")},
			}}),
			"User{Name: Foo Bar}",
		},
		{
			"User: optional field present",
			&ts.User{
				Name:    "Foo Bar",
				Contact: &ts.ContactInfo{EmailAddress: "*****@*****.**"},
			},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueString("Foo Bar")},
				{ID: 2, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
					{ID: 1, Value: wire.NewValueString("*****@*****.**")},
				}})},
			}}),
			"User{Name: Foo Bar, Contact: ContactInfo{EmailAddress: [email protected]}}",
		},
		{
			"List: self-referential struct",
			&ts.List{Value: 1, Tail: &ts.List{Value: 2}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueI32(1)},
				{
					ID: 2,
					Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
						{ID: 1, Value: wire.NewValueI32(2)},
					}}),
				},
			}}),
			"Node{Value: 1, Tail: Node{Value: 2}}",
		},
		{
			"Document: PDF",
			&tu.Document{Pdf: []byte{1, 2, 3}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueBinary([]byte{1, 2, 3})},
			}}),
			"Document{Pdf: [1 2 3]}",
		},
		{
			"Document: PlainText",
			&tu.Document{PlainText: stringp("hello")},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 2, Value: wire.NewValueString("hello")},
			}}),
			"Document{PlainText: hello}",
		},
		{
			"ArbitraryValue: bool",
			&tu.ArbitraryValue{BoolValue: boolp(true)},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 1, Value: wire.NewValueBool(true)},
			}}),
			"ArbitraryValue{BoolValue: true}",
		},
		{
			"ArbitraryValue: i64",
			&tu.ArbitraryValue{Int64Value: int64p(42)},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 2, Value: wire.NewValueI64(42)},
			}}),
			"ArbitraryValue{Int64Value: 42}",
		},
		{
			"ArbitraryValue: string",
			&tu.ArbitraryValue{StringValue: stringp("hello")},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 3, Value: wire.NewValueString("hello")},
			}}),
			"ArbitraryValue{StringValue: hello}",
		},
		{
			"ArbitraryValue: list",
			&tu.ArbitraryValue{ListValue: []*tu.ArbitraryValue{
				{BoolValue: boolp(true)},
				{Int64Value: int64p(42)},
				{StringValue: stringp("hello")},
			}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 4, Value: wire.NewValueList(
					wire.ValueListFromSlice(wire.TStruct, []wire.Value{
						wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 1, Value: wire.NewValueBool(true)},
						}}),
						wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 2, Value: wire.NewValueI64(42)},
						}}),
						wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
							{ID: 3, Value: wire.NewValueString("hello")},
						}}),
					}),
				)},
			}}),
			"ArbitraryValue{ListValue: [ArbitraryValue{BoolValue: true} ArbitraryValue{Int64Value: 42} ArbitraryValue{StringValue: hello}]}",
		},
		{
			"ArbitraryValue: map",
			&tu.ArbitraryValue{MapValue: map[string]*tu.ArbitraryValue{
				"bool":   {BoolValue: boolp(true)},
				"int64":  {Int64Value: int64p(42)},
				"string": {StringValue: stringp("hello")},
			}},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
				{ID: 5, Value: wire.NewValueMap(
					wire.MapItemListFromSlice(wire.TBinary, wire.TStruct, []wire.MapItem{
						{
							Key: wire.NewValueString("bool"),
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 1, Value: wire.NewValueBool(true)},
							}}),
						},
						{
							Key: wire.NewValueString("int64"),
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 2, Value: wire.NewValueI64(42)},
							}}),
						},
						{
							Key: wire.NewValueString("string"),
							Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
								{ID: 3, Value: wire.NewValueString("hello")},
							}}),
						},
					}),
				)},
			}}),
			"",
		},
		{
			"EmptyStruct",
			&ts.EmptyStruct{},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
			"",
		},
		{
			"EmptyUnion",
			&tu.EmptyUnion{},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
			"",
		},
		{
			"EmptyException",
			&tx.EmptyException{},
			wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}),
			"",
		},
	}

	for _, tt := range tests {
		assertRoundTrip(t, tt.x, tt.v, tt.desc)
		if tt.s != "" {
			assert.Equal(t, tt.s, tt.x.String(), "ToString: %v", tt.desc)
		} else {
			assert.NotPanics(t, func() { _ = tt.x.String() }, "ToString: %v", tt.desc)
		}
	}
}