Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28c1c19

Browse files
committed
THRIFT-3023 Go compiler is a little overly conservative with names of attributes
Client: Go Patch: Paul Magrath <[email protected]> This closes apache#389
1 parent c7cf379 commit 28c1c19

File tree

4 files changed

+79
-20
lines changed

4 files changed

+79
-20
lines changed

compiler/cpp/src/generate/t_go_generator.cc

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,7 @@ void t_go_generator::get_publicized_name_and_def_value(t_field* tfield,
10301030
t_const_value** OUT_def_value) const {
10311031
const string base_field_name = tfield->get_name();
10321032
const string escaped_field_name = escape_string(base_field_name);
1033-
const string go_safe_name = variable_name_to_go_name(escaped_field_name);
1034-
*OUT_pub_name = publicize(go_safe_name);
1033+
*OUT_pub_name = publicize(escaped_field_name);
10351034
*OUT_def_value = tfield->get_value();
10361035
}
10371036

@@ -1132,7 +1131,7 @@ void t_go_generator::generate_go_struct_definition(ofstream& out,
11321131
if (it != (*m_iter)->annotations_.end()) {
11331132
gotag = it->second;
11341133
}
1135-
indent(out) << publicize(variable_name_to_go_name((*m_iter)->get_name())) << " " << goType
1134+
indent(out) << publicize((*m_iter)->get_name()) << " " << goType
11361135
<< " `thrift:\"" << escape_string((*m_iter)->get_name()) << ","
11371136
<< sorted_keys_pos;
11381137

@@ -1230,7 +1229,7 @@ void t_go_generator::generate_isset_helpers(ofstream& out,
12301229

12311230
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
12321231
const string field_name(
1233-
publicize(variable_name_to_go_name(escape_string((*f_iter)->get_name()))));
1232+
publicize(escape_string((*f_iter)->get_name())));
12341233
if ((*f_iter)->get_req() == t_field::T_OPTIONAL || is_pointer_field(*f_iter)) {
12351234
out << indent() << "func (p *" << tstruct_name << ") IsSet" << field_name << "() bool {"
12361235
<< endl;
@@ -1281,7 +1280,7 @@ void t_go_generator::generate_countsetfields_helper(ofstream& out,
12811280
continue;
12821281

12831282
const string field_name(
1284-
publicize(variable_name_to_go_name(escape_string((*f_iter)->get_name()))));
1283+
publicize(escape_string((*f_iter)->get_name())));
12851284

12861285
out << indent() << "if (p.IsSet" << field_name << "()) {" << endl;
12871286
indent_up();
@@ -1319,7 +1318,7 @@ void t_go_generator::generate_go_struct_reader(ofstream& out,
13191318
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
13201319
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
13211320
const string field_name(
1322-
publicize(variable_name_to_go_name(escape_string((*f_iter)->get_name()))));
1321+
publicize(escape_string((*f_iter)->get_name())));
13231322
indent(out) << "var isset" << field_name << " bool = false;" << endl;
13241323
}
13251324
}
@@ -1375,7 +1374,7 @@ void t_go_generator::generate_go_struct_reader(ofstream& out,
13751374
// Mark required field as read
13761375
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
13771376
const string field_name(
1378-
publicize(variable_name_to_go_name(escape_string((*f_iter)->get_name()))));
1377+
publicize(escape_string((*f_iter)->get_name())));
13791378
out << indent() << "isset" << field_name << " = true" << endl;
13801379
}
13811380

