Skip to content

Commit

Permalink
Fix Duplicate Declarations (#17)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alpancs authored Nov 10, 2022
1 parent a6e79ed commit 2233007
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 162 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ name: Go

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.17'
- uses: arduino/setup-protoc@v1
with:
version: '3.19.1'
- name: Run go test
run: go test -v ./...
- name: Install protoc-gen-pubsub-schema
run: go install
- name: Run examples
run: protoc example/user_add_comment.proto --pubsub-schema_out=.
- name: Verify examples are working
run: if [ -n "$(git status --porcelain)" ]; then git status; exit 1; fi
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.18"
- uses: arduino/setup-protoc@v1
with:
version: "3.x"
- name: Run go test
run: go test -v ./...
- name: Install protoc-gen-pubsub-schema
run: go install
- name: Run examples
run: protoc example/article_commented.proto --pubsub-schema_out=.
- name: Verify examples are working
run: if [ -n "$(git status --porcelain)" ]; then git status; exit 1; fi
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ protoc PROTO_FILES --pubsub-schema_out=OUT_DIR --pubsub-schema_opt=message-encod

## Example

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).
The following example shows how to generate [example/article_commented.pps](example/article_commented.pps) from [example/article_commented.proto](example/article_commented.proto).

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

# generate example/user_add_comment.pps
protoc example/user_add_comment.proto --pubsub-schema_out=.
# generate example/article_commented.pps
protoc example/article_commented.proto --pubsub-schema_out=.
```
40 changes: 23 additions & 17 deletions content_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,53 @@ import (
"fmt"
"strings"

"golang.org/x/exp/slices"
"google.golang.org/protobuf/types/descriptorpb"
)

type contentBuilder struct {
*responseBuilder
file *descriptorpb.FileDescriptorProto
output *strings.Builder
}

func newContentBuilder(b *responseBuilder) *contentBuilder {
return &contentBuilder{b, new(strings.Builder)}
func newContentBuilder(b *responseBuilder, file *descriptorpb.FileDescriptorProto) *contentBuilder {
return &contentBuilder{b, file, new(strings.Builder)}
}

func (b *contentBuilder) build(protoFile *descriptorpb.FileDescriptorProto) (string, error) {
if protoFile == nil {
return "", errors.New("build(protoFile *descriptorpb.FileDescriptorProto): protoFile is nil")
func (b *contentBuilder) build() (string, error) {
if b.file == nil {
return "", errors.New("contentBuilder.build(): protoFile is nil")
}

if len(protoFile.GetMessageType()) != 1 {
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)")
if len(b.file.GetMessageType()) != 1 {
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)")
}

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

func (b *contentBuilder) buildMessages(messages []*descriptorpb.DescriptorProto, level int) {
func (b *contentBuilder) buildMessages(level int, messages []*descriptorpb.DescriptorProto) {
built := make(map[*descriptorpb.DescriptorProto]bool)
for _, message := range messages {
if built[message] {
continue
}
fmt.Fprintln(b.output)
newMessageBuilder(b, message, level).build()
newMessageBuilder(b, level, message).build()
built[message] = true
}
}

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

func (b *contentBuilder) isInternalDefinition(field *descriptorpb.FieldDescriptorProto) bool {
return (field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE ||
field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_ENUM) &&
slices.Contains(b.fileTypeNames[b.file], field.GetTypeName())
}

func buildIndent(level int) string {
return strings.Repeat(" ", level)
}
60 changes: 60 additions & 0 deletions example/article_commented.pps
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Code generated by protoc-gen-pubsub-schema. DO NOT EDIT.
// versions:
// protoc-gen-pubsub-schema v1.6.0
// source: example/article_commented.proto

syntax = "proto2";

package example;

message ArticleCommented {
required string article_id = 1;
required User user = 2;
required string comment = 3;
repeated ExampleCommonLabel labels = 4;
required GoogleProtobufTimestamp timestamp = 101;

message User {
required string first_name = 1;
optional string last_name = 2;
required InternalRole internal_role = 3;
required ExampleCommonRole external_role_major = 4;
required ExampleCommonRole external_role_minor = 5;
optional bytes avatar = 6;
optional Location location = 7;
optional GoogleProtobufTimestamp created_at = 8;
optional GoogleProtobufTimestamp updated_at = 9;

message GoogleProtobufTimestamp {
optional int64 seconds = 1;
optional int32 nanos = 2;
}

enum ExampleCommonRole {
OWNER = 0;
EDITOR = 1;
VIEWER = 2;
}
}

message Location {
required double longitude = 1;
required double latitude = 2;
}

message ExampleCommonLabel {
optional string key = 1;
optional string value = 2;
}

message GoogleProtobufTimestamp {
optional int64 seconds = 1;
optional int32 nanos = 2;
}

enum InternalRole {
OWNER = 0;
EDITOR = 1;
VIEWER = 2;
}
}
38 changes: 38 additions & 0 deletions example/article_commented.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
syntax = "proto2";

package example;

import "example/common/label.proto";
import "example/common/role.proto";
import "google/protobuf/timestamp.proto";

message ArticleCommented {
required string article_id = 1;
required User user = 2;
required string comment = 3;
repeated example.common.Label labels = 4;
required google.protobuf.Timestamp timestamp = 101;

message User {
required string first_name = 1;
optional string last_name = 2;
required InternalRole internal_role = 3;
required example.common.Role external_role_major = 4;
required example.common.Role external_role_minor = 5;
optional bytes avatar = 6;
optional Location location = 7;
optional google.protobuf.Timestamp created_at = 8;
optional google.protobuf.Timestamp updated_at = 9;
}

message Location {
required double longitude = 1;
required double latitude = 2;
}

enum InternalRole {
OWNER = 0;
EDITOR = 1;
VIEWER = 2;
}
}
52 changes: 0 additions & 52 deletions example/user_add_comment.pps

This file was deleted.

29 changes: 0 additions & 29 deletions example/user_add_comment.proto

This file was deleted.

7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/alpancs/protoc-gen-pubsub-schema

go 1.16
go 1.18

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

0 comments on commit 2233007

Please sign in to comment.