Skip to content

Commit 6409167

Browse files
committed
Name changes & enhancements for custom plugin
1 parent 7d4c46b commit 6409167

11 files changed

Lines changed: 136 additions & 69 deletions

File tree

protobuf-grpc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ gen:
2020
@make -s -C ./proto gen
2121

2222
gen-custom:
23-
@make -s -C ./protoc-gen-list-messages gen
23+
@make -s -C ./proto gen-custom
2424

2525
# Wrappers for running clients & servers of each language
2626
server-go:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
#################################################
5+
# Code generated by protoc-gen-bash. DO NOT EDIT.
6+
#
7+
# source file: echo/v1/echo.proto
8+
#################################################
9+
10+
printf 'The messages defined in the provided proto file(s) are:\n'
11+
12+
printf '
13+
echo.v1.EchoRequest (fields: msg:string)
14+
echo.v1.EchoResponse (fields: msg:string)
15+
'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
#################################################
5+
# Code generated by protoc-gen-bash. DO NOT EDIT.
6+
#
7+
# source file: employees/v1/employees.proto
8+
#################################################
9+
10+
printf 'The messages defined in the provided proto file(s) are:\n'
11+
12+
printf '
13+
employees.v1.GetEmployeeRequest (fields: short_name:string)
14+
employees.v1.GetEmployeeResponse (fields: employee:message)
15+
employees.v1.GetEmployeeResponse.Employee (fields: id:int64, full_name:string, birthday:string)
16+
employees.v1.ListEmployeesRequest (fields: None)
17+
employees.v1.ListEmployeesResponse (fields: short_names:repeated_string)
18+
'

protobuf-grpc/proto/Makefile

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ pyvenv = source ../venv/bin/activate
2020

2121
get-proto-deps:
2222
@mkdir -p /tmp/proto-deps
23-
@for dep in $(proto_deps_sources) ; do git clone --depth=1 https://"$${dep}" $(proto_deps_rootdir)/"$${dep}" || true ; done
23+
@for dep in $(proto_deps_sources) ; do \
24+
if [[ ! -d $(proto_deps_rootdir)/"$${dep}" ]] ; then \
25+
git clone --depth=1 https://"$${dep}" $(proto_deps_rootdir)/"$${dep}" || true ; \
26+
fi ; \
27+
done
2428

2529
ensure-outdir:
2630
@mkdir -p $(outdir)
@@ -30,7 +34,7 @@ ensure-outdir:
3034
#
3135
# Most lang/output code can be generated directly by a single call to protoc
3236
# with available plugins ...
33-
gen: ensure-outdir
37+
gen: get-proto-deps ensure-outdir
3438
@printf 'Generating protobuf code...\n'
3539
@$(pyvenv) && \
3640
protoc \
@@ -72,3 +76,17 @@ gen: ensure-outdir
7276
# our tree set up that way
7377
@printf 'Fixing Python import statements...\n'
7478
@find $(outdir) -type f -name '*.py' | xargs -I{} sed -E -i 's/^from .*\.v[0-9]+ import (.*)/from . import \1/g' {}
79+
80+
# These targets show that you can use a custom-built protoc plugin to generate
81+
# code from proto defs however you want!
82+
build:
83+
@make -s -C ../protoc-gen-bash build
84+
85+
gen-custom: get-proto-deps ensure-outdir build
86+
@printf 'Generating code using custom protoc plugin...\n'
87+
@protoc \
88+
--plugin=../protoc-gen-bash/build/protoc-gen-bash \
89+
$(proto_deps_import_flags) \
90+
--bash_out=$(outdir) \
91+
--bash_opt=paths=source_relative \
92+
$(proto_files)

protobuf-grpc/proto/employees/v1/employees.proto

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ service EmployeesService {
3333
}
3434
}
3535

36-
// Defines the record structure for each employee
37-
message Employee {
38-
// Employee ID number
39-
int64 id = 1;
40-
// Employee full name
41-
string full_name = 2;
42-
// Employee birthday, in RFC 3339 date format
43-
string birthday = 3;
44-
}
45-
4636
// For calling GetEmployee
4737
message GetEmployeeRequest {
4838
string short_name = 1;
4939
}
5040

5141
// Response from the GetEmployee call
5242
message GetEmployeeResponse {
43+
// Defines the record structure for each employee
44+
message Employee {
45+
// Employee ID number
46+
int64 id = 1;
47+
// Employee full name
48+
string full_name = 2;
49+
// Employee birthday, in RFC 3339 date format
50+
string birthday = 3;
51+
}
52+
5353
// Contains Employee record details
5454
Employee employee = 1;
5555
}
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.PHONY: build
2+
build:
3+
@go build -o ./build/ .
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
6+
"google.golang.org/protobuf/compiler/protogen"
7+
)
8+
9+
func main() {
10+
opts := protogen.Options{}
11+
opts.Run(func(gen *protogen.Plugin) error {
12+
for _, f := range gen.Files {
13+
if f.Generate {
14+
generateFile(gen, f)
15+
}
16+
}
17+
return nil
18+
})
19+
}
20+
21+
func generateFile(gen *protogen.Plugin, file *protogen.File) {
22+
filename := file.GeneratedFilenamePrefix + "_bash.pb.sh"
23+
g := gen.NewGeneratedFile(filename, file.GoImportPath)
24+
g.P("#!/usr/bin/env bash")
25+
g.P("set -euo pipefail")
26+
g.P()
27+
g.P("#################################################")
28+
g.P("# Code generated by protoc-gen-bash. DO NOT EDIT.")
29+
g.P("#")
30+
g.P("# source file: ", *file.Proto.Name)
31+
g.P("#################################################")
32+
g.P()
33+
g.P(`printf 'The messages defined in the provided proto file(s) are:\n'`)
34+
g.P()
35+
g.P(`printf '`)
36+
printMessages(g, file.Messages)
37+
g.P(`'`)
38+
}
39+
40+
// printMessages traverses nested Messages recursively and prints their
41+
// FullNames along with their Field information
42+
func printMessages(g *protogen.GeneratedFile, msgs []*protogen.Message) {
43+
for _, msg := range msgs {
44+
var fields []string
45+
for _, field := range msg.Fields {
46+
fieldName := string(field.Desc.Name())
47+
48+
// Cardinality tells us e.g. if a field is repeated
49+
fieldCardinality := field.Desc.Cardinality().String()
50+
if fieldCardinality != "repeated" {
51+
fieldCardinality = ""
52+
} else {
53+
fieldCardinality += "_"
54+
}
55+
56+
fieldType := field.Desc.Kind().String()
57+
fields = append(fields, fieldName+":"+fieldCardinality+fieldType)
58+
}
59+
// In case a message has no fields, like ListEmployeesRequest
60+
if len(fields) == 0 {
61+
fields = []string{"None"}
62+
}
63+
64+
g.P(` `, msg.Desc.FullName(), " (fields: ", strings.Join(fields, ", "), ")")
65+
if len(msg.Messages) > 0 {
66+
printMessages(g, msg.Messages)
67+
}
68+
}
69+
}

protobuf-grpc/protoc-gen-list-messages/Makefile

Lines changed: 0 additions & 14 deletions
This file was deleted.

protobuf-grpc/protoc-gen-list-messages/main.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)