Skip to content

Commit

Permalink
feat(go): Dynamic snippets support option.WithBaseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney committed Dec 11, 2024
1 parent f5529b1 commit bcc60a9
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 11 deletions.
4 changes: 2 additions & 2 deletions generators/go-v2/ast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
"@fern-api/browser-compatible-base-generator": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/path-utils": "workspace:*",
"@fern-fern/ir-sdk": "^53.23.0",
"@fern-fern/ir-sdk": "^53.24.0",
"zod": "^3.22.3"
},
"devDependencies": {
"@fern-api/browser-compatible-base-generator": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/path-utils": "workspace:*",
"@fern-fern/ir-sdk": "^53.23.0",
"@fern-fern/ir-sdk": "^53.24.0",
"@types/jest": "^29.5.12",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
Expand Down
2 changes: 1 addition & 1 deletion generators/go-v2/dynamic-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@fern-api/go-ast": "workspace:*",
"@fern-api/go-formatter": "workspace:*",
"@fern-api/path-utils": "workspace:*",
"@fern-fern/ir-sdk": "^53.23.0",
"@fern-fern/ir-sdk": "^53.24.0",
"@types/jest": "^29.5.12",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
Expand Down
71 changes: 71 additions & 0 deletions generators/go-v2/dynamic-snippets/src/EndpointSnippetGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ export class EndpointSnippetGenerator {
snippet: DynamicSnippets.EndpointSnippetRequest;
}): go.AstNode[] {
const args: go.AstNode[] = [];
const baseUrlArg = this.getConstructorBaseUrlArg({
baseUrl: snippet.baseUrl,
environment: snippet.environment
});
if (baseUrlArg != null) {
args.push(baseUrlArg);
}
if (endpoint.auth != null) {
if (snippet.auth != null) {
args.push(this.getConstructorAuthArg({ auth: endpoint.auth, values: snippet.auth }));
Expand Down Expand Up @@ -187,6 +194,70 @@ export class EndpointSnippetGenerator {
});
}

private getConstructorBaseUrlArg({
baseUrl,
environment
}: {
baseUrl: string | undefined;
environment: DynamicSnippets.EnvironmentValues | undefined;
}): go.AstNode | undefined {
const baseUrlArg = this.getBaseUrlArg({ baseUrl, environment });
if (baseUrlArg == null) {
return undefined;
}
return go.codeblock((writer) => {
writer.writeNode(
go.invokeFunc({
func: go.typeReference({
name: "WithBaseURL",
importPath: this.context.getOptionImportPath()
}),
arguments_: [baseUrlArg]
})
);
});
}

private getBaseUrlArg({
baseUrl,
environment
}: {
baseUrl: string | undefined;
environment: DynamicSnippets.EnvironmentValues | undefined;
}): go.AstNode | undefined {
if (baseUrl != null && environment != null) {
this.context.errors.add({
severity: Severity.Critical,
message: "Cannot specify both baseUrl and environment options"
});
return undefined;
}
if (baseUrl != null) {
return go.TypeInstantiation.string(baseUrl);
}
if (environment != null) {
if (this.context.isSingleEnvironmentID(environment)) {
const typeReference = this.context.getEnvironmentTypeReferenceFromID(environment);
if (typeReference == null) {
this.context.errors.add({
severity: Severity.Warning,
message: `Environment "${environment}" was not found`
});
return undefined;
}
return go.TypeInstantiation.reference(typeReference);
}
if (this.context.isMultiEnvironmentValues(environment)) {
this.context.errors.add({
severity: Severity.Warning,
message:
"The Go SDK doesn't support a multi-environment client option yet; use the baseUrl option instead"
});
}
}
return undefined;
}

private getConstructorBearerAuthArg({
auth,
values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ func do() () {
"
`;

exports[`imdb (sync) > GET /movies/{movieId} w/ baseURL 1`] = `
"package example
import (
client "github.com/acme/acme-go/client"
option "github.com/acme/acme-go/option"
context "context"
)
func do() () {
client := client.NewClient(
option.WithBaseURL(
"http://localhost:8080",
),
option.WithToken(
"<YOUR_API_KEY>",
),
)
client.Imdb.GetMovie(
context.TODO(),
"movie_xyz",
)
}
"
`;

exports[`imdb (sync) > GET /movies/{movieId} w/ exportedClientName 1`] = `
"package example
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`single-url-environment-default > custom baseURL 1`] = `
"package example
import (
context "context"
client "github.com/acme/acme-go/client"
option "github.com/acme/acme-go/option"
)
func do() {
client := client.NewClient(
option.WithBaseURL(
"http://localhost:8080",
),
option.WithToken(
"<YOUR_API_KEY>",
),
)
client.Dummy.GetDummy(
context.TODO(),
)
}
"
`;

exports[`single-url-environment-default > invalid baseURL and environment 1`] = `
[
{
"message": "Cannot specify both baseUrl and environment options",
"path": [],
"severity": "CRITICAL",
},
]
`;

exports[`single-url-environment-default > invalid environment 1`] = `
[
{
"message": "Environment "Unrecognized" was not found",
"path": [],
"severity": "WARNING",
},
]
`;

exports[`single-url-environment-default > production environment 1`] = `
"package example
import (
context "context"
acme "github.com/acme/acme-go"
client "github.com/acme/acme-go/client"
option "github.com/acme/acme-go/option"
)
func do() {
client := client.NewClient(
option.WithBaseURL(
acme.Environments.Production,
),
option.WithToken(
"<YOUR_API_KEY>",
),
)
client.Dummy.GetDummy(
context.TODO(),
)
}
"
`;

exports[`single-url-environment-default > staging environment 1`] = `
"package example
import (
context "context"
acme "github.com/acme/acme-go"
client "github.com/acme/acme-go/client"
option "github.com/acme/acme-go/option"
)
func do() {
client := client.NewClient(
option.WithBaseURL(
acme.Environments.Staging,
),
option.WithToken(
"<YOUR_API_KEY>",
),
)
client.Dummy.GetDummy(
context.TODO(),
)
}
"
`;
10 changes: 10 additions & 0 deletions generators/go-v2/dynamic-snippets/src/__test__/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("examples", () => {
method: "GET",
path: "/metadata"
},
baseUrl: undefined,
environment: undefined,
auth: AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
Expand All @@ -35,6 +37,8 @@ describe("examples", () => {
method: "GET",
path: "/metadata"
},
baseUrl: undefined,
environment: undefined,
auth: AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
Expand All @@ -56,6 +60,8 @@ describe("examples", () => {
method: "POST",
path: "/movie"
},
baseUrl: undefined,
environment: undefined,
auth: AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
Expand Down Expand Up @@ -89,6 +95,8 @@ describe("examples", () => {
method: "POST",
path: "/big-entity"
},
baseUrl: undefined,
environment: undefined,
auth: AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
Expand Down Expand Up @@ -128,6 +136,8 @@ describe("examples (errors)", () => {
auth: AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
baseUrl: undefined,
environment: undefined,
pathParameters: undefined,
queryParameters: undefined,
headers: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("exhaustive", () => {
method: "POST",
path: "/container/list-of-primitives"
},
baseUrl: undefined,
environment: undefined,
auth: dynamic.AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
Expand All @@ -30,6 +32,8 @@ describe("exhaustive", () => {
method: "POST",
path: "/container/list-of-objects"
},
baseUrl: undefined,
environment: undefined,
auth: dynamic.AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
Expand Down Expand Up @@ -71,6 +75,8 @@ describe("exhaustive (errors)", () => {
method: "POST",
path: "/container/list-of-objects"
},
baseUrl: undefined,
environment: undefined,
auth: dynamic.AuthValues.bearer({
token: "<YOUR_API_KEY>"
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("file-upload (success)", () => {
method: "POST",
path: "/"
},
baseUrl: undefined,
environment: undefined,
auth: undefined,
pathParameters: undefined,
queryParameters: undefined,
Expand All @@ -30,6 +32,8 @@ describe("file-upload (success)", () => {
method: "POST",
path: "/just-file"
},
baseUrl: undefined,
environment: undefined,
auth: undefined,
pathParameters: undefined,
queryParameters: undefined,
Expand All @@ -46,6 +50,8 @@ describe("file-upload (success)", () => {
method: "POST",
path: "/just-file-with-query-params"
},
baseUrl: undefined,
environment: undefined,
auth: undefined,
pathParameters: undefined,
queryParameters: {
Expand Down
Loading

0 comments on commit bcc60a9

Please sign in to comment.