Skip to content

Commit

Permalink
Improve handling of optional and nullable fields in stubs.
Browse files Browse the repository at this point in the history
Updated TypeScript and Go stub generation logic to correctly handle "omitempty" behavior and added nullable type support for optional fields. Also included a minor upgrade of dependencies to the latest version (v0.18.4) to ensure compatibility.
  • Loading branch information
ehsannm committed Dec 23, 2024
1 parent 7ce8b94 commit 27acddb
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
8 changes: 4 additions & 4 deletions contrib/go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
github.com/clubpay/ronykit/kit v0.18.3 h1:k/oimS8Bj4OjMDZT1eOBdN2TAktJop738inC8MtXePU=
github.com/clubpay/ronykit/kit v0.18.3/go.mod h1:CmkwWBo83JY5T9w+9L6HOFPpVm68qEjolEhIuFOH0/s=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.3 h1:cSX+ThNQ7HmQ6TQiS8t21YFBJ+8shkLFriSz2FILPO0=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.3/go.mod h1:hbYF/PcgMl0wvB6kS/enxcPDTu6ma9R2y8XJ8G2F7Ug=
github.com/clubpay/ronykit/kit v0.18.4 h1:o40w9KVpsAizD8M4HYrNvTU2h+b1x5/8Kv9Hrb9DevE=
github.com/clubpay/ronykit/kit v0.18.4/go.mod h1:CmkwWBo83JY5T9w+9L6HOFPpVm68qEjolEhIuFOH0/s=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.4 h1:DmCKNab1w18zO8d8zTiGVUBgeQs49ylienYvM6QeDh0=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.4/go.mod h1:teSFdgqIGBlrKCkdJcKURtSD2beAWoe/ko+ziaupwm8=
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=
Expand Down
1 change: 1 addition & 0 deletions example/ex-04-stubgen/dto/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type VeryComplexRequest struct {
SimpleHdr
Key1 string `json:"key1"`
Key1Ptr *string `json:"key1Ptr"`
Key2Ptr *int `json:"key2Ptr,omitempty"`
MapKey1 map[string]int `json:"mapKey1"`
MapKey2 map[int64]KeyValue `json:"mapKey2"`
SliceKey1 []bool `json:"sliceKey1"`
Expand Down
1 change: 1 addition & 0 deletions example/ex-04-stubgen/stub/sampleservice/stub.go

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

23 changes: 12 additions & 11 deletions example/ex-04-stubgen/stub/sampleservicets/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ export interface SimpleHdr {
// VeryComplexRequest is a data transfer object
export interface VeryComplexRequest extends SimpleHdr {
key1: string;
key1Ptr?: string;
mapKey1: { [key: string]: number };
mapKey2: { [key: number]: KeyValue };
sliceKey1: boolean[];
sliceKey2: KeyValue[];
rawKey: any;
key1Ptr: string | null;
key2Ptr?: number;
mapKey1: { [key: string]: number } | null;
mapKey2: { [key: number]: KeyValue } | null;
sliceKey1: boolean[] | null;
sliceKey2: KeyValue[] | null;
rawKey: any | null;
}
// VeryComplexResponse is a data transfer object
export interface VeryComplexResponse {
key1: string;
key1?: string;
key1Ptr?: string;
mapKey1: { [key: string]: number };
mapKey2: { [key: number]: KeyValue };
sliceKey1: number[];
sliceKey2: KeyValue[];
mapKey1?: { [key: string]: number };
mapKey2?: { [key: number]: KeyValue };
sliceKey1: number[] | null;
sliceKey2: KeyValue[] | null;
}

export class sampleServiceStub {
Expand Down
2 changes: 1 addition & 1 deletion kit/desc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (ps *ParsedService) parseMessage(m kit.Message, enc kit.Encoding) ParsedMes
GoName: f.Name,
Name: ptn.Value,
Tag: ptn,
Optional: ft.Kind() == reflect.Pointer,
Optional: ft.Kind() == reflect.Pointer || ft.Kind() == reflect.Slice || ft.Kind() == reflect.Map,
Embedded: f.Anonymous,
Element: utils.ValPtr(ps.parseElement(ft, enc)),
},
Expand Down
16 changes: 8 additions & 8 deletions rony/go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
github.com/clubpay/ronykit/contrib v0.18.3 h1:aawPjX1ubO0CvDKYRsAcS4e/iM/HniGu+MCF13JqO90=
github.com/clubpay/ronykit/contrib v0.18.3/go.mod h1:7EJtn0HcO2BtBBD6o3ztmxLnODatwm+dkMHjIU2rjCE=
github.com/clubpay/ronykit/kit v0.18.3 h1:k/oimS8Bj4OjMDZT1eOBdN2TAktJop738inC8MtXePU=
github.com/clubpay/ronykit/kit v0.18.3/go.mod h1:CmkwWBo83JY5T9w+9L6HOFPpVm68qEjolEhIuFOH0/s=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.3 h1:cSX+ThNQ7HmQ6TQiS8t21YFBJ+8shkLFriSz2FILPO0=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.3/go.mod h1:hbYF/PcgMl0wvB6kS/enxcPDTu6ma9R2y8XJ8G2F7Ug=
github.com/clubpay/ronykit/stub v0.18.3 h1:SeQ2jXhGkBZ6S3ZSVbJ5rVhFOMWwNbZUpry3vfq515c=
github.com/clubpay/ronykit/stub v0.18.3/go.mod h1:ANC0424PibQQY6UIIyIRmua6ONcQjp8eYjQhAFZaJIc=
github.com/clubpay/ronykit/contrib v0.18.4 h1:++AczjeD3kfkJqSRvvH6ihY9iaZwDGXVpEhGCJjWlpk=
github.com/clubpay/ronykit/contrib v0.18.4/go.mod h1:5QO8WnCMa9zD1aEmAKfbJj4HBtocQCDjVyrcM4UvTPc=
github.com/clubpay/ronykit/kit v0.18.4 h1:o40w9KVpsAizD8M4HYrNvTU2h+b1x5/8Kv9Hrb9DevE=
github.com/clubpay/ronykit/kit v0.18.4/go.mod h1:CmkwWBo83JY5T9w+9L6HOFPpVm68qEjolEhIuFOH0/s=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.4 h1:DmCKNab1w18zO8d8zTiGVUBgeQs49ylienYvM6QeDh0=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.18.4/go.mod h1:teSFdgqIGBlrKCkdJcKURtSD2beAWoe/ko+ziaupwm8=
github.com/clubpay/ronykit/stub v0.18.4 h1:+/CkMadqZk+5zGw+wawa+1x0ekR0RNpSTC2O91fY0Fo=
github.com/clubpay/ronykit/stub v0.18.4/go.mod h1:ZuVANOPrFlEzbtEFpOHoyybr8CGrVN4hp28VAjNcYVw=
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=
Expand Down
2 changes: 1 addition & 1 deletion stub/internal/tpl/ts/stub.tstmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{{- end }}
{{- range .Fields -}}
{{- if .Embedded }} {{continue}} {{end}}
{{index (strSplit (.Tag.Get "json") ",") 0}}{{ if .Optional}}?{{end}}: {{tsType .Element.RType}}
{{index (strSplit (.Tag.Get "json") ",") 0}}{{ if .Tag.OmitEmpty}}?{{end}}: {{tsType .Element.RType}}{{ if (and .Optional (not .Tag.OmitEmpty)) }}| null{{end}}
{{- end }}
}
{{- end }}
Expand Down

0 comments on commit 27acddb

Please sign in to comment.