-
Notifications
You must be signed in to change notification settings - Fork 303
feat(go): add clientDefault support to Go SDK generator #15215
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
Changes from all commits
16f27ab
f0a731b
80849a3
c806409
ddc03bc
14bfeb5
d1255fb
7848ab7
1080c47
187a3a5
0d1bcd9
06af2d2
a0829b7
6548f65
4459876
a35e871
5dc28ef
d584b11
2e65427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { assertNever } from "@fern-api/core-utils"; | |||||||||||||||||||||||||||||||||
| import { go } from "@fern-api/go-ast"; | ||||||||||||||||||||||||||||||||||
| import { FernIr } from "@fern-fern/ir-sdk"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import { isPlainStringType } from "../../authUtils.js"; | ||||||||||||||||||||||||||||||||||
| import { SdkGeneratorContext } from "../../SdkGeneratorContext.js"; | ||||||||||||||||||||||||||||||||||
| import { AbstractEndpointGenerator } from "../AbstractEndpointGenerator.js"; | ||||||||||||||||||||||||||||||||||
| import { EndpointSignatureInfo } from "../EndpointSignatureInfo.js"; | ||||||||||||||||||||||||||||||||||
|
|
@@ -620,19 +621,48 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator { | |||||||||||||||||||||||||||||||||
| const pathSuffix = this.getPathSuffix({ endpoint }); | ||||||||||||||||||||||||||||||||||
| const baseUrl = pathSuffix.length === 0 ? "baseURL" : `baseURL + "/${pathSuffix}"`; | ||||||||||||||||||||||||||||||||||
| return go.codeblock((writer) => { | ||||||||||||||||||||||||||||||||||
| writer.write("endpointURL := "); | ||||||||||||||||||||||||||||||||||
| if (endpoint.allPathParameters.length === 0) { | ||||||||||||||||||||||||||||||||||
| writer.write("endpointURL := "); | ||||||||||||||||||||||||||||||||||
| writer.write(baseUrl); | ||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // Apply clientDefault fallback for path parameters before URL encoding. | ||||||||||||||||||||||||||||||||||
| // Use local variables to avoid mutating the caller's request struct when | ||||||||||||||||||||||||||||||||||
| // path params come from a wrapped request (e.g., request.Region). | ||||||||||||||||||||||||||||||||||
| const pathParamLocalVars: Record<string, string> = {}; | ||||||||||||||||||||||||||||||||||
| for (const pathParameter of endpoint.allPathParameters) { | ||||||||||||||||||||||||||||||||||
| if (pathParameter.clientDefault != null && isPlainStringType(pathParameter.valueType)) { | ||||||||||||||||||||||||||||||||||
| const ref = signature.pathParameterReferences[getOriginalName(pathParameter.name)]; | ||||||||||||||||||||||||||||||||||
| if (ref != null) { | ||||||||||||||||||||||||||||||||||
| const localVar = `_${this.context.getFieldName(pathParameter.name)}`; | ||||||||||||||||||||||||||||||||||
| writer.writeLine(`${localVar} := ${ref}`); | ||||||||||||||||||||||||||||||||||
| writer.writeLine(`if ${localVar} == "" {`); | ||||||||||||||||||||||||||||||||||
| writer.indent(); | ||||||||||||||||||||||||||||||||||
| writer.write(`${localVar} = `); | ||||||||||||||||||||||||||||||||||
| writer.writeNode(this.context.getLiteralValue(pathParameter.clientDefault)); | ||||||||||||||||||||||||||||||||||
| writer.newLine(); | ||||||||||||||||||||||||||||||||||
| writer.dedent(); | ||||||||||||||||||||||||||||||||||
| writer.writeLine("}"); | ||||||||||||||||||||||||||||||||||
| pathParamLocalVars[getOriginalName(pathParameter.name)] = localVar; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| const pathParameterReferences: go.AstNode[] = []; | ||||||||||||||||||||||||||||||||||
| for (const pathParameter of endpoint.allPathParameters) { | ||||||||||||||||||||||||||||||||||
| const pathParameterReference = signature.pathParameterReferences[getOriginalName(pathParameter.name)]; | ||||||||||||||||||||||||||||||||||
| const originalName = getOriginalName(pathParameter.name); | ||||||||||||||||||||||||||||||||||
| // Use the local variable if we created one for clientDefault | ||||||||||||||||||||||||||||||||||
| const localVar = pathParamLocalVars[originalName]; | ||||||||||||||||||||||||||||||||||
| if (localVar != null) { | ||||||||||||||||||||||||||||||||||
| pathParameterReferences.push(go.codeblock(localVar)); | ||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| const pathParameterReference = signature.pathParameterReferences[originalName]; | ||||||||||||||||||||||||||||||||||
| if (pathParameterReference == null) { | ||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||
| pathParameterReferences.push(go.codeblock(pathParameterReference)); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| writer.write("endpointURL := "); | ||||||||||||||||||||||||||||||||||
| writer.writeNode(this.context.callEncodeUrl([go.codeblock(baseUrl), ...pathParameterReferences])); | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
@@ -725,7 +755,7 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator { | |||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // extract and populate defaults | ||||||||||||||||||||||||||||||||||
| // extract and populate defaults (from type-level defaults and clientDefault) | ||||||||||||||||||||||||||||||||||
| const defaults = []; | ||||||||||||||||||||||||||||||||||
| if (this.context.customConfig.useDefaultRequestParameterValues) { | ||||||||||||||||||||||||||||||||||
| for (const queryParameter of endpoint.queryParameters) { | ||||||||||||||||||||||||||||||||||
|
|
@@ -739,6 +769,33 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator { | |||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // Track wire values that already have type-level defaults | ||||||||||||||||||||||||||||||||||
| const defaultedWireValues = new Set<string>(); | ||||||||||||||||||||||||||||||||||
| if (this.context.customConfig.useDefaultRequestParameterValues) { | ||||||||||||||||||||||||||||||||||
| for (const queryParameter of endpoint.queryParameters) { | ||||||||||||||||||||||||||||||||||
| if (this.extractDefaultValue(queryParameter.valueType) != null) { | ||||||||||||||||||||||||||||||||||
| defaultedWireValues.add(getWireValue(queryParameter.name)); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // Add clientDefault entries for query params that don't already have a type-level default. | ||||||||||||||||||||||||||||||||||
| // Unlike path params and headers (which use the == "" zero-value pattern and require | ||||||||||||||||||||||||||||||||||
| // string types), query params use a defaults map so any literal type works here. | ||||||||||||||||||||||||||||||||||
| for (const queryParameter of endpoint.queryParameters) { | ||||||||||||||||||||||||||||||||||
| if (queryParameter.clientDefault != null) { | ||||||||||||||||||||||||||||||||||
| const wireValue = getWireValue(queryParameter.name); | ||||||||||||||||||||||||||||||||||
| if (!defaultedWireValues.has(wireValue)) { | ||||||||||||||||||||||||||||||||||
| const literalString = | ||||||||||||||||||||||||||||||||||
| queryParameter.clientDefault.type === "string" | ||||||||||||||||||||||||||||||||||
| ? queryParameter.clientDefault.string | ||||||||||||||||||||||||||||||||||
| : String(queryParameter.clientDefault.boolean); | ||||||||||||||||||||||||||||||||||
| defaults.push({ | ||||||||||||||||||||||||||||||||||
| key: go.TypeInstantiation.string(wireValue), | ||||||||||||||||||||||||||||||||||
| value: go.TypeInstantiation.string(literalString) | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+788
to
+795
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Boolean clientDefault for query parameter produces wrong Go type in defaults map When a query parameter has a boolean
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| const defaults_map = go.TypeInstantiation.map({ | ||||||||||||||||||||||||||||||||||
| keyType: go.Type.string(), | ||||||||||||||||||||||||||||||||||
| valueType: go.Type.any(), | ||||||||||||||||||||||||||||||||||
|
|
@@ -849,9 +906,27 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator { | |||||||||||||||||||||||||||||||||
| writer.writeLine("}"); | ||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| writer.writeNode( | ||||||||||||||||||||||||||||||||||
| this.addHeaderValue({ wireValue: getWireValue(header.name), value: format.formatted }) | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| // Apply clientDefault fallback for non-optional string headers. | ||||||||||||||||||||||||||||||||||
| // Use a local variable to avoid mutating the caller's request struct. | ||||||||||||||||||||||||||||||||||
| if (header.clientDefault != null && isPlainStringType(header.valueType)) { | ||||||||||||||||||||||||||||||||||
| const localVar = `_${this.context.getFieldName(headerNameVal)}`; | ||||||||||||||||||||||||||||||||||
| writer.writeNewLineIfLastLineNot(); | ||||||||||||||||||||||||||||||||||
| writer.writeLine(`${localVar} := ${headerField}`); | ||||||||||||||||||||||||||||||||||
| writer.writeLine(`if ${localVar} == "" {`); | ||||||||||||||||||||||||||||||||||
| writer.indent(); | ||||||||||||||||||||||||||||||||||
| writer.write(`${localVar} = `); | ||||||||||||||||||||||||||||||||||
| writer.writeNode(this.context.getLiteralValue(header.clientDefault)); | ||||||||||||||||||||||||||||||||||
| writer.newLine(); | ||||||||||||||||||||||||||||||||||
| writer.dedent(); | ||||||||||||||||||||||||||||||||||
| writer.writeLine("}"); | ||||||||||||||||||||||||||||||||||
| writer.writeNode( | ||||||||||||||||||||||||||||||||||
| this.addHeaderValue({ wireValue: getWireValue(header.name), value: go.codeblock(localVar) }) | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| writer.writeNode( | ||||||||||||||||||||||||||||||||||
| this.addHeaderValue({ wireValue: getWireValue(header.name), value: format.formatted }) | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| const acceptHeader = this.getAcceptHeaderValue({ endpoint }); | ||||||||||||||||||||||||||||||||||
| if (acceptHeader != null) { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.