diff --git a/compiler/cpp/src/thrift/generate/t_go_generator.h b/compiler/cpp/src/thrift/generate/t_go_generator.h index f025e3db2f..5370c10764 100644 --- a/compiler/cpp/src/thrift/generate/t_go_generator.h +++ b/compiler/cpp/src/thrift/generate/t_go_generator.h @@ -295,7 +295,15 @@ class t_go_generator : public t_generator { return real_module; } - return lowercase(program->get_name()); + // Named after the IDL file, which may contain characters such as '-' that a Go package + // name cannot; '.' stays, because it separates the nested package directories. + real_module = lowercase(program->get_name()); + for (char& c : real_module) { + if (!std::isalnum(static_cast(c)) && c != '_' && c != '.') { + c = '_'; + } + } + return real_module; } static bool is_pointer_field(t_field* tfield, bool in_container = false); diff --git a/lib/go/test/Hyphenated-Name.thrift b/lib/go/test/Hyphenated-Name.thrift new file mode 100644 index 0000000000..e7e0dceabe --- /dev/null +++ b/lib/go/test/Hyphenated-Name.thrift @@ -0,0 +1,30 @@ +# +# 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. +# + +// No "namespace go": the generator names the package after the file, and a +// hyphen is not valid in a Go package name. + +struct HyphenatedStruct { + 1: i32 a + 2: list names +} + +service HyphenatedService { + HyphenatedStruct echo(1: HyphenatedStruct s, 2: map m) +} diff --git a/lib/go/test/Makefile.am b/lib/go/test/Makefile.am index 1f57eee173..76d49dc48c 100644 --- a/lib/go/test/Makefile.am +++ b/lib/go/test/Makefile.am @@ -76,7 +76,8 @@ gopath: $(THRIFT) $(THRIFTTEST) \ RemoteShadowAlias.thrift \ RemoteShadowClient.thrift \ RemoteShadowHost.thrift \ - RemoteShadowTest.thrift + RemoteShadowTest.thrift \ + Hyphenated-Name.thrift mkdir -p gopath/src build grep -v 'set' $(THRIFTTEST) > build/ThriftTest.thrift $(THRIFT) $(THRIFTARGS) $(RECURSIVE) @@ -120,6 +121,7 @@ gopath: $(THRIFT) $(THRIFTTEST) \ $(THRIFT) $(THRIFTARGS) StringParseAllocationTest.thrift $(THRIFT) $(THRIFTARGS) DocCommentTest.thrift $(THRIFT) $(THRIFTARGS) -r RemoteShadowTest.thrift + $(THRIFT) $(THRIFTARGS) Hyphenated-Name.thrift ln -nfs ../../tests gopath/src/tests cp -r ./dontexportrwtest gopath/src touch gopath @@ -156,7 +158,9 @@ check: gopath ./gopath/src/validatetest \ ./gopath/src/forwardtypetest \ ./gopath/src/remoteshadowtest \ - ./gopath/src/remoteshadowtest/shadow_service-remote + ./gopath/src/remoteshadowtest/shadow_service-remote \ + ./gopath/src/hyphenated_name \ + ./gopath/src/hyphenated_name/hyphenated_service-remote $(GO) test github.com/apache/thrift/lib/go/thrift $(GO) test ./gopath/src/tests ./gopath/src/dontexportrwtest @@ -196,6 +200,7 @@ EXTRA_DIST = \ EqualsTest.thrift \ ForwardType.thrift \ GoTagTest.thrift \ + Hyphenated-Name.thrift \ IgnoreInitialismsTest.thrift \ IncludesTest.thrift \ InitialismsTest.thrift \