@@ -1414,7 +1413,7 @@ void t_go_generator::generate_go_struct_reader(ofstream& out,
14141413
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
14151414
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
14161415
const string field_name(
1417-
publicize(variable_name_to_go_name(escape_string((*f_iter)->get_name()))));
1416+
publicize(escape_string((*f_iter)->get_name())));
14181417
out << indent() << "if !isset" << field_name << "{" << endl;
14191418
out << indent() << " return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, "
14201419
"fmt.Errorf(\"Required field " << field_name << " is not set\"));" << endl;
@@ -1516,7 +1515,7 @@ void t_go_generator::generate_go_struct_writer(ofstream& out,
15161515
indent_up();
15171516

15181517
if (field_required == t_field::T_OPTIONAL) {
1519-
out << indent() << "if p.IsSet" << publicize(variable_name_to_go_name(field_name)) << "() {"
1518+
out << indent() << "if p.IsSet" << publicize(field_name) << "() {"
15201519
<< endl;
15211520
indent_up();
15221521
}
@@ -1827,7 +1826,7 @@ void t_go_generator::generate_service_client(t_service* tservice) {
18271826
f_service_ << indent() << "args := " << argsname << "{" << endl;
18281827

18291828
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
1830-
f_service_ << indent() << publicize(variable_name_to_go_name((*fld_iter)->get_name()))
1829+
f_service_ << indent() << publicize((*fld_iter)->get_name())
18311830
<< " : " << variable_name_to_go_name((*fld_iter)->get_name()) << "," << endl;
18321831
}
18331832
f_service_ << indent() << "}" << endl;
@@ -1917,8 +1916,7 @@ void t_go_generator::generate_service_client(t_service* tservice) {
19171916
vector<t_field*>::const_iterator x_iter;
19181917

19191918
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
1920-
const std::string varname = variable_name_to_go_name((*x_iter)->get_name());
1921-
const std::string pubname = publicize(varname);
1919+
const std::string pubname = publicize((*x_iter)->get_name());
19221920

19231921
f_service_ << indent() << "if result." << pubname << " != nil {" << endl;
19241922
f_service_ << indent() << " err = result." << pubname << endl;
@@ -2561,7 +2559,7 @@ void t_go_generator::generate_process_function(t_service* tservice, t_function*
25612559
f_service_ << ", ";
25622560
}
25632561

2564-
f_service_ << "args." << publicize(variable_name_to_go_name((*f_iter)->get_name()));
2562+
f_service_ << "args." << publicize((*f_iter)->get_name());
25652563
}
25662564

25672565
f_service_ << "); err2 != nil {" << endl;
@@ -2577,7 +2575,7 @@ void t_go_generator::generate_process_function(t_service* tservice, t_function*
25772575
f_service_ << indent() << " case " << type_to_go_type(((*xf_iter)->get_type())) << ":"
25782576
<< endl;
25792577
f_service_ << indent() << "result."
2580-
<< publicize(variable_name_to_go_name((*xf_iter)->get_name())) << " = v" << endl;
2578+
<< publicize((*xf_iter)->get_name()) << " = v" << endl;
25812579
}
25822580

25832581
f_service_ << indent() << " default:" << endl;
@@ -2659,7 +2657,7 @@ void t_go_generator::generate_deserialize_field(ofstream& out,
26592657
(void)coerceData;
26602658
t_type* orig_type = tfield->get_type();
26612659
t_type* type = get_true_type(orig_type);
2662-
string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
2660+
string name(prefix + publicize(tfield->get_name()));
26632661

26642662
if (type->is_void()) {
26652663
throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + name;
@@ -2920,7 +2918,7 @@ void t_go_generator::generate_serialize_field(ofstream& out,
29202918
string prefix,
29212919
bool inkey) {
29222920
t_type* type = get_true_type(tfield->get_type());
2923-
string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
2921+
string name(prefix + publicize(tfield->get_name()));
29242922

29252923
// Do nothing for void types
29262924
if (type->is_void()) {
@@ -3156,7 +3154,7 @@ void t_go_generator::generate_go_docstring(ofstream& out,
31563154

31573155
for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
31583156
t_field* p = *p_iter;
3159-
ss << " - " << publicize(variable_name_to_go_name(p->get_name()));
3157+
ss << " - " << publicize(p->get_name());
31603158

31613159
if (p->has_doc()) {
31623160
ss << ": " << p->get_doc();

lib/go/test/Makefile.am

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ gopath: $(top_srcdir)/compiler/cpp/thrift $(THRIFTTEST) \
3131
GoTagTest.thrift \
3232
TypedefFieldTest.thrift \
3333
RefAnnotationFieldsTest.thrift \
34-
ErrorTest.thrift
34+
ErrorTest.thrift \
35+
NamesTest.thrift
3536
mkdir -p gopath/src
3637
grep -v list.*map.*list.*map $(THRIFTTEST) | grep -v 'set<Insanity>' > ThriftTest.thrift
3738
$(THRIFT) -r IncludesTest.thrift
@@ -44,6 +45,7 @@ gopath: $(top_srcdir)/compiler/cpp/thrift $(THRIFTTEST) \
4445
$(THRIFT) TypedefFieldTest.thrift
4546
$(THRIFT) RefAnnotationFieldsTest.thrift
4647
$(THRIFT) ErrorTest.thrift
48+
$(THRIFT) NamesTest.thrift
4749
GOPATH=`pwd`/gopath $(GO) get code.google.com/p/gomock/gomock
4850
ln -nfs ../../../thrift gopath/src/thrift
4951
ln -nfs ../../tests gopath/src/tests
@@ -56,7 +58,8 @@ check: gopath
5658
servicestest \
5759
typedeffieldtest \
5860
refannotationfieldstest \
59-
errortest
61+
errortest \
62+
namestest
6063
GOPATH=`pwd`/gopath $(GO) test thrift tests
6164

6265
clean-local:
@@ -77,4 +80,5 @@ EXTRA_DIST = \
7780
RefAnnotationFieldsTest.thrift \
7881
ServicesTest.thrift \
7982
TypedefFieldTest.thrift \
80-
ErrorTest.thrift
83+
ErrorTest.thrift \
84+
NamesTest.thrift

lib/go/test/NamesTest.thrift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
struct NamesTest {
21+
1: required string type
22+
}

lib/go/test/tests/names_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package tests
21+
22+
import (
23+
"namestest"
24+
"reflect"
25+
"testing"
26+
)
27+
28+
func TestThatAttributeNameSubstituionDoesNotOccur(t *testing.T) {
29+
s := namestest.NamesTest{}
30+
st := reflect.TypeOf(s)
31+
_, ok := st.FieldByName("Type")
32+
if !ok {
33+
t.Error("Type attribute is missing!")
34+
}
35+
}

0 commit comments

Comments
 (0)