THRIFT-3467 Go Maps for Thrift Sets Should Have Values of Type struct{}
Client: Go
Patch: artem antonenko <sam901@yandex.ru>
This closes #976
diff --git a/compiler/cpp/src/generate/t_go_generator.cc b/compiler/cpp/src/generate/t_go_generator.cc
index 3f9b858..6efff0e 100644
--- a/compiler/cpp/src/generate/t_go_generator.cc
+++ b/compiler/cpp/src/generate/t_go_generator.cc
@@ -1150,12 +1150,12 @@
} else if (type->is_set()) {
t_type* etype = ((t_set*)type)->get_elem_type();
const vector<t_const_value*>& val = value->get_list();
- out << "map[" << type_to_go_key_type(etype) << "]bool{" << endl;
+ out << "map[" << type_to_go_key_type(etype) << "]struct{}{" << endl;
indent_up();
vector<t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- out << indent() << render_const_value(etype, *v_iter, name) << ": true," << endl;
+ out << indent() << render_const_value(etype, *v_iter, name) << ": struct{}{}," << endl;
}
indent_down();
@@ -2969,7 +2969,7 @@
out << indent() << " return thrift.PrependError(\"error reading set begin: \", err)" << endl;
out << indent() << "}" << endl;
out << indent() << "tSet := make(map["
- << type_to_go_key_type(t->get_elem_type()->get_true_type()) << "]bool, size)" << endl;
+ << type_to_go_key_type(t->get_elem_type()->get_true_type()) << "]struct{}, size)" << endl;
out << indent() << prefix << eq << " " << (pointer_field ? "&" : "") << "tSet" << endl;
} else if (ttype->is_list()) {
out << indent() << "_, size, err := iprot.ReadListBegin()" << endl;
@@ -3048,7 +3048,7 @@
t_field felem(tset->get_elem_type(), elem);
felem.set_req(t_field::T_OPT_IN_REQ_OUT);
generate_deserialize_field(out, &felem, true, "", false, false, false, true, true);
- indent(out) << prefix << "[" << elem << "] = true" << endl;
+ indent(out) << prefix << "[" << elem << "] = struct{}{}" << endl;
}
/**
@@ -3611,7 +3611,7 @@
} else if (type->is_set()) {
t_set* t = (t_set*)type;
string elemType = type_to_go_key_type(t->get_elem_type());
- return maybe_pointer + string("map[") + elemType + string("]bool");
+ return maybe_pointer + string("map[") + elemType + string("]struct{}");
} else if (type->is_list()) {
t_list* t = (t_list*)type;
string elemType = type_to_go_type(t->get_elem_type());
diff --git a/lib/go/test/tests/client_error_test.go b/lib/go/test/tests/client_error_test.go
index 829a1dd..838883d 100644
--- a/lib/go/test/tests/client_error_test.go
+++ b/lib/go/test/tests/client_error_test.go
@@ -402,7 +402,7 @@
thing := errortest.NewTestStruct()
thing.M = make(map[string]string)
thing.L = make([]string, 0)
- thing.S = make(map[string]bool)
+ thing.S = make(map[string]struct{})
thing.I = 3
err := thrift.NewTTransportException(thrift.TIMED_OUT, "test")
@@ -434,7 +434,7 @@
thing := errortest.NewTestStruct()
thing.M = make(map[string]string)
thing.L = make([]string, 0)
- thing.S = make(map[string]bool)
+ thing.S = make(map[string]struct{})
thing.I = 3
err := thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, errors.New("test"))
diff --git a/lib/go/test/tests/thrifttest_driver.go b/lib/go/test/tests/thrifttest_driver.go
index 915383f..1e0cf86 100644
--- a/lib/go/test/tests/thrifttest_driver.go
+++ b/lib/go/test/tests/thrifttest_driver.go
@@ -162,7 +162,7 @@
t.Fatal("TestStringMap failed")
}
- setTestInput := map[int32]bool{1: true, 2: true, 3: true}
+ setTestInput := map[int32]struct{}{1: {}, 2: {}, 3: {}}
if r, err := client.TestSet(setTestInput); !reflect.DeepEqual(r, setTestInput) || err != nil {
t.Fatal("TestSet failed")
}
diff --git a/lib/go/test/tests/thrifttest_handler.go b/lib/go/test/tests/thrifttest_handler.go
index 50fe718..822a6c7 100644
--- a/lib/go/test/tests/thrifttest_handler.go
+++ b/lib/go/test/tests/thrifttest_handler.go
@@ -96,7 +96,7 @@
return thing, nil
}
-func (p *ThriftTestHandler) TestSet(thing map[int32]bool) (r map[int32]bool, err error) {
+func (p *ThriftTestHandler) TestSet(thing map[int32]struct{}) (r map[int32]struct{}, err error) {
return thing, nil
}
diff --git a/lib/go/thrift/serializer_test.go b/lib/go/thrift/serializer_test.go
index 3cdb387..06d27a1 100644
--- a/lib/go/thrift/serializer_test.go
+++ b/lib/go/thrift/serializer_test.go
@@ -85,7 +85,7 @@
m.Bin = make([]byte, 10)
m.StringMap = make(map[string]string, 5)
m.StringList = make([]string, 5)
- m.StringSet = make(map[string]bool, 5)
+ m.StringSet = make(map[string]struct{}, 5)
m.E = 2
s, err := t.WriteString(&m)
@@ -119,7 +119,7 @@
m.Bin = make([]byte, 10)
m.StringMap = make(map[string]string, 5)
m.StringList = make([]string, 5)
- m.StringSet = make(map[string]bool, 5)
+ m.StringSet = make(map[string]struct{}, 5)
m.E = 2
s, err := t.WriteString(&m)
diff --git a/lib/go/thrift/serializer_types_test.go b/lib/go/thrift/serializer_types_test.go
index 10f4737..38ab8d6 100644
--- a/lib/go/thrift/serializer_types_test.go
+++ b/lib/go/thrift/serializer_types_test.go
@@ -97,18 +97,18 @@
func MyTestEnumPtr(v MyTestEnum) *MyTestEnum { return &v }
type MyTestStruct struct {
- On bool `thrift:"on,1" json:"on"`
- B int8 `thrift:"b,2" json:"b"`
- Int16 int16 `thrift:"int16,3" json:"int16"`
- Int32 int32 `thrift:"int32,4" json:"int32"`
- Int64 int64 `thrift:"int64,5" json:"int64"`
- D float64 `thrift:"d,6" json:"d"`
- St string `thrift:"st,7" json:"st"`
- Bin []byte `thrift:"bin,8" json:"bin"`
- StringMap map[string]string `thrift:"stringMap,9" json:"stringMap"`
- StringList []string `thrift:"stringList,10" json:"stringList"`
- StringSet map[string]bool `thrift:"stringSet,11" json:"stringSet"`
- E MyTestEnum `thrift:"e,12" json:"e"`
+ On bool `thrift:"on,1" json:"on"`
+ B int8 `thrift:"b,2" json:"b"`
+ Int16 int16 `thrift:"int16,3" json:"int16"`
+ Int32 int32 `thrift:"int32,4" json:"int32"`
+ Int64 int64 `thrift:"int64,5" json:"int64"`
+ D float64 `thrift:"d,6" json:"d"`
+ St string `thrift:"st,7" json:"st"`
+ Bin []byte `thrift:"bin,8" json:"bin"`
+ StringMap map[string]string `thrift:"stringMap,9" json:"stringMap"`
+ StringList []string `thrift:"stringList,10" json:"stringList"`
+ StringSet map[string]struct{} `thrift:"stringSet,11" json:"stringSet"`
+ E MyTestEnum `thrift:"e,12" json:"e"`
}
func NewMyTestStruct() *MyTestStruct {
@@ -155,7 +155,7 @@
return p.StringList
}
-func (p *MyTestStruct) GetStringSet() map[string]bool {
+func (p *MyTestStruct) GetStringSet() map[string]struct{} {
return p.StringSet
}
@@ -366,7 +366,7 @@
if err != nil {
return PrependError("error reading set begin: ", err)
}
- tSet := make(map[string]bool, size)
+ tSet := make(map[string]struct{}, size)
p.StringSet = tSet
for i := 0; i < size; i++ {
var _elem3 string
@@ -375,7 +375,7 @@
} else {
_elem3 = v
}
- p.StringSet[_elem3] = true
+ p.StringSet[_elem3] = struct{}{}
}
if err := iprot.ReadSetEnd(); err != nil {
return PrependError("error reading set end: ", err)
diff --git a/test/go/src/bin/stress/main.go b/test/go/src/bin/stress/main.go
index 4fc88e0..1f713bb 100644
--- a/test/go/src/bin/stress/main.go
+++ b/test/go/src/bin/stress/main.go
@@ -201,14 +201,13 @@
atomic.AddInt64(&clicounter, 1)
}
case echoSet:
- s := map[int8]bool{-10: true, -9: true, -8: true, -7: true, -6: true, -5: true, -4: true, -3: true, -2: true, -1: true, 0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true}
+ s := map[int8]struct{}{-10: {}, -9: {}, -8: {}, -7: {}, -6: {}, -5: {}, -4: {}, -3: {}, -2: {}, -1: {}, 0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}}
for i := 0; i < *loop; i++ {
client.EchoSet(s)
atomic.AddInt64(&clicounter, 1)
}
case echoMap:
- m := map[int8]int8{
- -10: 10, -9: 9, -8: 8, -7: 7, -6: 6, -5: 5, -4: 4, -3: 3, -2: 2, -1: 1, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}
+ m := map[int8]int8{-10: 10, -9: 9, -8: 8, -7: 7, -6: 6, -5: 5, -4: 4, -3: 3, -2: 2, -1: 1, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}
for i := 0; i < *loop; i++ {
client.EchoMap(m)
atomic.AddInt64(&clicounter, 1)
@@ -244,7 +243,7 @@
atomic.AddInt64(&counter, 1)
return arg, nil
}
-func (h *handler) EchoSet(arg map[int8]bool) (r map[int8]bool, err error) {
+func (h *handler) EchoSet(arg map[int8]struct{}) (r map[int8]struct{}, err error) {
atomic.AddInt64(&counter, 1)
return arg, nil
}
diff --git a/test/go/src/bin/testclient/main.go b/test/go/src/bin/testclient/main.go
index 7a7af72..f4a19dd 100644
--- a/test/go/src/bin/testclient/main.go
+++ b/test/go/src/bin/testclient/main.go
@@ -174,7 +174,7 @@
t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
}
- s := map[int32]bool{1: true, 2: true, 42: true}
+ s := map[int32]struct{}{1: struct{}{}, 2: struct{}{}, 42: struct{}{}}
sret, err := client.TestSet(s)
if err != nil {
t.Fatalf("Unexpected error in TestSet() call: ", err)
diff --git a/test/go/src/common/clientserver_test.go b/test/go/src/common/clientserver_test.go
index 3b81423..2a9d5e9 100644
--- a/test/go/src/common/clientserver_test.go
+++ b/test/go/src/common/clientserver_test.go
@@ -105,7 +105,7 @@
handler.EXPECT().TestNest(&thrifttest.Xtruct2{StructThing: &thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}}).Return(&thrifttest.Xtruct2{StructThing: &thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}}, nil),
handler.EXPECT().TestMap(map[int32]int32{1: 2, 3: 4, 5: 42}).Return(map[int32]int32{1: 2, 3: 4, 5: 42}, nil),
handler.EXPECT().TestStringMap(map[string]string{"a": "2", "b": "blah", "some": "thing"}).Return(map[string]string{"a": "2", "b": "blah", "some": "thing"}, nil),
- handler.EXPECT().TestSet(map[int32]bool{1: true, 2: true, 42: true}).Return(map[int32]bool{1: true, 2: true, 42: true}, nil),
+ handler.EXPECT().TestSet(map[int32]struct{}{1: struct{}{}, 2: struct{}{}, 42: struct{}{}}).Return(map[int32]struct{}{1: struct{}{}, 2: struct{}{}, 42: struct{}{}}, nil),
handler.EXPECT().TestList([]int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil),
handler.EXPECT().TestEnum(thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil),
handler.EXPECT().TestTypedef(thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil),
@@ -222,7 +222,7 @@
t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret)
}
- s := map[int32]bool{1: true, 2: true, 42: true}
+ s := map[int32]struct{}{1: struct{}{}, 2: struct{}{}, 42: struct{}{}}
sret, err := client.TestSet(s)
if err != nil {
t.Errorf("Unexpected error in TestSet() call: ", err)
diff --git a/test/go/src/common/mock_handler.go b/test/go/src/common/mock_handler.go
index 7495fc6..6ae8130 100644
--- a/test/go/src/common/mock_handler.go
+++ b/test/go/src/common/mock_handler.go
@@ -223,9 +223,9 @@
return _mr.mock.ctrl.RecordCall(_mr.mock, "TestOneway", arg0)
}
-func (_m *MockThriftTest) TestSet(_param0 map[int32]bool) (map[int32]bool, error) {
+func (_m *MockThriftTest) TestSet(_param0 map[int32]struct{}) (map[int32]struct{}, error) {
ret := _m.ctrl.Call(_m, "TestSet", _param0)
- ret0, _ := ret[0].(map[int32]bool)
+ ret0, _ := ret[0].(map[int32]struct{})
ret1, _ := ret[1].(error)
return ret0, ret1
}
diff --git a/test/go/src/common/printing_handler.go b/test/go/src/common/printing_handler.go
index 5fe3d53..afee8da 100644
--- a/test/go/src/common/printing_handler.go
+++ b/test/go/src/common/printing_handler.go
@@ -188,7 +188,7 @@
//
// Parameters:
// - Thing
-func (p *printingHandler) TestSet(thing map[int32]bool) (r map[int32]bool, err error) {
+func (p *printingHandler) TestSet(thing map[int32]struct{}) (r map[int32]struct{}, err error) {
fmt.Printf("testSet({")
first := true
for k, _ := range thing {
diff --git a/test/go/src/common/simple_handler.go b/test/go/src/common/simple_handler.go
index 944f11c..7bd3a30 100644
--- a/test/go/src/common/simple_handler.go
+++ b/test/go/src/common/simple_handler.go
@@ -77,7 +77,7 @@
return thing, nil
}
-func (p *simpleHandler) TestSet(thing map[int32]bool) (r map[int32]bool, err error) {
+func (p *simpleHandler) TestSet(thing map[int32]struct{}) (r map[int32]struct{}, err error) {
return thing, nil
}