本文整理汇总了Golang中go/uber/org/thriftrw/wire.NewValueMap函数的典型用法代码示例。如果您正苦于以下问题:Golang NewValueMap函数的具体用法?Golang NewValueMap怎么用?Golang NewValueMap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewValueMap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ToWire
func (v *MapOfBinaryAndString) ToWire() (wire.Value, error) {
var (
fields [2]wire.Field
i int = 0
w wire.Value
err error
)
if v.BinaryToString != nil {
w, err = wire.NewValueMap(_Map_Binary_String_MapItemList(v.BinaryToString)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 1, Value: w}
i++
}
if v.StringToBinary != nil {
w, err = wire.NewValueMap(_Map_String_Binary_MapItemList(v.StringToBinary)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 2, Value: w}
i++
}
return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil
}
示例2: ToWire
func (v *ThriftTest_TestMulti_Args) ToWire() (wire.Value, error) {
var (
fields [6]wire.Field
i int = 0
w wire.Value
err error
)
if v.Arg0 != nil {
w, err = wire.NewValueI8(*(v.Arg0)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 1, Value: w}
i++
}
if v.Arg1 != nil {
w, err = wire.NewValueI32(*(v.Arg1)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 2, Value: w}
i++
}
if v.Arg2 != nil {
w, err = wire.NewValueI64(*(v.Arg2)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 3, Value: w}
i++
}
if v.Arg3 != nil {
w, err = wire.NewValueMap(_Map_I16_String_MapItemList(v.Arg3)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 4, Value: w}
i++
}
if v.Arg4 != nil {
w, err = v.Arg4.ToWire()
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 5, Value: w}
i++
}
if v.Arg5 != nil {
w, err = v.Arg5.ToWire()
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 6, Value: w}
i++
}
return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil
}
示例3: ToWire
func (v *GenerateServiceRequest) ToWire() (wire.Value, error) {
var (
fields [3]wire.Field
i int = 0
w wire.Value
err error
)
if v.RootServices == nil {
return w, errors.New("field RootServices of GenerateServiceRequest is required")
}
w, err = wire.NewValueList(_List_ServiceID_ValueList(v.RootServices)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 1, Value: w}
i++
if v.Services == nil {
return w, errors.New("field Services of GenerateServiceRequest is required")
}
w, err = wire.NewValueMap(_Map_ServiceID_Service_MapItemList(v.Services)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 2, Value: w}
i++
if v.Modules == nil {
return w, errors.New("field Modules of GenerateServiceRequest is required")
}
w, err = wire.NewValueMap(_Map_ModuleID_Module_MapItemList(v.Modules)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 3, Value: w}
i++
return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil
}
示例4: ForEach
func (v _List_Map_I32_I32_ValueList) ForEach(f func(wire.Value) error) error {
for i, x := range v {
if x == nil {
return fmt.Errorf("invalid [%v]: value is nil", i)
}
w, err := wire.NewValueMap(_Map_I32_I32_MapItemList(x)), error(nil)
if err != nil {
return err
}
err = f(w)
if err != nil {
return err
}
}
return nil
}
示例5: ToWire
func (v *ThriftTest_TestMap_Args) ToWire() (wire.Value, error) {
var (
fields [1]wire.Field
i int = 0
w wire.Value
err error
)
if v.Thing != nil {
w, err = wire.NewValueMap(_Map_I32_I32_MapItemList(v.Thing)), error(nil)
if err != nil {
return w, err
}
fields[i] = wire.Field{ID: 1, Value: w}
i++
}
return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil
}
示例6: ForEach
func (m _Map_UserId_Map_Numberz_Insanity_MapItemList) ForEach(f func(wire.MapItem) error) error {
for k, v := range m {
if v == nil {
return fmt.Errorf("invalid [%v]: value is nil", k)
}
kw, err := k.ToWire()
if err != nil {
return err
}
vw, err := wire.NewValueMap(_Map_Numberz_Insanity_MapItemList(v)), error(nil)
if err != nil {
return err
}
err = f(wire.MapItem{Key: kw, Value: vw})
if err != nil {
return err
}
}
return nil
}
示例7: ForEach
func (m _Map_I32_Map_I32_I32_MapItemList) ForEach(f func(wire.MapItem) error) error {
for k, v := range m {
if v == nil {
return fmt.Errorf("invalid [%v]: value is nil", k)
}
kw, err := wire.NewValueI32(k), error(nil)
if err != nil {
return err
}
vw, err := wire.NewValueMap(_Map_I32_I32_MapItemList(v)), error(nil)
if err != nil {
return err
}
err = f(wire.MapItem{Key: kw, Value: vw})
if err != nil {
return err
}
}
return nil
}
示例8: TestUnhashableMapKeyAlias
func TestUnhashableMapKeyAlias(t *testing.T) {
tests := []struct {
x td.PointMap
v wire.Value
}{
{
td.PointMap{},
wire.NewValueMap(
wire.MapItemListFromSlice(wire.TStruct, wire.TStruct, []wire.MapItem{}),
),
},
{
td.PointMap{
{
Key: &ts.Point{X: 1, Y: 2},
Value: &ts.Point{X: 3, Y: 4},
},
{
Key: &ts.Point{X: 5, Y: 6},
Value: &ts.Point{X: 7, Y: 8},
},
{
Key: &ts.Point{X: 9, Y: 10},
Value: &ts.Point{X: 11, Y: 12},
},
},
wire.NewValueMap(
wire.MapItemListFromSlice(wire.TStruct, wire.TStruct, []wire.MapItem{
{
Key: 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.NewValueDouble(3)},
{ID: 2, Value: wire.NewValueDouble(4)},
}}),
},
{
Key: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 1, Value: wire.NewValueDouble(5)},
{ID: 2, Value: wire.NewValueDouble(6)},
}}),
Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 1, Value: wire.NewValueDouble(7)},
{ID: 2, Value: wire.NewValueDouble(8)},
}}),
},
{
Key: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 1, Value: wire.NewValueDouble(9)},
{ID: 2, Value: wire.NewValueDouble(10)},
}}),
Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{
{ID: 1, Value: wire.NewValueDouble(11)},
{ID: 2, Value: wire.NewValueDouble(12)},
}}),
},
}),
),
},
}
for _, tt := range tests {
assertRoundTrip(t, &tt.x, tt.v, "PointMap")
}
}
示例9: vmap
func vmap(kt, vt wire.Type, items ...wire.MapItem) wire.Value {
return wire.NewValueMap(wire.MapItemListFromSlice(kt, vt, items))
}
示例10: TestCollectionsOfPrimitives
//.........这里部分代码省略.........
// 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)
}
}
示例11: ReadValue
// ReadValue reads a value off the given type off the wire starting at the
// given offset.
//
// Returns the Value, the new offset, and an error if there was a decode error.
func (br *Reader) ReadValue(t wire.Type, off int64) (wire.Value, int64, error) {
switch t {
case wire.TBool:
b, off, err := br.readByte(off)
if err != nil {
return wire.Value{}, off, err
}
if b != 0 && b != 1 {
return wire.Value{}, off, decodeErrorf(
"invalid value %q for bool field", b,
)
}
return wire.NewValueBool(b == 1), off, nil
case wire.TI8:
b, off, err := br.readByte(off)
return wire.NewValueI8(int8(b)), off, err
case wire.TDouble:
value, off, err := br.readInt64(off)
d := math.Float64frombits(uint64(value))
return wire.NewValueDouble(d), off, err
case wire.TI16:
n, off, err := br.readInt16(off)
return wire.NewValueI16(n), off, err
case wire.TI32:
n, off, err := br.readInt32(off)
return wire.NewValueI32(n), off, err
case wire.TI64:
n, off, err := br.readInt64(off)
return wire.NewValueI64(n), off, err
case wire.TBinary:
v, off, err := br.readBytes(off)
return wire.NewValueBinary(v), off, err
case wire.TStruct:
s, off, err := br.readStruct(off)
return wire.NewValueStruct(s), off, err
case wire.TMap:
m, off, err := br.readMap(off)
return wire.NewValueMap(m), off, err
case wire.TSet:
s, off, err := br.readSet(off)
return wire.NewValueSet(s), off, err
case wire.TList:
l, off, err := br.readList(off)
return wire.NewValueList(l), off, err
default:
return wire.Value{}, off, decodeErrorf("unknown ttype %v", t)
}
}
示例12: TestStructRoundTripAndString
//.........这里部分代码省略.........
&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{
示例13: TestEnumContainers
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")
}
}
示例14: TestStructValidation
//.........这里部分代码省略.........
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"),
}),
),
},
{
示例15: TestCrazyTown
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{
//.........这里部分代码省略.........