Repository: thrift Updated Branches: refs/heads/master 329d59aab -> c0d126fca
THRIFT-2549 Generate json tag for struct members. use go.tag annotation to override the default generated tag. Client: Go Patch: Aleksey Pesternikov This closes #128 Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/c0d126fc Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/c0d126fc Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/c0d126fc Branch: refs/heads/master Commit: c0d126fcafe215b40a83c779751d97192f9d10b1 Parents: 329d59a Author: Jens Geyer <[email protected]> Authored: Thu Jun 19 22:49:54 2014 +0200 Committer: Jens Geyer <[email protected]> Committed: Thu Jun 19 22:49:54 2014 +0200 ---------------------------------------------------------------------- compiler/cpp/src/generate/t_go_generator.cc | 8 +++-- lib/go/test/GoTagTest.thrift | 23 +++++++++++++ lib/go/test/Makefile.am | 2 ++ lib/go/test/tests/gotag_test.go | 44 ++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/c0d126fc/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 19f502d..f9bd3d3 100644 --- a/compiler/cpp/src/generate/t_go_generator.cc +++ b/compiler/cpp/src/generate/t_go_generator.cc @@ -1159,7 +1159,11 @@ 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()) + "\""); + std::map<string, string>::iterator it = (*m_iter)->annotations_.find("go.tag"); + if (it != (*m_iter)->annotations_.end()) { + gotag = it->second; + } indent(out) << publicize(variable_name_to_go_name((*m_iter)->get_name())) << " " << goType << " `thrift:\"" << escape_string((*m_iter)->get_name()) @@ -1169,7 +1173,7 @@ void t_go_generator::generate_go_struct_definition(ofstream& out, out << ",required"; } - out << "\"`" << endl; + out << "\" " <<gotag <<"`" << endl; sorted_keys_pos ++; } } else { http://git-wip-us.apache.org/repos/asf/thrift/blob/c0d126fc/lib/go/test/GoTagTest.thrift ---------------------------------------------------------------------- diff --git a/lib/go/test/GoTagTest.thrift b/lib/go/test/GoTagTest.thrift new file mode 100644 index 0000000..d92c66b --- /dev/null +++ b/lib/go/test/GoTagTest.thrift @@ -0,0 +1,23 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +struct tagged { + 1: string string_thing, + 2: i64 int_thing (go.tag = "json:\"int_thing,string\"") +} http://git-wip-us.apache.org/repos/asf/thrift/blob/c0d126fc/lib/go/test/Makefile.am ---------------------------------------------------------------------- diff --git a/lib/go/test/Makefile.am b/lib/go/test/Makefile.am index 0be6cf7..43f17a1 100644 --- a/lib/go/test/Makefile.am +++ b/lib/go/test/Makefile.am @@ -28,6 +28,7 @@ gopath: $(top_srcdir)/compiler/cpp/thrift $(THRIFTTEST) \ OnewayTest.thrift \ OptionalFieldsTest.thrift \ ServicesTest.thrift \ + GoTagTest.thrift \ TypedefFieldTest.thrift \ RefAnnotationFieldsTest.thrift mkdir -p gopath/src @@ -38,6 +39,7 @@ gopath: $(top_srcdir)/compiler/cpp/thrift $(THRIFTTEST) \ $(THRIFT) OnewayTest.thrift $(THRIFT) OptionalFieldsTest.thrift $(THRIFT) ServicesTest.thrift + $(THRIFT) GoTagTest.thrift $(THRIFT) TypedefFieldTest.thrift $(THRIFT) RefAnnotationFieldsTest.thrift GOPATH=`pwd`/gopath $(GO) get code.google.com/p/gomock/gomock http://git-wip-us.apache.org/repos/asf/thrift/blob/c0d126fc/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 new file mode 100644 index 0000000..ffa6e96 --- /dev/null +++ b/lib/go/test/tests/gotag_test.go @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package tests + +import ( + "gotagtest" + "reflect" + "testing" +) + +func TestDefaultTag(t *testing.T) { + s := gotagtest.Tagged{} + st := reflect.TypeOf(s) + field, ok := st.FieldByName("StringThing") + if !ok || field.Tag.Get("json") != "string_thing" { + t.Error("Unexpected default tag value") + } +} + +func TestCustomTag(t *testing.T) { + s := gotagtest.Tagged{} + st := reflect.TypeOf(s) + field, ok := st.FieldByName("IntThing") + if !ok || field.Tag.Get("json") != "int_thing,string" { + t.Error("Unexpected custom tag value") + } +}
