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

internal: specify the example ID within the generated snippet #3684

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 29 additions & 40 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion generators/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@fern-api/fs-utils": "workspace:*",
"@fern-api/logger": "workspace:*",
"@fern-api/logging-execa": "workspace:*",
"@fern-fern/generator-exec-sdk": "^0.0.837",
"@fern-fern/generator-exec-sdk": "^0.0.845",
"@fern-fern/ir-sdk": "0.0.3294",
"tmp-promise": "^3.0.3",
"zod": "^3.22.3"
Expand Down
2 changes: 1 addition & 1 deletion generators/csharp/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@fern-api/fern-csharp-model": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-fern/generator-exec-sdk": "^0.0.837",
"@fern-fern/generator-exec-sdk": "^0.0.845",
"@fern-fern/ir-sdk": "0.0.3318",
"lodash-es": "^4.17.21",
"zod": "^3.22.3"
Expand Down
2 changes: 1 addition & 1 deletion generators/go/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.22.0
0.22.1
2 changes: 1 addition & 1 deletion generators/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/fern-api/fern-go
go 1.19

require (
github.com/fern-api/generator-exec-go v0.0.817
github.com/fern-api/generator-exec-go v0.0.854
github.com/google/uuid v1.6.0
github.com/hmdsefi/gograph v0.4.0
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions generators/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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=
github.com/fern-api/generator-exec-go v0.0.817 h1:Vywp/rITxqSC2+g6hsnIMgapqwD6id5RlbqoVkSKmRw=
github.com/fern-api/generator-exec-go v0.0.817/go.mod h1:Z7V5lZnaD8PblJAynK087662md2wBNhQcAl+W6IIydw=
github.com/fern-api/generator-exec-go v0.0.854 h1:fqddyPblGrmzSlioRKixOF4rbPO4e+Txllg8rNOpcwI=
github.com/fern-api/generator-exec-go v0.0.854/go.mod h1:Z7V5lZnaD8PblJAynK087662md2wBNhQcAl+W6IIydw=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hmdsefi/gograph v0.4.0 h1:eiWprSQ+lSogrcay0yvgvJWEMsVT/VsZ+NR4uW2k+2k=
Expand Down
51 changes: 27 additions & 24 deletions generators/go/internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,32 +742,35 @@ func maybeWriteSnippets(
}
var endpoints []*generatorexec.Endpoint
for _, generatedEndpoint := range generatedClient.Endpoints {
client, err := ast.NewSourceCodeBuilder(generatedEndpoint.Snippet).BuildSnippet()
if err != nil {
// Log the warning and continue. We don't want to fail generation just
// because there's a bug in the snippet generator.
_ = coordinator.Log(
generatorexec.LogLevelWarn,
fmt.Sprintf(
"Failed to generate snippet for endpoint %s %q: %v",
generatedEndpoint.Identifier.Method,
generatedEndpoint.Identifier.Path,
err,
),
for _, generatedSnippet := range generatedEndpoint.Snippets {
client, err := ast.NewSourceCodeBuilder(generatedSnippet.Snippet).BuildSnippet()
if err != nil {
// Log the warning and continue. We don't want to fail generation just
// because there's a bug in the snippet generator.
_ = coordinator.Log(
generatorexec.LogLevelWarn,
fmt.Sprintf(
"Failed to generate snippet for endpoint %s %q: %v",
generatedEndpoint.Identifier.Method,
generatedEndpoint.Identifier.Path,
err,
),
)
continue
}
endpoints = append(
endpoints,
&generatorexec.Endpoint{
Id: generatedEndpoint.Identifier,
ExampleIdentifier: generatedSnippet.ExampleIdentifier,
Snippet: generatorexec.NewEndpointSnippetFromGo(
&generatorexec.GoEndpointSnippet{
Client: client,
},
),
},
)
continue
}
endpoints = append(
endpoints,
&generatorexec.Endpoint{
Id: generatedEndpoint.Identifier,
Snippet: generatorexec.NewEndpointSnippetFromGo(
&generatorexec.GoEndpointSnippet{
Client: client,
},
),
},
)
}
// Sort the endpoints for deterministic results.
sort.Slice(
Expand Down
62 changes: 42 additions & 20 deletions generators/go/internal/generator/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,12 @@ type GeneratedClient struct {

type GeneratedEndpoint struct {
Identifier *generatorexec.EndpointIdentifier
Snippet ast.Expr
Snippets []*GeneratedSnippet
}

type GeneratedSnippet struct {
ExampleIdentifier *string
Snippet ast.Expr
}

// WriteClient writes a client for interacting with the given service.
Expand Down Expand Up @@ -1506,19 +1511,18 @@ func NewGeneratedClient(
if len(endpoint.Examples) == 0 {
continue
}
example := exampleEndpointCallFromEndpointExample(endpoint.Examples[0])
if example == nil {
generatedEndpoint := newGeneratedEndpoint(
f,
fernFilepath,
rootClientInstantiation,
endpoint,
)
if generatedEndpoint == nil {
continue
}
generatedEndpoints = append(
generatedEndpoints,
newGeneratedEndpoint(
f,
fernFilepath,
rootClientInstantiation,
endpoint,
example,
),
generatedEndpoint,
)
}
return &GeneratedClient{
Expand Down Expand Up @@ -1552,17 +1556,27 @@ func newGeneratedEndpoint(
fernFilepath *ir.FernFilepath,
rootClientInstantiation *ast.AssignStmt,
endpoint *ir.HttpEndpoint,
example *ir.ExampleEndpointCall,
) *GeneratedEndpoint {
return &GeneratedEndpoint{
Identifier: endpointToIdentifier(endpoint),
Snippet: newEndpointSnippet(
var snippets []*GeneratedSnippet
for _, endpointExample := range endpoint.Examples {
example := exampleEndpointCallFromEndpointExample(endpointExample)
snippet := newEndpointSnippet(
f,
fernFilepath,
rootClientInstantiation,
endpoint,
example,
),
)

snippets = append(snippets, snippet)
}

if len(snippets) == 0 {
return nil
}
return &GeneratedEndpoint{
Identifier: endpointToIdentifier(endpoint),
Snippets: snippets,
}
}

Expand Down Expand Up @@ -1610,7 +1624,7 @@ func newEndpointSnippet(
rootClientInstantiation *ast.AssignStmt,
endpoint *ir.HttpEndpoint,
example *ir.ExampleEndpointCall,
) *ast.Block {
) *GeneratedSnippet {
methodName := getEndpointMethodName(fernFilepath, endpoint)
parameters := getEndpointParameters(
f,
Expand Down Expand Up @@ -1645,10 +1659,18 @@ func newEndpointSnippet(
call,
},
}
return &ast.Block{
Exprs: []ast.Expr{
rootClientInstantiation,
endpointCall,

var exampleId *string
if example.Name != nil {
exampleId = &example.Name.OriginalName
}
return &GeneratedSnippet{
ExampleIdentifier: exampleId,
Snippet: &ast.Block{
Exprs: []ast.Expr{
rootClientInstantiation,
endpointCall,
},
},
}
}
Expand Down
4 changes: 4 additions & 0 deletions generators/go/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- ## Unreleased -->

## [0.22.1 - 2024-05-23]

- Internal: SDK snippets now specify their example ID and we create a snippet per example

## [0.22.0 - 2024-05-21]

- Feature: Extra properties decoded from response objects are retained and accessible via the
Expand Down
19 changes: 9 additions & 10 deletions generators/python/poetry.lock

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

2 changes: 1 addition & 1 deletion generators/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = []
python = "^3.8"
pydantic = "^1.9.2,<=1.10.14"
typer = {extras = ["all"], version = "^0.6.1"}
fern-fern-generator-exec-sdk = {version = "0.0.831", source = "fern-prod"}
fern-fern-generator-exec-sdk = {version = "0.0.846", source = "fern-prod"}
ordered-set = "^4.1.0"
fern-fern-fdr-sdk = {version = "0.0.5435", source = "fern-prod"}
fern-fern-ir-v39 = "^0.0.3473"
Expand Down
6 changes: 5 additions & 1 deletion generators/python/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.5.3] - 2024-05-23

- Internal: SDK snippets now specify their example ID and we create a snippet per example

## [2.5.2] - 2024-05-23

- Fix: Support `list` SDK method names instead of defaulting to `list_`.
- Fix: Support `list` SDK method names instead of defaulting to `list_`.

## [2.5.1-rc0] - 2024-05-23

Expand Down
2 changes: 1 addition & 1 deletion generators/python/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.2
2.5.3
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ def _create_class_declaration(self, *, is_async: bool) -> AST.ClassDeclaration:
self._is_default_body_parameter_used = True

if generated_endpoint_function.snippet is not None:
if is_async:
self._snippet_registry.register_async_client_endpoint_snippet(
endpoint=endpoint, expr=generated_endpoint_function.snippet
)
else:
self._snippet_registry.register_sync_client_endpoint_snippet(
endpoint=endpoint, expr=generated_endpoint_function.snippet
)
for snippet in generated_endpoint_function.snippet:
if is_async:
self._snippet_registry.register_async_client_endpoint_snippet(
endpoint=endpoint, expr=snippet.snippet, example_id=snippet.example_id
)
else:
self._snippet_registry.register_sync_client_endpoint_snippet(
endpoint=endpoint, expr=snippet.snippet, example_id=snippet.example_id
)

return class_declaration

Expand Down
Loading
Loading