Skip to content

Commit

Permalink
[kit] improve desc parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Apr 16, 2023
1 parent 5560ec2 commit 831f887
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 21 deletions.
12 changes: 6 additions & 6 deletions cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ cd "$wd"/contrib || exit
go mod tidy
go fmt ./...
go vet ./...
golangci-lint run
GOWORK=off golangci-lint run

echo "Cleaning up [kit]..."
cd "$wd"/kit || exit
go mod tidy
go fmt ./...
go vet ./...
golangci-lint run
GOWORK=off golangci-lint run

echo "Cleaning up [redisCluster]..."
cd "$wd"/std/clusters/rediscluster || exit
go mod tidy -compat=1.17
go fmt ./...
go vet ./...
golangci-lint run
GOWORK=off golangci-lint run

echo "Cleaning up [fasthttp]..."
cd "$wd"/std/gateways/fasthttp || exit
go mod tidy -compat=1.17
go fmt ./...
go vet ./...
golangci-lint run
GOWORK=off golangci-lint run

echo "Cleaning up [fastws]..."
cd "$wd"/std/gateways/fastws || exit
go mod tidy -compat=1.17
go fmt ./...
go vet ./...
golangci-lint run
GOWORK=off golangci-lint run

echo "Cleaning up [silverhttp]..."
cd "$wd"/std/gateways/silverhttp || exit
go mod tidy -compat=1.17
go fmt ./...
go vet ./...
golangci-lint run
GOWORK=off golangci-lint run
5 changes: 4 additions & 1 deletion contrib/swagger/export_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package swagger

var ToSwaggerDefinition = toSwagDefinition
var (
ToSwaggerDefinition = toSwagDefinition
ToPostmanItem = toPostmanItem
)
10 changes: 6 additions & 4 deletions contrib/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func toSwagDefinition(m desc.ParsedMessage) spec.Schema {
}

for fields.Back() != nil {
p := fields.Remove(fields.Back()).(desc.ParsedField)
p := fields.Remove(fields.Back()).(desc.ParsedField) //nolint:forcetypeassert

name, kind, wrapFuncChain := getWrapFunc(p)

Expand All @@ -245,6 +245,8 @@ func toSwagDefinition(m desc.ParsedMessage) spec.Schema {
def.SetProperty(p.Name, wrapFuncChain.Apply(spec.Int64Property()))
case desc.Bool:
def.SetProperty(p.Name, wrapFuncChain.Apply(spec.BoolProperty()))
case desc.Byte:
def.SetProperty(p.Name, wrapFuncChain.Apply(spec.Int8Property()))
default:
def.SetProperty(p.Name, wrapFuncChain.Apply(spec.StringProperty()))
}
Expand Down Expand Up @@ -315,15 +317,15 @@ func (sg *Generator) WritePostmanTo(w io.Writer, descs ...desc.ServiceDesc) erro
for _, c := range ps.Contracts {
switch c.Type {
case desc.REST:
sg.addPostmanItem(colItems, c)
colItems.AddItem(toPostmanItem(c))
}
}
}

return col.Write(w, postman.V210)
}

