Skip to content

Commit

Permalink
Fix optional and add debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
srikrsna committed Jul 4, 2021
1 parent 61279e2 commit c1f01ee
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 8 deletions.
3 changes: 3 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ plugins:
- name: go
out: .
opt: paths=source_relative
- name: debug
out: .
opt: ./debug
- name: gotag
out: .
opt: paths=source_relative,xxx=graphql+"-" bson+"-"
Binary file added debug/code_generator_request.pb.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion example/example.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/srikrsna/protoc-gen-gotag

go 1.11
go 1.16

retract v0.6.0

require (
github.com/fatih/structtag v1.2.0
github.com/lyft/protoc-gen-star v0.5.3
github.com/spf13/afero v1.5.1 // indirect
github.com/spf13/afero v1.5.1
golang.org/x/text v0.3.5 // indirect
google.golang.org/protobuf v1.26.0
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
2 changes: 1 addition & 1 deletion module/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (v *tagExtractor) VisitField(f pgs.Field) (pgs.Visitor, error) {
}

msgName := v.Context.Name(f.Message()).String()
if f.InOneOf() {
if f.InOneOf() && !f.Descriptor().GetProto3Optional() {
msgName = f.Message().Name().UpperCamelCase().String() + "_" + f.Name().UpperCamelCase().String()
}

Expand Down
27 changes: 27 additions & 0 deletions module/extract_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package module_test

import (
"bytes"
"os"
"testing"

pgs "github.com/lyft/protoc-gen-star"
"github.com/spf13/afero"
"github.com/srikrsna/protoc-gen-gotag/module"
)

func TestExtract(t *testing.T) {
req, err := os.Open("../debug/code_generator_request.pb.bin")
if err != nil {
t.Fatal(err)
}

fs := afero.NewMemMapFs()
res := &bytes.Buffer{}

pgs.Init(
pgs.ProtocInput(req),
pgs.ProtocOutput(res),
pgs.FileSystem(fs),
).RegisterModule(module.New()).Render()
}
2 changes: 1 addition & 1 deletion module/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestRetag(t *testing.T) {
}

module.Retag(n, map[string]map[string]*structtag.Tags{
"Simple": map[string]*structtag.Tags{
"Simple": {
"Single": tagMust(structtag.Parse(`sql:"-,omitempty"`)),
"Multiple": tagMust(structtag.Parse(`xml:"-,omitempty"`)),
"None": tagMust(structtag.Parse(`json:"none,omitempty"`)),
Expand Down
2 changes: 1 addition & 1 deletion module/test/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package main

type Simple struct {
Single string `json:"key,option" sql:"-,omitempty"`
Multiple string `json:"ke,op" xml:"-,omitempty"`
Multiple *string `json:"ke,op" xml:"-,omitempty"`
None int32 `json:"none,omitempty"`
}
2 changes: 1 addition & 1 deletion module/test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package main

type Simple struct {
Single string `json:"key,option"`
Multiple string `json:"ke,op" xml:"ke,op"`
Multiple *string `json:"ke,op" xml:"ke,op"`
None int32
}

0 comments on commit c1f01ee

Please sign in to comment.