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 packageLayout setting #5713

Merged
merged 2 commits into from
Jan 22, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 31 additions & 0 deletions fern/pages/changelogs/go-sdk/2025-01-22.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## 0.36.0
**`(feat):`** Adds support for a few new configuration options, all of which are shown below:
```yaml - name: fern-api/fern-go-sdk
version: 0.36.0
config:
packageLayout: flat
clientName: Acme
clientConstructorName: New
```
With this, the generated SDK will all be deposited at the root of the module, and the client can be constructed like so:
```go package main
import (
"context"
"fmt"
"log"
acme "github.com/acme/acme-go"
)
func main() {
client := acme.New()
response, err := client.GetUser(
context.Background(),
&acme.GetUserRequest{
ID: "85307b0b-094b-41b5-b61d-347ca15e5da2",
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
} ```

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ export const BaseGoCustomConfigSchema = z.object({
importPath: z.string().optional(),

alwaysSendRequiredProperties: z.boolean().optional(),
clientConstructorName: z.string().optional(),
clientName: z.string().optional(),
enableExplicitNull: z.boolean().optional(),
exportedClientName: z.string().optional(),
includeLegacyClientOptions: z.boolean().optional(),
inlinePathParameters: z.boolean().optional(),
inlineFileProperties: z.boolean().optional(),
packageLayout: z.enum(["flat", "nested"]).optional(),
union: z.string().optional()
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,26 @@ export class DynamicSnippetsGeneratorContext extends AbstractDynamicSnippetsGene
}

public getClientConstructorName(): string {
if (this.customConfig?.clientConstructorName != null) {
return this.customConfig.clientConstructorName;
}
if (this.customConfig?.exportedClientName != null) {
return `New${this.customConfig.exportedClientName}`;
}
return "NewClient";
return `New${this.getClientName()}`;
}

public getClientName(): string {
if (this.customConfig?.clientName != null) {
return this.customConfig.clientName;
}
return "Client";
}

public getClientImportPath(): string {
if (this.customConfig?.packageLayout === "flat") {
return this.rootImportPath;
}
return `${this.rootImportPath}/client`;
}

Expand Down
3 changes: 3 additions & 0 deletions generators/go/cmd/fern-go-fiber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
config.Version,
config.IrFilepath,
config.SnippetFilepath,
config.ClientName,
config.ClientConstructorName,
config.ImportPath,
config.PackageName,
config.ExportedClientName,
config.PackageLayout,
config.UnionVersion,
config.Module,
)
Expand Down
3 changes: 3 additions & 0 deletions generators/go/cmd/fern-go-model/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
config.Version,
config.IrFilepath,
config.SnippetFilepath,
config.ClientName,
config.ClientConstructorName,
config.ImportPath,
config.PackageName,
config.ExportedClientName,
config.PackageLayout,
config.UnionVersion,
config.Module,
)
Expand Down
3 changes: 3 additions & 0 deletions generators/go/cmd/fern-go-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
config.Version,
config.IrFilepath,
config.SnippetFilepath,
config.ClientName,
config.ClientConstructorName,
config.ImportPath,
config.PackageName,
config.ExportedClientName,
config.PackageLayout,
config.UnionVersion,
config.Module,
)
Expand Down
9 changes: 9 additions & 0 deletions generators/go/internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ type Config struct {
Version string
IrFilepath string
SnippetFilepath string
ClientName string
ClientConstructorName string
ImportPath string
ExportedClientName string
PackageName string
PackageLayout string
UnionVersion string
Module *generator.ModuleConfig
Writer *writer.Config
Expand Down Expand Up @@ -214,9 +217,12 @@ func newConfig(configFilename string) (*Config, error) {
Version: outputVersionFromGeneratorConfig(config),
IrFilepath: config.IrFilepath,
SnippetFilepath: snippetFilepath,
ClientName: customConfig.ClientName,
ClientConstructorName: customConfig.ClientConstructorName,
ImportPath: customConfig.ImportPath,
PackageName: customConfig.PackageName,
ExportedClientName: customConfig.ExportedClientName,
PackageLayout: customConfig.PackageLayout,
UnionVersion: customConfig.UnionVersion,
Module: moduleConfig,
Writer: writerConfig,
Expand Down Expand Up @@ -263,9 +269,12 @@ type customConfig struct {
InlineFileProperties bool `json:"inlineFileProperties,omitempty"`
IncludeLegacyClientOptions bool `json:"includeLegacyClientOptions,omitempty"`
AlwaysSendRequiredProperties bool `json:"alwaysSendRequiredProperties,omitempty"`
ClientName string `json:"clientName,omitempty"`
ClientConstructorName string `json:"clientConstructorName,omitempty"`
ImportPath string `json:"importPath,omitempty"`
PackageName string `json:"packageName,omitempty"`
ExportedClientName string `json:"exportedClientName,omitempty"`
PackageLayout string `json:"packageLayout,omitempty"`
UnionVersion string `json:"union,omitempty"`
Module *moduleConfig `json:"module,omitempty"`
}
Expand Down
35 changes: 35 additions & 0 deletions generators/go/internal/generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ package generator

import "fmt"

// PackageLayout represents the different package layouts supported by the generator.
type PackageLayout uint

// Enumerates the supported package layouts.
const (
PackageLayoutUnspecified PackageLayout = iota
PackageLayoutNested
PackageLayoutFlat
)

// UnionVersion represents the different union versions supported by the generator.
type UnionVersion uint

Expand All @@ -26,9 +36,12 @@ type Config struct {
Version string
IRFilepath string
SnippetFilepath string
ClientName string
ClientConstructorName string
ImportPath string
PackageName string
ExportedClientName string
PackageLayout PackageLayout
UnionVersion UnionVersion

// If not specified, a go.mod and go.sum will not be generated.
Expand Down Expand Up @@ -65,12 +78,19 @@ func NewConfig(
version string,
irFilepath string,
snippetFilepath string,
clientName string,
clientConstructorName string,
importPath string,
packageName string,
exportedClientName string,
packageLayout string,
unionVersion string,
moduleConfig *ModuleConfig,
) (*Config, error) {
pl, err := parsePackageLayout(packageLayout)
if err != nil {
return nil, err
}
uv, err := parseUnionVersion(unionVersion)
if err != nil {
return nil, err
Expand All @@ -88,9 +108,12 @@ func NewConfig(
Version: version,
IRFilepath: irFilepath,
SnippetFilepath: snippetFilepath,
ClientName: clientName,
ClientConstructorName: clientConstructorName,
ImportPath: importPath,
PackageName: packageName,
ExportedClientName: exportedClientName,
PackageLayout: pl,
UnionVersion: uv,
ModuleConfig: moduleConfig,
}, nil
Expand All @@ -107,3 +130,15 @@ func parseUnionVersion(unionVersion string) (UnionVersion, error) {
}
return UnionVersionUnspecified, fmt.Errorf("unrecognized union version %q", unionVersion)
}

func parsePackageLayout(packageLayout string) (PackageLayout, error) {
switch packageLayout {
case "":
return PackageLayoutUnspecified, nil
case "nested":
return PackageLayoutNested, nil
case "flat":
return PackageLayoutFlat, nil
}
return PackageLayoutUnspecified, fmt.Errorf("unrecognized package layout %q", packageLayout)
}
6 changes: 3 additions & 3 deletions generators/go/internal/generator/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func (f *fileWriter) WriteFiberRequestType(fernFilepath *ir.FernFilepath, endpoi
)
continue
}
goType := typeReferenceToGoType(header.ValueType, f.types, f.scope, f.baseImportPath, importPath, false)
goType := typeReferenceToGoType(header.ValueType, f.types, f.scope, f.baseImportPath, importPath, false, f.packageLayout)
f.P(header.Name.Name.PascalCase.UnsafeName, " ", goType, " `header:\"", header.Name.Name.OriginalName, "\"`")
}
for _, queryParam := range endpoint.QueryParameters {
value := typeReferenceToGoType(queryParam.ValueType, f.types, f.scope, f.baseImportPath, importPath, false)
value := typeReferenceToGoType(queryParam.ValueType, f.types, f.scope, f.baseImportPath, importPath, false, f.packageLayout)
if queryParam.AllowMultiple {
value = fmt.Sprintf("[]%s", value)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (f *fileWriter) WriteFiberRequestType(fernFilepath *ir.FernFilepath, endpoi
)
if reference := endpoint.RequestBody.Reference; reference != nil {
referenceType = strings.TrimPrefix(
typeReferenceToGoType(reference.RequestBodyType, f.types, f.scope, f.baseImportPath, importPath, false),
typeReferenceToGoType(reference.RequestBodyType, f.types, f.scope, f.baseImportPath, importPath, false, f.packageLayout),
"*",
)
referenceIsPointer = reference.RequestBodyType.Named != nil && isPointer(f.types[reference.RequestBodyType.Named.TypeId])
Expand Down
4 changes: 4 additions & 0 deletions generators/go/internal/generator/file_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type fileWriter struct {
inlinePathParameters bool
inlineFileProperties bool
unionVersion UnionVersion
packageLayout PackageLayout
scope *gospec.Scope
types map[ir.TypeId]*ir.TypeDeclaration
errors map[ir.ErrorId]*ir.ErrorDeclaration
Expand All @@ -53,6 +54,7 @@ func newFileWriter(
alwaysSendRequiredProperties bool,
inlinePathParameters bool,
inlineFileProperties bool,
packageLayout PackageLayout,
unionVersion UnionVersion,
types map[ir.TypeId]*ir.TypeDeclaration,
errors map[ir.ErrorId]*ir.ErrorDeclaration,
Expand Down Expand Up @@ -95,6 +97,7 @@ func newFileWriter(
alwaysSendRequiredProperties: alwaysSendRequiredProperties,
inlinePathParameters: inlinePathParameters,
inlineFileProperties: inlineFileProperties,
packageLayout: packageLayout,
unionVersion: unionVersion,
scope: scope,
types: types,
Expand Down Expand Up @@ -173,6 +176,7 @@ func (f *fileWriter) clone() *fileWriter {
f.alwaysSendRequiredProperties,
f.inlinePathParameters,
f.inlineFileProperties,
f.packageLayout,
f.unionVersion,
f.types,
f.errors,
Expand Down
Loading
Loading