Skip to content

Commit

Permalink
[kit] fix parser issue with pointer elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Feb 13, 2024
1 parent 1db18da commit 766ea89
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
21 changes: 13 additions & 8 deletions contrib/swagger/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ type subRes struct {
}

type sampleRes struct {
Out1 int `json:"out1"`
Out2 string `json:"out2"`
Sub subRes `json:"sub"`
Subs []subRes `json:"subs"`
XSub *subRes `json:"xSub"`
Out1 int `json:"out1"`
Out2 string `json:"out2"`
Sub subRes `json:"sub"`
Subs []subRes `json:"subs,omitempty"`
XSub *subRes `json:"xSub"`
Subs2 []*subRes `json:"subs2,omitempty"`
}

type sampleError struct {
Expand Down Expand Up @@ -135,6 +136,7 @@ var _ = Describe("ToSwaggerDefinition", func() {
// Sub subRes `json:"sub"`
// Subs []subRes `json:"subs,omitempty"`
// XSub *subRes `json:"xSub"`
// Subs2 []*subRes `json:"subs2,omitempty"`
// }

d := swagger.ToSwaggerDefinition(ps.Contracts[0].Request.Message)
Expand Down Expand Up @@ -167,9 +169,12 @@ var _ = Describe("ToSwaggerDefinition", func() {
Expect(props[3].Name).To(Equal("subs"))
Expect(props[3].SchemaProps.Type[0]).To(Equal("array"))
Expect(props[3].SchemaProps.Items.Schema.Ref.String()).To(Equal("#/definitions/subRes"))
Expect(props[4].Name).To(Equal("xSub"))
Expect(props[4].SchemaProps.Ref.String()).To(Equal("#/definitions/subRes"))
Expect(props[4].SchemaProps.Description).To(Equal("[Optional]"))
Expect(props[4].Name).To(Equal("subs2"))
Expect(props[4].SchemaProps.Type[0]).To(Equal("array"))
Expect(props[4].SchemaProps.Items.Schema.Ref.String()).To(Equal("#/definitions/subRes"))
Expect(props[5].Name).To(Equal("xSub"))
Expect(props[5].SchemaProps.Ref.String()).To(Equal("#/definitions/subRes"))
Expect(props[5].SchemaProps.Description).To(Equal("[Optional]"))
})

It("Check Request for Contract[1].Response (anotherRes)", func() {
Expand Down
15 changes: 8 additions & 7 deletions kit/desc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (ps *ParsedService) parseMessage(m kit.Message, enc kit.Encoding) ParsedMes
pp.Type = ft.String()
switch pp.Kind {
case Map:
// we only support maps with string keys
if ft.Key().Kind() != reflect.String {
continue
}
Expand All @@ -153,15 +154,16 @@ func (ps *ParsedService) parseMessage(m kit.Message, enc kit.Encoding) ParsedMes
keepGoing := true
for keepGoing {
ft = ft.Elem()
kind := parseKind(ft)
pe.Kind = kind
switch kind {
pe.Kind = parseKind(ft)
switch pe.Kind {
case Map, Array:
pe.Element = &ParsedElement{}
pe = pe.Element
case Object:
pm := ps.parseMessage(reflect.New(ft).Interface(), enc)
pe.Message = &pm
if ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
pe.Message = utils.ValPtr(ps.parseMessage(reflect.New(ft).Interface(), enc))
keepGoing = false
default:
keepGoing = false
Expand All @@ -173,8 +175,7 @@ func (ps *ParsedService) parseMessage(m kit.Message, enc kit.Encoding) ParsedMes
} else if ps.isVisited(ft.Name()) {
panic(fmt.Sprintf("infinite recursion detected: %s.%s", mt.Name(), ft.Name()))
} else {
pm := ps.parseMessage(reflect.New(ft).Interface(), enc)
pp.Message = &pm
pp.Message = utils.ValPtr(ps.parseMessage(reflect.New(ft).Interface(), enc))
}

case None:
Expand Down
22 changes: 15 additions & 7 deletions kit/desc/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ type FlatMessage struct {
}

type NestedMessage struct {
A string `json:"a"`
B FlatMessage `json:"b"`
BA []FlatMessage `json:"ba,omitempty"`
BM map[string]FlatMessage `json:"bm"`
C *FlatMessage `json:"c"`
A string `json:"a"`
B FlatMessage `json:"b"`
BA []FlatMessage `json:"ba,omitempty"`
BM map[string]FlatMessage `json:"bm"`
C *FlatMessage `json:"c"`
PA []*FlatMessage `json:"pa"`
PMap map[string]*FlatMessage `json:"pmap"`
}

var _ = Describe("DescParser", func() {
Expand All @@ -79,7 +81,7 @@ var _ = Describe("DescParser", func() {
Expect(contract0.Method).To(Equal("GET"))
Expect(contract0.GroupName).To(Equal("c1"))
Expect(contract0.Request.Message.Name).To(Equal("NestedMessage"))
Expect(contract0.Request.Message.Fields).To(HaveLen(5))
Expect(contract0.Request.Message.Fields).To(HaveLen(7))
Expect(contract0.Responses[0].Message.Name).To(Equal("FlatMessage"))
Expect(contract0.Responses[0].Message.Fields).To(HaveLen(6))

Expand All @@ -91,7 +93,7 @@ var _ = Describe("DescParser", func() {
Expect(contract1.GroupName).To(Equal("c1"))

Expect(contract0.Request.Message.Name).To(Equal("NestedMessage"))
Expect(contract0.Request.Message.Fields).To(HaveLen(5))
Expect(contract0.Request.Message.Fields).To(HaveLen(7))
Expect(contract0.Request.Message.Fields[0].Name).To(Equal("a"))
Expect(contract0.Request.Message.Fields[0].Kind).To(Equal(desc.String))
Expect(contract0.Request.Message.Fields[1].Name).To(Equal("b"))
Expand All @@ -104,6 +106,12 @@ var _ = Describe("DescParser", func() {
Expect(contract0.Request.Message.Fields[4].Optional).To(BeTrue())
Expect(contract0.Request.Message.FieldByName("ba").Kind).To(Equal(desc.Array))
Expect(contract0.Request.Message.FieldByGoName("BA").Element.Kind).To(Equal(desc.Object))
Expect(contract0.Request.Message.FieldByGoName("PA").Kind).To(Equal(desc.Array))
Expect(contract0.Request.Message.FieldByGoName("PA").Element.Kind).To(Equal(desc.Object))
Expect(contract0.Request.Message.FieldByGoName("PA").Element.Message.Name).To(Equal("FlatMessage"))
Expect(contract0.Request.Message.FieldByGoName("PMap").Kind).To(Equal(desc.Map))
Expect(contract0.Request.Message.FieldByGoName("PMap").Element.Kind).To(Equal(desc.Object))
Expect(contract0.Request.Message.FieldByGoName("PMap").Element.Message.Name).To(Equal("FlatMessage"))

b := contract0.Request.Message.Fields[1]
Expect(b.Name).To(Equal("b"))
Expand Down
6 changes: 3 additions & 3 deletions kit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ require (
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/klauspost/compress v1.17.3 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions kit/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -27,8 +27,8 @@ github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8I
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
github.com/jedib0t/go-pretty/v6 v6.5.4 h1:gOGo0613MoqUcf0xCj+h/V3sHDaZasfv152G6/5l91s=
github.com/jedib0t/go-pretty/v6 v6.5.4/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg=
github.com/klauspost/compress v1.17.3 h1:qkRjuerhUU1EmXLYGkSH6EZL+vPSxIrYjLNAK4slzwA=
github.com/klauspost/compress v1.17.3/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand Down Expand Up @@ -58,13 +58,13 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHYoKol/szxQ=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
Expand Down

0 comments on commit 766ea89

Please sign in to comment.