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

feat(go): Add configurable exportedClientName #5003

Merged
merged 2 commits into from
Oct 25, 2024
Merged
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
4 changes: 4 additions & 0 deletions fern/pages/changelogs/go-sdk/2024-10-25.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 0.28.0
**`(feat):`** Add support for the exportedClientName configuration, which can be used to customize the generated client name and constructor included in snippets.
Note that this configuration option assumes that the SDK includes a hand-written client constructor defined in the client package.

1 change: 1 addition & 0 deletions generators/go/cmd/fern-go-fiber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
config.SnippetFilepath,
config.ImportPath,
config.PackageName,
config.ExportedClientName,
config.UnionVersion,
config.Module,
)
Expand Down
1 change: 1 addition & 0 deletions generators/go/cmd/fern-go-model/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
config.SnippetFilepath,
config.ImportPath,
config.PackageName,
config.ExportedClientName,
config.UnionVersion,
config.Module,
)
Expand Down
1 change: 1 addition & 0 deletions generators/go/cmd/fern-go-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
config.SnippetFilepath,
config.ImportPath,
config.PackageName,
config.ExportedClientName,
config.UnionVersion,
config.Module,
)
Expand Down
3 changes: 3 additions & 0 deletions generators/go/internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Config struct {
IrFilepath string
SnippetFilepath string
ImportPath string
ExportedClientName string
PackageName string
UnionVersion string
Module *generator.ModuleConfig
Expand Down Expand Up @@ -211,6 +212,7 @@ func newConfig(configFilename string) (*Config, error) {
SnippetFilepath: snippetFilepath,
ImportPath: customConfig.ImportPath,
PackageName: customConfig.PackageName,
ExportedClientName: customConfig.ExportedClientName,
UnionVersion: customConfig.UnionVersion,
Module: moduleConfig,
Writer: writerConfig,
Expand Down Expand Up @@ -257,6 +259,7 @@ type customConfig struct {
AlwaysSendRequiredProperties bool `json:"alwaysSendRequiredProperties,omitempty"`
ImportPath string `json:"importPath,omitempty"`
PackageName string `json:"packageName,omitempty"`
ExportedClientName string `json:"exportedClientName,omitempty"`
UnionVersion string `json:"union,omitempty"`
Module *moduleConfig `json:"module,omitempty"`
}
Expand Down
5 changes: 4 additions & 1 deletion generators/go/internal/generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type Config struct {
IRFilepath string
SnippetFilepath string
ImportPath string
UnionVersion UnionVersion
PackageName string
ExportedClientName string
UnionVersion UnionVersion

// If not specified, a go.mod and go.sum will not be generated.
ModuleConfig *ModuleConfig
Expand Down Expand Up @@ -62,6 +63,7 @@ func NewConfig(
snippetFilepath string,
importPath string,
packageName string,
exportedClientName string,
unionVersion string,
moduleConfig *ModuleConfig,
) (*Config, error) {
Expand All @@ -82,6 +84,7 @@ func NewConfig(
SnippetFilepath: snippetFilepath,
ImportPath: importPath,
PackageName: packageName,
ExportedClientName: exportedClientName,
UnionVersion: uv,
ModuleConfig: moduleConfig,
}, nil
Expand Down
14 changes: 14 additions & 0 deletions generators/go/internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
const (
// packageDocsFilename represents the standard package documentation filename.
packageDocsFilename = "doc.go"

// defaultExportedClientName is the default name for the generated client.
defaultExportedClientName = "Client"
)

// Mode is an enum for different generator modes (i.e. types, client, etc).
Expand Down Expand Up @@ -189,6 +192,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
}
}
}
exportedClientName := getExportedClientName(ir, g.config.ExportedClientName)
rootPackageName := getRootPackageName(ir, g.config.PackageName)
cycleInfo, err := cycleInfoFromIR(ir, g.config.ImportPath)
if err != nil {
Expand Down Expand Up @@ -370,6 +374,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
generatedAuth,
generatedEnvironment,
exportedClientName,
)
if len(ir.IdempotencyHeaders) > 0 {
fileInfo = fileInfoForIdempotentRequestOptionsDefinition()
Expand Down Expand Up @@ -1491,6 +1496,15 @@ func getRootPackageName(ir *fernir.IntermediateRepresentation, packageNameOverri
return strings.ToLower(ir.ApiName.CamelCase.SafeName)
}

// getExportedClientName returns the exported client name. This is configurable so that
// users can customize how snippets are rendered, but it has no impact on the generated code.
func getExportedClientName(ir *fernir.IntermediateRepresentation, exportedClientNameOverride string) string {
if exportedClientNameOverride != "" {
return exportedClientNameOverride
}
return defaultExportedClientName
}

// generatorexecEndpointSnippetToString returns the string representation of the given
// endpoint snippet.
//
Expand Down
3 changes: 2 additions & 1 deletion generators/go/internal/generator/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,7 @@ func generatedClientInstantiation(
baseImportPath string,
generatedAuth *GeneratedAuth,
generatedEnvironment *GeneratedEnvironment,
exportedClientName string,
) *ast.AssignStmt {
var parameters []ast.Expr
if generatedAuth != nil {
Expand All @@ -1989,7 +1990,7 @@ func generatedClientInstantiation(
Right: []ast.Expr{
ast.NewCallExpr(
ast.NewImportedReference(
"NewClient",
fmt.Sprintf("New%s", exportedClientName),
packagePathToImportPath(baseImportPath, packagePathForClient(new(ir.FernFilepath))),
),
parameters,
Expand Down
10 changes: 10 additions & 0 deletions generators/go/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
- version: 0.28.0
changelogEntry:
- type: feat
summary: >-
Add support for the exportedClientName configuration, which can be used to customize
the generated client name and constructor included in snippets.

Note that this configuration option assumes that the SDK includes a hand-written client
constructor defined in the client package.
irVersion: 40
- version: 0.27.0
changelogEntry:
- type: feat
Expand Down

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

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

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

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

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

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

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

Loading
Loading