-
-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[proto] Allow multiple outputs from a proto compiler
- Loading branch information
jacqueline.lee
committed
Aug 11, 2023
1 parent
98165a6
commit 416dca0
Showing
7 changed files
with
395 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* Copyright 2019 The Bazel Authors. All rights reserved. | ||
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 multi_suffix_compiler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bazelbuild/rules_go/tests/core/go_proto_library/enum" | ||
) | ||
|
||
func use(interface{}) {} | ||
|
||
func TestMultiSuffixCompiler(t *testing.T) { | ||
// just make sure types and generated functions exist | ||
v := enum.Enum_BYTES | ||
expected := "bytes_type" | ||
if v.String() != expected { | ||
panic(v.String()) | ||
} | ||
v = enum.Enum_INT32 | ||
expected = "INT32" | ||
if v.String() != expected { | ||
panic(v.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") | ||
load( | ||
"//proto:compiler.bzl", | ||
"go_proto_compiler", | ||
) | ||
load( | ||
"//proto/wkt:well_known_types.bzl", | ||
"GOGO_WELL_KNOWN_TYPE_REMAPS", | ||
) | ||
|
||
go_library( | ||
name = "dbenum", | ||
srcs = ["dbenums.go"], | ||
importpath = "github.com/bazelbuild/rules_go/tests/core/go_proto_library/compilers", | ||
visibility = ["//visibility:private"], | ||
deps = [ | ||
"@com_github_gogo_protobuf//proto", | ||
"@com_github_gogo_protobuf//protoc-gen-gogo/descriptor", | ||
"@com_github_gogo_protobuf//protoc-gen-gogo/generator", | ||
], | ||
) | ||
|
||
go_library( | ||
name = "protoc-gen-dbenum_lib", | ||
srcs = ["main.go"], | ||
importpath = "github.com/bazelbuild/rules_go/tests/core/go_proto_library/compilers", | ||
visibility = ["//visibility:private"], | ||
deps = [ | ||
"//tests/core/go_proto_library/compilers:dbenum", | ||
"@com_github_gogo_protobuf//protoc-gen-gogo/plugin", | ||
"@com_github_gogo_protobuf//vanity", | ||
"@com_github_gogo_protobuf//vanity/command", | ||
], | ||
) | ||
|
||
go_binary( | ||
name = "protoc-gen-dbenum-compiler", | ||
embed = [":protoc-gen-dbenum_lib"], | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
go_proto_compiler( | ||
name = "dbenum_compiler", | ||
options = GOGO_WELL_KNOWN_TYPE_REMAPS, | ||
plugin = "//tests/core/go_proto_library/compilers:protoc-gen-dbenum-compiler", | ||
suffixes = [ | ||
"_dbenum.pb.go", | ||
".pb.go", | ||
], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package dbenum | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor" | ||
pb "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" | ||
"github.com/gogo/protobuf/protoc-gen-gogo/generator" | ||
) | ||
|
||
func init() { | ||
generator.RegisterPlugin(NewGenerator()) | ||
} | ||
|
||
type Generator struct { | ||
*generator.Generator | ||
generator.PluginImports | ||
write bool | ||
} | ||
|
||
func NewGenerator() *Generator { | ||
return &Generator{} | ||
} | ||
|
||
func (g *Generator) Name() string { | ||
return "dbenum" | ||
} | ||
|
||
func (g *Generator) Init(gen *generator.Generator) { | ||
g.Generator = gen | ||
} | ||
|
||
func (g *Generator) GenerateImports(file *generator.FileDescriptor) { | ||
} | ||
|
||
func (g *Generator) Generate(file *generator.FileDescriptor) { | ||
for _, enum := range file.Enums() { | ||
g.enumHelper(enum) | ||
} | ||
g.writeTrailer(file.Enums()) | ||
} | ||
|
||
func (g *Generator) Write() bool { | ||
return g.write | ||
} | ||
|
||
const initTmpl = ` | ||
` | ||
|
||
func (g *Generator) writeTrailer(enums []*generator.EnumDescriptor) { | ||
type desc struct { | ||
PackageName string | ||
TypeName string | ||
LowerCaseTypeName string | ||
} | ||
if !g.write { | ||
return | ||
} | ||
tmpl := template.Must(template.New("db_enum_trailer").Parse(initTmpl)) | ||
g.P("func init() {") | ||
for _, e := range enums { | ||
if !HasDBEnum(e.Value) { | ||
continue | ||
} | ||
pkg := e.File().GetPackage() | ||
if pkg != "" { | ||
pkg += "." | ||
} | ||
tp := generator.CamelCaseSlice(e.TypeName()) | ||
var buf bytes.Buffer | ||
tmpl.Execute(&buf, desc{ | ||
PackageName: pkg + tp, | ||
TypeName: tp, | ||
LowerCaseTypeName: strings.ToLower(tp), | ||
}) | ||
g.P(buf.String()) | ||
} | ||
g.P("}") | ||
} | ||
|
||
func (g *Generator) enumHelper(enum *generator.EnumDescriptor) { | ||
type anEnum struct { | ||
PBName string | ||
DBName string | ||
} | ||
type typeDesc struct { | ||
TypeName string | ||
TypeNamespace string | ||
LowerCaseTypeName string | ||
Found map[int32]bool | ||
Names []anEnum | ||
AllNames []anEnum | ||
} | ||
tp := generator.CamelCaseSlice(enum.TypeName()) | ||
namespace := tp | ||
enumTypeName := enum.TypeName() | ||
if len(enumTypeName) > 1 { // This is a nested enum. | ||
names := enumTypeName[:len(enumTypeName)-1] | ||
// See https://protobuf.dev/reference/go/go-generated/#enum | ||
namespace = generator.CamelCaseSlice(names) | ||
} | ||
t := typeDesc{ | ||
TypeName: tp, | ||
TypeNamespace: namespace, | ||
LowerCaseTypeName: strings.ToLower(tp), | ||
Found: make(map[int32]bool), | ||
} | ||
for _, v := range enum.Value { | ||
enumValue := v.GetNumber() | ||
if validDbEnum, dbName := getDbEnum(v); validDbEnum { | ||
names := anEnum{PBName: v.GetName(), DBName: dbName} | ||
t.AllNames = append(t.AllNames, names) | ||
// Skip enums that are aliased where one value has already been processed. | ||
if t.Found[enumValue] { | ||
continue | ||
} | ||
t.Found[enumValue] = true | ||
t.Names = append(t.Names, names) | ||
} else { | ||
t.Found[enumValue] = true | ||
} | ||
} | ||
if len(t.AllNames) == 0 { | ||
return | ||
} | ||
g.write = true | ||
tmpl := template.Must(template.New("db_enum").Parse(tmpl)) | ||
var buf bytes.Buffer | ||
tmpl.Execute(&buf, t) | ||
g.P(buf.String()) | ||
} | ||
|
||
var E_DbEnum = &proto.ExtensionDesc{ | ||
ExtendedType: (*descriptor.EnumValueOptions)(nil), | ||
ExtensionType: (*string)(nil), | ||
Field: 5002, | ||
Name: "tests.core.go_proto_library.enum", | ||
Tag: "bytes,5002,opt,name=db_enum", | ||
} | ||
|
||
func getDbEnum(value *pb.EnumValueDescriptorProto) (bool, string) { | ||
if value == nil || value.Options == nil { | ||
return false, "" | ||
} | ||
EDbEnum := E_DbEnum | ||
v, err := proto.GetExtension(value.Options, EDbEnum) | ||
if err != nil { | ||
return false, "" | ||
} | ||
strPtr := v.(*string) | ||
if strPtr == nil { | ||
return false, "" | ||
} | ||
return true, *strPtr | ||
} | ||
|
||
// HasDBEnum returns if there is DBEnums extensions defined in given enums. | ||
func HasDBEnum(enums []*pb.EnumValueDescriptorProto) bool { | ||
for _, enum := range enums { | ||
if validDbEnum, _ := getDbEnum(enum); validDbEnum { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
const tmpl = ` | ||
var {{ .LowerCaseTypeName }}ToStringValue = ` + | ||
`map[{{ .TypeName }}]string { {{ range $names := .Names }} | ||
{{ $.TypeNamespace }}_{{ $names.PBName }}: ` + | ||
`"{{ $names.DBName }}",{{ end }} | ||
} | ||
// String implements the stringer interface and should produce the same output | ||
// that is inserted into the db. | ||
func (v {{ .TypeName }}) String() string { | ||
if val, ok := {{ .LowerCaseTypeName }}ToStringValue[v]; ok { | ||
return val | ||
} else if int(v) == 0 { | ||
return "null" | ||
} else { | ||
return proto.EnumName({{ .TypeName }}_name, int32(v)) | ||
} | ||
}` |
Oops, something went wrong.