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
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 @@
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 @@
out << ",required";
}
- out << "\"`" << endl;
+ out << "\" " <<gotag <<"`" << endl;
sorted_keys_pos ++;
}
} else {
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\"")
+}
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 @@
OnewayTest.thrift \
OptionalFieldsTest.thrift \
ServicesTest.thrift \
+ GoTagTest.thrift \
TypedefFieldTest.thrift \
RefAnnotationFieldsTest.thrift
mkdir -p gopath/src
@@ -38,6 +39,7 @@
$(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
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")
+ }
+}