Skip to content

Commit

Permalink
Merge pull request #4 from chuhlomin/chuhlomin/fix-comments-global-pa…
Browse files Browse the repository at this point in the history
…ckage

Support messages in global package
  • Loading branch information
chuhlomin authored Sep 19, 2020
2 parents b099598 + 768fac4 commit 400654f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var (
parent: nil,
children: make(map[string]*ProtoPackage),
types: make(map[string]*descriptor.DescriptorProto),
comments: make(map[string]Comments),
path: make(map[string]string),
}
)

Expand Down Expand Up @@ -98,7 +100,7 @@ func registerType(pkgName *string, msg *descriptor.DescriptorProto, comments Com

func (pkg *ProtoPackage) lookupType(name string) (*descriptor.DescriptorProto, bool, Comments, string) {
if strings.HasPrefix(name, ".") {
return globalPkg.relativelyLookupType(name[1:len(name)])
return globalPkg.relativelyLookupType(name[1:])
}

for ; pkg != nil; pkg = pkg.parent {
Expand Down Expand Up @@ -397,9 +399,12 @@ var e_TableName = &proto.ExtensionDesc{

func convertFile(file *descriptor.FileDescriptorProto) ([]*plugin.CodeGeneratorResponse_File, error) {
name := path.Base(file.GetName())
pkg, ok := globalPkg.relativelyLookupPackage(file.GetPackage())
if !ok {
return nil, fmt.Errorf("no such package found: %s", file.GetPackage())
pkg := globalPkg
if file.Package != nil {
var ok bool
if pkg, ok = globalPkg.relativelyLookupPackage(file.GetPackage()); !ok {
return nil, fmt.Errorf("no such package found: %s", file.GetPackage())
}
}

comments := ParseComments(file)
Expand Down Expand Up @@ -433,8 +438,12 @@ func convertFile(file *descriptor.FileDescriptorProto) ([]*plugin.CodeGeneratorR
return nil, err
}

filename := tableName + ".schema"
if file.Package != nil {
filename = strings.Replace(file.GetPackage(), ".", "/", -1) + "/" + filename
}
resFile := &plugin.CodeGeneratorResponse_File{
Name: proto.String(fmt.Sprintf("%s/%s.schema", strings.Replace(file.GetPackage(), ".", "/", -1), tableName)),
Name: proto.String(filename),
Content: proto.String(string(jsonSchema)),
}
response = append(response, resFile)
Expand Down
22 changes: 22 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,25 @@ func TestExtraFields(t *testing.T) {
]`,
})
}

func TestGlobalPackage(t *testing.T) {
testConvert(t, `
file_to_generate: "foo.proto"
proto_file <
name: "foo.proto"
message_type <
name: "FooProto"
field <
name: "i32" number: 1 type: TYPE_MESSAGE label: LABEL_OPTIONAL
type_name: ".google.protobuf.Int32Value"
>
options < [gen_bq_schema.bigquery_opts] <table_name: "foo_table"> >
>
>
`,
map[string]string{
"foo_table.schema": `[
{ "name": "i32", "type": "INTEGER", "mode": "NULLABLE" }
]`,
})
}

0 comments on commit 400654f

Please sign in to comment.