Skip to content

Commit

Permalink
Merge pull request #299 from zwiedmann-isp/allow_foreigntypes_inrpcs
Browse files Browse the repository at this point in the history
fix: parsing errors when using foreign types in rpcs
  • Loading branch information
zaquestion committed Jul 25, 2020
2 parents c575929 + a475239 commit 73dec66
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cmd/truss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func parseInput() (*truss.Config, error) {
return nil, errors.Wrap(err, "generated service not found in importable go package")
}

log.WithField("Service Packages", p).Info()
log.WithField("Service Packages", p).Debug()

cfg.ServicePackage = p[0].PkgPath
cfg.ServicePath = svcPath
Expand Down
16 changes: 1 addition & 15 deletions gengokit/handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"strings"
"testing"

log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/metaverse/truss/gengokit"
helper "github.com/metaverse/truss/gengokit/gentesthelper"
Expand Down Expand Up @@ -76,12 +76,8 @@ func TestServerMethsTempl(t *testing.T) {
}
genBytes, err := ioutil.ReadAll(gen)
const expected = `
// ProtoMethod implements Service.
func (s protoService) ProtoMethod(ctx context.Context, in *pb.RequestMessage) (*pb.ResponseMessage, error){
var resp pb.ResponseMessage
resp = pb.ResponseMessage{
// Output:
}
return &resp, nil
}
`
Expand Down Expand Up @@ -149,12 +145,8 @@ func TestApplyServerTempl(t *testing.T) {
type protoService struct{}
// ProtoMethod implements Service.
func (s protoService) ProtoMethod(ctx context.Context, in *pb.RequestMessage) (*pb.ResponseMessage, error) {
var resp pb.ResponseMessage
resp = pb.ResponseMessage{
// Output:
}
return &resp, nil
}
`
Expand Down Expand Up @@ -326,18 +318,12 @@ func TestPruneDecls(t *testing.T) {
// ProtoMethod implements Service.
func (s protoService) ProtoMethod(ctx context.Context, in *pb.RequestMessage) (*pb.ResponseMessage, error) {
var resp pb.ResponseMessage
resp = pb.ResponseMessage{
// Output:
}
return &resp, nil
}
// FOOBAR implements Service.
func (s protoService) FOOBAR(ctx context.Context, in *pb.RequestMessage) (*pb.ResponseMessage, error) {
var resp pb.ResponseMessage
resp = pb.ResponseMessage{
// Output:
}
return &resp, nil
}
`
Expand Down
12 changes: 0 additions & 12 deletions gengokit/handlers/templates/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ package templates
const HandlerMethods = `
{{ with $te := .}}
{{range $i := .Methods}}
// {{.Name}} implements Service.
func (s {{ToLower $te.ServiceName}}Service) {{.Name}}(ctx context.Context, in *pb.{{GoName .RequestType.Name}}) (*pb.{{GoName .ResponseType.Name}}, error){
var resp pb.{{GoName .ResponseType.Name}}
resp = pb.{{GoName .ResponseType.Name}}{
{{range $j := $i.ResponseType.Message.Fields -}}
// {{GoName $j.Name}}:
{{end -}}
}
return &resp, nil
}
{{end}}
Expand All @@ -35,14 +29,8 @@ type {{ToLower .Service.Name}}Service struct{}
{{with $te := . }}
{{range $i := $te.Service.Methods}}
// {{$i.Name}} implements Service.
func (s {{ToLower $te.Service.Name}}Service) {{$i.Name}}(ctx context.Context, in *pb.{{GoName $i.RequestType.Name}}) (*pb.{{GoName $i.ResponseType.Name}}, error){
var resp pb.{{GoName $i.ResponseType.Name}}
resp = pb.{{GoName $i.ResponseType.Name}}{
{{range $j := $i.ResponseType.Message.Fields -}}
// {{GoName $j.Name}}:
{{end -}}
}
return &resp, nil
}
{{end}}
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ go 1.13
require (
github.com/davecgh/go-spew v1.1.1
github.com/gogo/protobuf v1.2.2-0.20190601103108-21df5aa0e680
github.com/kevinburke/go-bindata v3.21.0+incompatible // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/pflag v1.0.5
golang.org/x/tools v0.0.0-20191018190819-0bbdbb2ef609
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/sys v0.0.0-20191220142924-d4481acd189f // indirect
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
28 changes: 23 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gogo/protobuf v1.2.2-0.20190601103108-21df5aa0e680 h1:C0sYRtvp5PUPDdevTvhGWbyiRrLM1ci+tHfyJymCNzM=
github.com/gogo/protobuf v1.2.2-0.20190601103108-21df5aa0e680/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/kevinburke/go-bindata v3.21.0+incompatible h1:baK7hwFJDlAHrOqmE9U3u8tow1Uc5ihN9E/b7djcK2g=
github.com/kevinburke/go-bindata v3.21.0+incompatible/go.mod h1:/pEEZ72flUW2p0yi30bslSp9YqD9pysLxunQDdb2CPM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -16,19 +20,33 @@ github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191220142924-d4481acd189f h1:68K/z8GLUxV76xGSqwTWw2gyk/jwn79LUL43rES2g8o=
golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191018190819-0bbdbb2ef609 h1:hkvhomHA88LaxvpleecITQ0Z2pKbcmgvRIMP6UlHrn8=
golang.org/x/tools v0.0.0-20191018190819-0bbdbb2ef609/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114 h1:DnSr2mCsxyCE6ZgIkmcWUQY2R5cH/6wL7eIxEmQOMSE=
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
16 changes: 11 additions & 5 deletions svcdef/svcdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"reflect"
"strings"

log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

// Svcdef is the top-level struct for the definition of a service.
Expand Down Expand Up @@ -448,14 +448,20 @@ func NewServiceMethod(m *ast.Field, info *DebugInfo) (*ServiceMethod, error) {
"is not *ast.StarExpr",
info.Path, info.Position(in.Pos()))
}
ident, ok := star.X.(*ast.Ident)
if !ok {

var name string
switch node := star.X.(type) {
case *ast.SelectorExpr: // package.FuncName
name = fmt.Sprintf("%s", node.Sel.Name)
case *ast.Ident: // FuncName
name = fmt.Sprintf("%s", node.Name)
default:
return nil, NewLocationError("cannot create FieldType, "+
"star.Type is not *ast.Ident",
"star.Type is not *ast.Ident or *ast.SelectorExpr",
info.Path, info.Position(star.Pos()))
}
return &FieldType{
Name: ident.Name,
Name: name,
StarExpr: true,
}, nil
}
Expand Down
48 changes: 32 additions & 16 deletions svcdef/svcparse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,22 @@ func ParseMethod(lex *SvcLexer) (*Method, error) {
}
}

toret.RequestType = val

tk, val = lex.GetTokenIgnoreWhitespace()
if tk != CLOSE_PAREN {
return nil, parserErr{
expected: "')'",
line: lex.GetLineNumber(),
val: val,
// Looping here handles for request types from other packages
// (google.protobuf.Struct). Only the last ident is kept as truss
// doesn't support properly importanting external packages, but by
// using this approach type aliases can be made alongside the generated
// pb.go code (type Struct = types.Struct)
for ; tk != CLOSE_PAREN; tk, val = lex.GetTokenIgnoreWhitespace() {
switch tk {
case SYMBOL: // skip
case IDENT:
toret.RequestType = val
default:
return nil, parserErr{
expected: "')' or '.'",
line: lex.GetLineNumber(),
val: val,
}
}
}

Expand Down Expand Up @@ -320,14 +328,22 @@ func ParseMethod(lex *SvcLexer) (*Method, error) {
}
}

toret.ResponseType = val

tk, val = lex.GetTokenIgnoreWhitespace()
if tk != CLOSE_PAREN {
return nil, parserErr{
expected: "')' after declaration of return type to method",
line: lex.GetLineNumber(),
val: val,
// Looping here handles for response types from other packages
// (google.protobuf.Struct). Only the last ident is kept as truss
// doesn't support properly importanting external packages, but by
// using this approach type aliases can be made alongside the generated
// pb.go code (type Struct = types.Struct)
for ; tk != CLOSE_PAREN; tk, val = lex.GetTokenIgnoreWhitespace() {
switch tk {
case SYMBOL: // skip
case IDENT:
toret.ResponseType = val
default:
return nil, parserErr{
expected: "')' or '.'",
line: lex.GetLineNumber(),
val: val,
}
}
}

Expand Down

0 comments on commit 73dec66

Please sign in to comment.