Skip to content

Commit 2233007

Browse files
authored
Fix Duplicate Declarations (#17)
* only declare used messages and enums * update pascalCase * refactor * prevent redefining internal messages * minor * update sample * move the Location * fix enum generation * update sample * refactor * upgrade dependencies * bump version * remove protoc version * update samples * update actions * upgrade actions/checkout * bug fix
1 parent a6e79ed commit 2233007

File tree

11 files changed

+194
-162
lines changed

11 files changed

+194
-162
lines changed

.github/workflows/go.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ name: Go
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
test:
1111
name: Test
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v2
15-
- uses: actions/setup-go@v2
16-
with:
17-
go-version: '^1.17'
18-
- uses: arduino/setup-protoc@v1
19-
with:
20-
version: '3.19.1'
21-
- name: Run go test
22-
run: go test -v ./...
23-
- name: Install protoc-gen-pubsub-schema
24-
run: go install
25-
- name: Run examples
26-
run: protoc example/user_add_comment.proto --pubsub-schema_out=.
27-
- name: Verify examples are working
28-
run: if [ -n "$(git status --porcelain)" ]; then git status; exit 1; fi
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-go@v3
16+
with:
17+
go-version: "1.18"
18+
- uses: arduino/setup-protoc@v1
19+
with:
20+
version: "3.x"
21+
- name: Run go test
22+
run: go test -v ./...
23+
- name: Install protoc-gen-pubsub-schema
24+
run: go install
25+
- name: Run examples
26+
run: protoc example/article_commented.proto --pubsub-schema_out=.
27+
- name: Verify examples are working
28+
run: if [ -n "$(git status --porcelain)" ]; then git status; exit 1; fi

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ protoc PROTO_FILES --pubsub-schema_out=OUT_DIR --pubsub-schema_opt=message-encod
3535

3636
## Example
3737

38-
The following example shows how to generate [example/user_add_comment.pps](example/user_add_comment.pps) from [example/user_add_comment.proto](example/user_add_comment.proto).
38+
The following example shows how to generate [example/article_commented.pps](example/article_commented.pps) from [example/article_commented.proto](example/article_commented.proto).
3939

4040
```sh
4141
# include go compiled binaries in the $PATH if it hasn't been there yet
4242
export PATH=$PATH:$(go env GOPATH)/bin
4343

44-
# generate example/user_add_comment.pps
45-
protoc example/user_add_comment.proto --pubsub-schema_out=.
44+
# generate example/article_commented.pps
45+
protoc example/article_commented.proto --pubsub-schema_out=.
4646
```

content_builder.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,53 @@ import (
55
"fmt"
66
"strings"
77

8+
"golang.org/x/exp/slices"
89
"google.golang.org/protobuf/types/descriptorpb"
910
)
1011

1112
type contentBuilder struct {
1213
*responseBuilder
14+
file *descriptorpb.FileDescriptorProto
1315
output *strings.Builder
1416
}
1517

16-
func newContentBuilder(b *responseBuilder) *contentBuilder {
17-
return &contentBuilder{b, new(strings.Builder)}
18+
func newContentBuilder(b *responseBuilder, file *descriptorpb.FileDescriptorProto) *contentBuilder {
19+
return &contentBuilder{b, file, new(strings.Builder)}
1820
}
1921

20-
func (b *contentBuilder) build(protoFile *descriptorpb.FileDescriptorProto) (string, error) {
21-
if protoFile == nil {
22-
return "", errors.New("build(protoFile *descriptorpb.FileDescriptorProto): protoFile is nil")
22+
func (b *contentBuilder) build() (string, error) {
23+
if b.file == nil {
24+
return "", errors.New("contentBuilder.build(): protoFile is nil")
2325
}
2426

25-
if len(protoFile.GetMessageType()) != 1 {
26-
return "", errors.New(protoFile.GetName() + ": only one top-level type may be defined in a file (see https://cloud.google.com/pubsub/docs/schemas#schema_types). use nested types or imports (see https://developers.google.com/protocol-buffers/docs/proto)")
27+
if len(b.file.GetMessageType()) != 1 {
28+
return "", errors.New(b.file.GetName() + ": only one top-level type may be defined in a file (see https://cloud.google.com/pubsub/docs/schemas#schema_types). use nested types or imports (see https://developers.google.com/protocol-buffers/docs/proto)")
2729
}
2830

29-
compVersion := b.request.GetCompilerVersion()
3031
fmt.Fprintf(b.output, "// Code generated by protoc-gen-pubsub-schema. DO NOT EDIT.\n")
3132
fmt.Fprintf(b.output, "// versions:\n")
32-
fmt.Fprintf(b.output, "// protoc-gen-pubsub-schema v1.5.0\n")
33-
fmt.Fprintf(b.output, "// protoc v%d.%d.%d%s\n", compVersion.GetMajor(), compVersion.GetMinor(), compVersion.GetPatch(), compVersion.GetSuffix())
34-
fmt.Fprintf(b.output, "// source: %s\n\n", protoFile.GetName())
33+
fmt.Fprintf(b.output, "// protoc-gen-pubsub-schema v1.6.0\n")
34+
fmt.Fprintf(b.output, "// source: %s\n\n", b.file.GetName())
3535
fmt.Fprintf(b.output, "syntax = \"%s\";\n\n", b.schemaSyntax)
36-
fmt.Fprintf(b.output, "package %s;\n", protoFile.GetPackage())
37-
b.buildMessages(protoFile.GetMessageType(), 0)
38-
b.buildEnums(protoFile.GetEnumType(), 0)
36+
fmt.Fprintf(b.output, "package %s;\n", b.file.GetPackage())
37+
b.buildMessages(0, b.file.GetMessageType())
38+
b.buildEnums(0, b.file.GetEnumType())
3939
return b.output.String(), nil
4040
}
4141

42-
func (b *contentBuilder) buildMessages(messages []*descriptorpb.DescriptorProto, level int) {
42+
func (b *contentBuilder) buildMessages(level int, messages []*descriptorpb.DescriptorProto) {
4343
built := make(map[*descriptorpb.DescriptorProto]bool)
4444
for _, message := range messages {
4545
if built[message] {
4646
continue
4747
}
4848
fmt.Fprintln(b.output)
49-
newMessageBuilder(b, message, level).build()
49+
newMessageBuilder(b, level, message).build()
5050
built[message] = true
5151
}
5252
}
5353

54-
func (b *contentBuilder) buildEnums(enums []*descriptorpb.EnumDescriptorProto, level int) {
54+
func (b *contentBuilder) buildEnums(level int, enums []*descriptorpb.EnumDescriptorProto) {
5555
built := make(map[*descriptorpb.EnumDescriptorProto]bool)
5656
for _, enum := range enums {
5757
if built[enum] {
@@ -67,6 +67,12 @@ func (b *contentBuilder) buildEnums(enums []*descriptorpb.EnumDescriptorProto, l
6767
}
6868
}
6969

70+
func (b *contentBuilder) isInternalDefinition(field *descriptorpb.FieldDescriptorProto) bool {
71+
return (field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE ||
72+
field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_ENUM) &&
73+
slices.Contains(b.fileTypeNames[b.file], field.GetTypeName())
74+
}
75+
7076
func buildIndent(level int) string {
7177
return strings.Repeat(" ", level)
7278
}

example/article_commented.pps

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Code generated by protoc-gen-pubsub-schema. DO NOT EDIT.
2+
// versions:
3+
// protoc-gen-pubsub-schema v1.6.0
4+
// source: example/article_commented.proto
5+
6+
syntax = "proto2";
7+
8+
package example;
9+
10+
message ArticleCommented {
11+
required string article_id = 1;
12+
required User user = 2;
13+
required string comment = 3;
14+
repeated ExampleCommonLabel labels = 4;
15+
required GoogleProtobufTimestamp timestamp = 101;
16+
17+
message User {
18+
required string first_name = 1;
19+
optional string last_name = 2;
20+
required InternalRole internal_role = 3;
21+
required ExampleCommonRole external_role_major = 4;
22+
required ExampleCommonRole external_role_minor = 5;
23+
optional bytes avatar = 6;
24+
optional Location location = 7;
25+
optional GoogleProtobufTimestamp created_at = 8;
26+
optional GoogleProtobufTimestamp updated_at = 9;
27+
28+
message GoogleProtobufTimestamp {
29+
optional int64 seconds = 1;
30+
optional int32 nanos = 2;
31+
}
32+
33+
enum ExampleCommonRole {
34+
OWNER = 0;
35+
EDITOR = 1;
36+
VIEWER = 2;
37+
}
38+
}
39+
40+
message Location {
41+
required double longitude = 1;
42+
required double latitude = 2;
43+
}
44+
45+
message ExampleCommonLabel {
46+
optional string key = 1;
47+
optional string value = 2;
48+
}
49+
50+
message GoogleProtobufTimestamp {
51+
optional int64 seconds = 1;
52+
optional int32 nanos = 2;
53+
}
54+
55+
enum InternalRole {
56+
OWNER = 0;
57+
EDITOR = 1;
58+
VIEWER = 2;
59+
}
60+
}

example/article_commented.proto

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
syntax = "proto2";
2+
3+
package example;
4+
5+
import "example/common/label.proto";
6+
import "example/common/role.proto";
7+
import "google/protobuf/timestamp.proto";
8+
9+
message ArticleCommented {
10+
required string article_id = 1;
11+
required User user = 2;
12+
required string comment = 3;
13+
repeated example.common.Label labels = 4;
14+
required google.protobuf.Timestamp timestamp = 101;
15+
16+
message User {
17+
required string first_name = 1;
18+
optional string last_name = 2;
19+
required InternalRole internal_role = 3;
20+
required example.common.Role external_role_major = 4;
21+
required example.common.Role external_role_minor = 5;
22+
optional bytes avatar = 6;
23+
optional Location location = 7;
24+
optional google.protobuf.Timestamp created_at = 8;
25+
optional google.protobuf.Timestamp updated_at = 9;
26+
}
27+
28+
message Location {
29+
required double longitude = 1;
30+
required double latitude = 2;
31+
}
32+
33+
enum InternalRole {
34+
OWNER = 0;
35+
EDITOR = 1;
36+
VIEWER = 2;
37+
}
38+
}

example/user_add_comment.pps

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

example/user_add_comment.proto

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

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module github.com/alpancs/protoc-gen-pubsub-schema
22

3-
go 1.16
3+
go 1.18
44

5-
require google.golang.org/protobuf v1.27.1
5+
require (
6+
golang.org/x/exp v0.0.0-20221109205753-fc8884afc316
7+
google.golang.org/protobuf v1.28.1
8+
)

go.sum

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
2-
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
32
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
3+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
4+
golang.org/x/exp v0.0.0-20221109205753-fc8884afc316 h1:FedCSp0+vayF11p3wAQndIgu+JTcW2nLp5M+HSefjlM=
5+
golang.org/x/exp v0.0.0-20221109205753-fc8884afc316/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
56
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
67
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
7-
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
8-
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
8+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
9+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

0 commit comments

Comments
 (0)