Skip to content

Commit

Permalink
cc: Convert base file name to screaming snake case (#272)
Browse files Browse the repository at this point in the history
* cc: Convert base file name to screaming snake case

This patch converts the base file name into screaming snake case instead
of upper for cc macro generation.

This is motivated by:

INFO: From Compiling external/com_github_openzipkin_zipkinapi/zipkin-jsonv2.pb.validate.cc:
In file included from bazel-out/k8-opt/bin/external/com_github_openzipkin_zipkinapi/zipkin-jsonv2.pb.validate.cc:5:
bazel-out/k8-opt/bin/external/com_github_openzipkin_zipkinapi/zipkin-jsonv2.pb.validate.h:37:31: warning: ISO C99 requires whitespace after the macro name [-Wc99-extensions]
                              ^
1 warning generated.

As reported in
envoyproxy/envoy#6985 (comment).

Signed-off-by: Dhi Aurrahman <dio@tetrate.io>

* Simple basename replacement

Signed-off-by: Dhi Aurrahman <dio@tetrate.io>

* Remove noise

Signed-off-by: Dhi Aurrahman <dio@tetrate.io>

* Remove upper, since it is not being used

Signed-off-by: Dhi Aurrahman <dio@tetrate.io>
  • Loading branch information
dio authored and akonradi committed Sep 4, 2019
1 parent b2e4ad3 commit fd7de02
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bazel/protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _protoc_python_output_files(proto_file_sources):
for p in proto_file_sources:
basename = p.basename[:-len(".proto")]

python_srcs.append(basename + "_pb2.py")
python_srcs.append(basename.replace("-", "_", maxsplit = None) + "_pb2.py")
return python_srcs

def _protoc_gen_validate_python_impl(ctx):
Expand Down
1 change: 1 addition & 0 deletions templates/cc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//templates/shared:go_default_library",
"//vendor/github.com/iancoleman/strcase:go_default_library",
"//vendor/github.com/lyft/protoc-gen-star:go_default_library",
"//vendor/github.com/lyft/protoc-gen-star/lang/go:go_default_library",
"@com_github_golang_protobuf//ptypes:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion templates/cc/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ using std::string;
} // namespace
{{ end }}
#define X_{{ .Package.ProtoName.ScreamingSnakeCase }}_{{ .File.InputPath.BaseName | upper }}(X) \
#define X_{{ .Package.ProtoName.ScreamingSnakeCase }}_{{ .File.InputPath.BaseName | screaming_snake_case }}(X) \
{{ range .AllMessages -}}
X({{class . }}) \
{{ end }}
Expand Down
7 changes: 4 additions & 3 deletions templates/cc/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/duration"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/iancoleman/strcase"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
)
Expand Down Expand Up @@ -95,9 +96,9 @@ func RegisterHeader(tpl *template.Template, params pgs.Parameters) {
fns := CCFuncs{pgsgo.InitContext(params)}

tpl.Funcs(map[string]interface{}{
"class": fns.className,
"output": fns.output,
"upper": strings.ToUpper,
"class": fns.className,
"output": fns.output,
"screaming_snake_case": strcase.ToScreamingSnake,
})

template.Must(tpl.Parse(headerFileTpl))
Expand Down
1 change: 1 addition & 0 deletions tests/harness/cases/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ proto_library(
"bool.proto",
"bytes.proto",
"enums.proto",
"filename-with-dash.proto",
"kitchen_sink.proto",
"maps.proto",
"messages.proto",
Expand Down
6 changes: 6 additions & 0 deletions tests/harness/cases/filename-with-dash.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

package tests.harness.cases;
option go_package = "cases";

import "validate/validate.proto";
2 changes: 2 additions & 0 deletions tests/harness/cc/harness.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "tests/harness/cases/bytes.pb.validate.h"
#include "tests/harness/cases/enums.pb.h"
#include "tests/harness/cases/enums.pb.validate.h"
#include "tests/harness/cases/filename-with-dash.pb.h"
#include "tests/harness/cases/filename-with-dash.pb.validate.h"
#include "tests/harness/cases/maps.pb.h"
#include "tests/harness/cases/maps.pb.validate.h"
#include "tests/harness/cases/messages.pb.h"
Expand Down
2 changes: 2 additions & 0 deletions tests/harness/python/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from tests.harness.cases.bool_pb2 import *
from tests.harness.cases.bytes_pb2 import *
from tests.harness.cases.enums_pb2 import *
from tests.harness.cases.enums_pb2 import *
from tests.harness.cases.filename_with_dash_pb2 import *
from tests.harness.cases.messages_pb2 import *
from tests.harness.cases.numbers_pb2 import *
from tests.harness.cases.oneofs_pb2 import *
Expand Down

0 comments on commit fd7de02

Please sign in to comment.