Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clientgen: fix handling of optionals #726

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions e2e-tests/echo_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ type NonBasicResponse struct {
RawStruct json.RawMessage

// Query
QueryString string
QueryNumber int
QueryString string
QueryNumber int
OptQueryString string
OptQueryNumber int

// Path
PathString string
Expand Down
6 changes: 4 additions & 2 deletions e2e-tests/testdata/echo/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ type NonBasicData struct {
RawStruct json.RawMessage

// Query
QueryString string `query:"string"`
QueryNumber int `query:"no"`
QueryString string `query:"string"`
QueryNumber int `query:"no"`
OptQueryNumber int `query:"optnum" encore:"optional"`
OptQueryString string `query:"optstr" encore:"optional"`

// Path Parameters
PathString string
Expand Down
44 changes: 26 additions & 18 deletions e2e-tests/testdata/echo_client/golang/client/goclient.go

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

2 changes: 2 additions & 0 deletions e2e-tests/testdata/echo_client/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ class EchoServiceClient {

const query = makeRecord({
no: String(params.QueryNumber),
optnum: params.OptQueryNumber === undefined ? undefined : String(params.OptQueryNumber),
optstr: params.OptQueryString,
string: params.QueryString,
})

Expand Down
4 changes: 4 additions & 0 deletions e2e-tests/testdata/echo_client/ts/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ export namespace echo {
QueryString: string

QueryNumber: number
OptQueryNumber?: number
OptQueryString?: string
/**
* Path Parameters
*/
Expand Down Expand Up @@ -364,6 +366,8 @@ export namespace echo {

const query = makeRecord<string, string | string[]>({
no: String(params.QueryNumber),
optnum: params.OptQueryNumber === undefined ? undefined : String(params.OptQueryNumber),
optstr: params.OptQueryString,
string: params.QueryString,
})

Expand Down
23 changes: 15 additions & 8 deletions internal/clientgen/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (js *javascript) rpcCallSite(w *indentWriter, rpc *meta.RPC, rpcPath string
dict := make(map[string]string)
for _, field := range reqEnc.HeaderParameters {
ref := js.Dot("params", field.SrcName)
dict[field.WireFormat] = js.convertBuiltinToString(field.Type.GetBuiltin(), ref)
dict[field.WireFormat] = js.convertBuiltinToString(field.Type.GetBuiltin(), ref, field.Optional)
}

w.WriteString("const headers = makeRecord(")
Expand All @@ -275,11 +275,12 @@ func (js *javascript) rpcCallSite(w *indentWriter, rpc *meta.RPC, rpcPath string
for _, field := range reqEnc.QueryParameters {
if list := field.Type.GetList(); list != nil {
dict[field.WireFormat] = js.Dot("params", field.SrcName) +
".map((v) => " + js.convertBuiltinToString(list.Elem.GetBuiltin(), "v") + ")"
".map((v) => " + js.convertBuiltinToString(list.Elem.GetBuiltin(), "v", field.Optional) + ")"
} else {
dict[field.WireFormat] = js.convertBuiltinToString(
field.Type.GetBuiltin(),
js.Dot("params", field.SrcName),
field.Optional,
)
}
}
Expand Down Expand Up @@ -552,10 +553,10 @@ if (authData) {
if list := field.Type.GetList(); list != nil {
w.WriteString(
js.Dot("authData", field.SrcName) +
".map((v) => " + js.convertBuiltinToString(list.Elem.GetBuiltin(), "v") + ")",
".map((v) => " + js.convertBuiltinToString(list.Elem.GetBuiltin(), "v", field.Optional) + ")",
)
} else {
w.WriteString(js.convertBuiltinToString(field.Type.GetBuiltin(), js.Dot("authData", field.SrcName)))
w.WriteString(js.convertBuiltinToString(field.Type.GetBuiltin(), js.Dot("authData", field.SrcName), field.Optional))
}
w.WriteString("\n")
}
Expand All @@ -565,7 +566,7 @@ if (authData) {
w.WriteString("init.headers[\"")
w.WriteString(field.WireFormat)
w.WriteString("\"] = ")
w.WriteString(js.convertBuiltinToString(field.Type.GetBuiltin(), js.Dot("authData", field.SrcName)))
w.WriteString(js.convertBuiltinToString(field.Type.GetBuiltin(), js.Dot("authData", field.SrcName), field.Optional))
w.WriteString("\n")
}
} else {
Expand Down Expand Up @@ -658,15 +659,21 @@ function mustBeSet(field, value) {
}
}

func (js *javascript) convertBuiltinToString(typ schema.Builtin, val string) string {
func (js *javascript) convertBuiltinToString(typ schema.Builtin, val string, isOptional bool) string {
var code string
switch typ {
case schema.Builtin_STRING:
return val
case schema.Builtin_JSON:
return fmt.Sprintf("JSON.stringify(%s)", val)
code = fmt.Sprintf("JSON.stringify(%s)", val)
default:
return fmt.Sprintf("String(%s)", val)
code = fmt.Sprintf("String(%s)", val)
}

if isOptional {
code = fmt.Sprintf("%s === undefined ? undefined : %s", val, code)
}
return code
}

func (js *javascript) convertStringToBuiltin(typ schema.Builtin, val string) string {
Expand Down
48 changes: 40 additions & 8 deletions internal/clientgen/testdata/expected_golang.go

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

20 changes: 19 additions & 1 deletion internal/clientgen/testdata/expected_javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,25 @@ class SvcServiceClient {
* DummyAPI is a dummy endpoint.
*/
async DummyAPI(params) {
await this.baseClient.callAPI("POST", `/svc.DummyAPI`, JSON.stringify(params))
// Convert our params into the objects we need for the request
const headers = makeRecord({
baz: params.HeaderBaz,
int: params.HeaderInt === undefined ? undefined : String(params.HeaderInt),
})

const query = makeRecord({
bar: params.QueryBar,
foo: params.QueryFoo === undefined ? undefined : String(params.QueryFoo),
})

// Construct the body with only the fields which we want encoded within the body (excluding query string or header fields)
const body = {
Foo: params.Foo,
Raw: params.Raw,
boo: params.boo,
}

await this.baseClient.callAPI("POST", `/svc.DummyAPI`, JSON.stringify(body), {headers, query})
}

async Get(params) {
Expand Down
24 changes: 23 additions & 1 deletion internal/clientgen/testdata/expected_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export namespace svc {
*/
boo: string

QueryFoo?: boolean
QueryBar?: string
HeaderBaz?: string
HeaderInt?: number
/**
* This is a multiline
* comment on the raw message!
Expand Down Expand Up @@ -224,7 +228,25 @@ export namespace svc {
* DummyAPI is a dummy endpoint.
*/
public async DummyAPI(params: Request): Promise<void> {
await this.baseClient.callAPI("POST", `/svc.DummyAPI`, JSON.stringify(params))
// Convert our params into the objects we need for the request
const headers = makeRecord<string, string>({
baz: params.HeaderBaz,
int: params.HeaderInt === undefined ? undefined : String(params.HeaderInt),
})

const query = makeRecord<string, string | string[]>({
bar: params.QueryBar,
foo: params.QueryFoo === undefined ? undefined : String(params.QueryFoo),
})

// Construct the body with only the fields which we want encoded within the body (excluding query string or header fields)
const body: Record<string, any> = {
Foo: params.Foo,
Raw: params.Raw,
boo: params.boo,
}

await this.baseClient.callAPI("POST", `/svc.DummyAPI`, JSON.stringify(body), {headers, query})
}

public async Get(params: GetRequest): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion internal/clientgen/testdata/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ type Request struct {
Bar string `json:"-"`
Baz string `json:"boo"` // Baz is better

// This is a multiline
QueryFoo bool `query:"foo" encore:"optional"`
QueryBar string `query:"bar" encore:"optional"`
HeaderBaz string `header:"baz" encore:"optional"`
HeaderInt int `header:"int" encore:"optional"`

// This is a multiline
// comment on the raw message!
Raw json.RawMessage
}
Expand Down
Loading