From 72cec547cdae32f2b7f8dd64e83576dea3146f17 Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Mon, 24 Oct 2016 10:01:39 -0400 Subject: [PATCH 1/2] Convert - to _ for protobuf package names Convert - to _ for protobuf package names to allow protobuf code generation support for go packages that have - in their names. --- .../go2idl/go-to-protobuf/protobuf/cmd.go | 3 -- .../go2idl/go-to-protobuf/protobuf/namer.go | 3 +- .../go-to-protobuf/protobuf/namer_test.go | 50 +++++++++++++++++++ test/test_owners.csv | 1 + 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 cmd/libs/go2idl/go-to-protobuf/protobuf/namer_test.go diff --git a/cmd/libs/go2idl/go-to-protobuf/protobuf/cmd.go b/cmd/libs/go2idl/go-to-protobuf/protobuf/cmd.go index daac979c1e6..aae64d2bdd8 100644 --- a/cmd/libs/go2idl/go-to-protobuf/protobuf/cmd.go +++ b/cmd/libs/go2idl/go-to-protobuf/protobuf/cmd.go @@ -138,9 +138,6 @@ func Run(g *Generator) { d = d[1:] outputPackage = false } - if strings.Contains(d, "-") { - log.Fatalf("Package names must be valid protobuf package identifiers, which allow only [a-z0-9_]: %s", d) - } name := protoSafePackage(d) parts := strings.SplitN(d, "=", 2) if len(parts) > 1 { diff --git a/cmd/libs/go2idl/go-to-protobuf/protobuf/namer.go b/cmd/libs/go2idl/go-to-protobuf/protobuf/namer.go index 7ae5c614e39..60060d4edf4 100644 --- a/cmd/libs/go2idl/go-to-protobuf/protobuf/namer.go +++ b/cmd/libs/go2idl/go-to-protobuf/protobuf/namer.go @@ -94,7 +94,8 @@ func (n *protobufNamer) GoNameToProtoName(name types.Name) types.Name { } func protoSafePackage(name string) string { - return strings.Replace(name, "/", ".", -1) + pkg := strings.Replace(name, "/", ".", -1) + return strings.Replace(pkg, "-", "_", -1) } type typeNameSet map[types.Name]*protobufPackage diff --git a/cmd/libs/go2idl/go-to-protobuf/protobuf/namer_test.go b/cmd/libs/go2idl/go-to-protobuf/protobuf/namer_test.go new file mode 100644 index 00000000000..0ee71f80beb --- /dev/null +++ b/cmd/libs/go2idl/go-to-protobuf/protobuf/namer_test.go @@ -0,0 +1,50 @@ +/* +Copyright 2016 The Kubernetes Authors. + +Licensed 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 protobuf + +import "testing" + +func TestProtoSafePackage(t *testing.T) { + tests := []struct { + pkg string + expected string + }{ + { + pkg: "foo", + expected: "foo", + }, + { + pkg: "foo/bar", + expected: "foo.bar", + }, + { + pkg: "foo/bar/baz", + expected: "foo.bar.baz", + }, + { + pkg: "foo/bar-baz/x/y-z/q", + expected: "foo.bar_baz.x.y_z.q", + }, + } + + for _, test := range tests { + actual := protoSafePackage(test.pkg) + if e, a := test.expected, actual; e != a { + t.Errorf("%s: expected %s, got %s", test.pkg, e, a) + } + } +} diff --git a/test/test_owners.csv b/test/test_owners.csv index 629b0810ed6..a382790252e 100644 --- a/test/test_owners.csv +++ b/test/test_owners.csv @@ -452,6 +452,7 @@ k8s.io/kubernetes/cmd/kubeadm/app/images,davidopp,1 k8s.io/kubernetes/cmd/kubeadm/app/util,krousey,1 k8s.io/kubernetes/cmd/kubelet/app,hurf,1 k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset/typed/testgroup.k8s.io/unversioned,eparis,1 +k8s.io/kubernetes/cmd/libs/go2idl/go-to-protobuf/protobuf,smarterclayton,0 k8s.io/kubernetes/cmd/libs/go2idl/openapi-gen/generators,davidopp,1 k8s.io/kubernetes/cmd/mungedocs,mwielgus,1 k8s.io/kubernetes/examples,Random-Liu,0 From e7068751ed4ffe40719783a8722c72d8776c94cf Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Fri, 28 Oct 2016 11:08:27 -0400 Subject: [PATCH 2/2] Update bazel --- cmd/libs/go2idl/go-to-protobuf/protobuf/BUILD | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/libs/go2idl/go-to-protobuf/protobuf/BUILD b/cmd/libs/go2idl/go-to-protobuf/protobuf/BUILD index 00d55aea503..02348af9050 100644 --- a/cmd/libs/go2idl/go-to-protobuf/protobuf/BUILD +++ b/cmd/libs/go2idl/go-to-protobuf/protobuf/BUILD @@ -33,3 +33,11 @@ go_library( "//vendor:k8s.io/gengo/types", ], ) + +go_test( + name = "go_default_test", + srcs = ["namer_test.go"], + library = "go_default_library", + tags = ["automanaged"], + deps = [], +)