func (sg *Generator) addPostmanItem(items *postman.Items, c desc.ParsedContract) {
func toPostmanItem(c desc.ParsedContract) *postman.Items {
itm := postman.CreateItem(
postman.Item{
Name: c.SuggestName(),
Expand Down Expand Up @@ -406,7 +408,7 @@ func (sg *Generator) addPostmanItem(items *postman.Items, c desc.ParsedContract)
)
}

items.AddItem(itm)
return itm
}

func setSwaggerParam(p *spec.Parameter, pp desc.ParsedField) *spec.Parameter {
Expand Down
16 changes: 13 additions & 3 deletions contrib/swagger/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,32 @@ func TestSwagger(t *testing.T) {

var _ = Describe("ToSwaggerDefinition", func() {
ps := desc.Parse(testService{})
BeforeEach(func() {

})
It("Check Request for Contract[0].Request (sampleReq)", func() {
// type sampleReq struct {
// X string `json:"x,int"`
// Y string `json:"y,omitempty"`
// Z int64 `json:"z"`
// W []string `json:"w"`
// TT [][]string `json:"tt"`
// }

d := swagger.ToSwaggerDefinition(ps.Contracts[0].Request.Message)
props := d.Properties.ToOrderedSchemaItems()
Expect(props[0].Name).To(Equal("tt"))
Expect(props[0].SchemaProps.Type[0]).To(Equal("array"))
Expect(props[0].SchemaProps.Items.Schema.Type[0]).To(Equal("array"))
Expect(props[0].SchemaProps.Items.Schema.Items.Schema.Type[0]).To(Equal("string"))
Expect(props[1].Name).To(Equal("w"))
Expect(props[1].SchemaProps.Type[0]).To(Equal("array"))
Expect(props[1].SchemaProps.Items.Schema.Type[0]).To(Equal("string"))
Expect(props[2].Name).To(Equal("x"))
Expect(props[2].SchemaProps.Type[0]).To(Equal("string"))
Expect(props[3].Name).To(Equal("y"))
Expect(props[3].SchemaProps.Type[0]).To(Equal("string"))
Expect(props[4].Name).To(Equal("z"))
Expect(props[4].SchemaProps.Type[0]).To(Equal("integer"))
})

It("Check Request for Contract[0].Response (sampleRes)", func() {
d := swagger.ToSwaggerDefinition(ps.Contracts[0].Responses[0].Message)
props := d.Properties.ToOrderedSchemaItems()
Expand Down Expand Up @@ -158,6 +167,7 @@ var _ = Describe("ToSwaggerDefinition", func() {
props := d.Properties.ToOrderedSchemaItems()
Expect(props[0].Name).To(Equal("another"))
Expect(props[0].SchemaProps.Type[0]).To(Equal("array"))
Expect(props[0].SchemaProps.Items.Schema.Type[0]).To(Equal("integer"))
Expect(props[1].Name).To(Equal("out1"))
Expect(props[1].SchemaProps.Type[0]).To(Equal("integer"))
Expect(props[2].Name).To(Equal("out2"))
Expand Down
52 changes: 45 additions & 7 deletions kit/desc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ const (
String Kind = "string"
Integer Kind = "integer"
Float Kind = "float"
Byte Kind = "byte"
Object Kind = "object"
Map Kind = "map"
Array Kind = "array"
Expand All @@ -302,14 +303,49 @@ func (pm ParsedMessage) JSON() string {
m := map[string]interface{}{}
for _, p := range pm.Fields {
switch p.Kind {
default:
m[p.Name] = p.Kind
case Object:
m[p.Name] = json.RawMessage(p.Message.JSON())
case Map:
m[p.Name] = map[string]interface{}{}
var inner interface{}
switch p.Element.Kind {
default:
inner = p.Element.Kind
case Object:
inner = json.RawMessage(p.Element.Message.JSON())
case Integer, Float, Byte:
inner = 0
case Array:
inner = []interface{}{p.Element.Element.Kind}
case Map:
inner = map[string]interface{}{
"keyName": p.Element.Element.Kind,
}
}
m[p.Name] = map[string]interface{}{
"keyName": inner,
}
case Array:
m[p.Name] = []interface{}{}
case Integer, Float:
var inner interface{}
switch p.Element.Kind {
default:
inner = p.Element.Kind
case Object:
inner = json.RawMessage(p.Element.Message.JSON())
case Integer, Float, Byte:
inner = 0
case Array:
inner = []interface{}{p.Element.Element.Kind}
case Map:
inner = map[string]interface{}{
"keyName": p.Element.Element.Kind,
}
}
m[p.Name] = []interface{}{inner}
case Integer, Float, Byte:
m[p.Name] = 0
default:
m[p.Name] = p.Kind

}
}

Expand Down Expand Up @@ -414,8 +450,10 @@ func parseKind(t reflect.Type) Kind {
return Bool
case reflect.String:
return String
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Uint8, reflect.Int8:
return Byte
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Integer
case reflect.Float32, reflect.Float64:
return Float
Expand Down
17 changes: 17 additions & 0 deletions kit/desc/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package desc_test

import (
"fmt"

"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/desc"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -129,3 +131,18 @@ var _ = Describe("DescParser", func() {
Expect(m.Element.Element.Message).To(BeNil())
})
})

var _ = Describe("ParseMessage.JSON()", func() {
d := desc.NewService("sample").
AddContract(
desc.NewContract().
SetName("c1").
NamedSelector("s1", newREST(kit.JSON, "/path1", "GET")).
NamedSelector("s2", newREST(kit.JSON, "/path2", "POST")).
In(&NestedMessage{}).
Out(&FlatMessage{}),
)

ps := desc.ParseService(d)
fmt.Println(ps.Contracts[0].Request.Message.JSON())
})

0 comments on commit 831f887

Please sign in to comment.