Repository: thrift Updated Branches: refs/heads/master bd60b92c6 -> 962e41078
THRIFT-3006 Attach 'omitempty' json tag for optional fields in Go Client: Go Patch: Peter Woodman <[email protected]> This closes #380 Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/962e4107 Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/962e4107 Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/962e4107 Branch: refs/heads/master Commit: 962e41078bb92656d4e28acfb717884e1ef57f5e Parents: bd60b92 Author: Jens Geyer <[email protected]> Authored: Fri Feb 27 22:28:50 2015 +0100 Committer: Jens Geyer <[email protected]> Committed: Fri Feb 27 22:53:00 2015 +0100 ---------------------------------------------------------------------- compiler/cpp/src/generate/t_go_generator.cc | 7 ++++++- lib/go/test/GoTagTest.thrift | 3 ++- lib/go/test/tests/gotag_test.go | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/962e4107/compiler/cpp/src/generate/t_go_generator.cc ---------------------------------------------------------------------- diff --git a/compiler/cpp/src/generate/t_go_generator.cc b/compiler/cpp/src/generate/t_go_generator.cc index 2bae622..07712f9 100644 --- a/compiler/cpp/src/generate/t_go_generator.cc +++ b/compiler/cpp/src/generate/t_go_generator.cc @@ -1121,7 +1121,12 @@ void t_go_generator::generate_go_struct_definition(ofstream& out, t_type* fieldType = (*m_iter)->get_type(); string goType = type_to_go_type_with_opt(fieldType, is_pointer_field(*m_iter)); - string gotag("json:\"" + escape_string((*m_iter)->get_name()) + "\""); + string gotag; + if ((*m_iter)->get_req() == t_field::T_OPTIONAL) { + gotag = "json:\"" + escape_string((*m_iter)->get_name()) + ",omitempty\""; + } else { + gotag = "json:\"" + escape_string((*m_iter)->get_name()) + "\""; + } std::map<string, string>::iterator it = (*m_iter)->annotations_.find("go.tag"); if (it != (*m_iter)->annotations_.end()) { gotag = it->second; http://git-wip-us.apache.org/repos/asf/thrift/blob/962e4107/lib/go/test/GoTagTest.thrift ---------------------------------------------------------------------- diff --git a/lib/go/test/GoTagTest.thrift b/lib/go/test/GoTagTest.thrift index 539f3d2..508b3b6 100644 --- a/lib/go/test/GoTagTest.thrift +++ b/lib/go/test/GoTagTest.thrift @@ -19,5 +19,6 @@ struct tagged { 1: string string_thing, - 2: i64 int_thing (go.tag = "json:\"int_thing,string\"") + 2: i64 int_thing (go.tag = "json:\"int_thing,string\""), + 3: optional i64 optional_int_thing } http://git-wip-us.apache.org/repos/asf/thrift/blob/962e4107/lib/go/test/tests/gotag_test.go ---------------------------------------------------------------------- diff --git a/lib/go/test/tests/gotag_test.go b/lib/go/test/tests/gotag_test.go index ffa6e96..32f056f 100644 --- a/lib/go/test/tests/gotag_test.go +++ b/lib/go/test/tests/gotag_test.go @@ -42,3 +42,12 @@ func TestCustomTag(t *testing.T) { t.Error("Unexpected custom tag value") } } + +func TestOptionalTag(t *testing.T) { + s := gotagtest.Tagged{} + st := reflect.TypeOf(s) + field, ok := st.FieldByName("OptionalIntThing") + if !ok || field.Tag.Get("json") != "optional_int_thing,omitempty" { + t.Error("Unexpected default tag value for optional field") + } +}
