Skip to content

Commit

Permalink
feat(go): Add configurable packageLayout setting
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney committed Jan 22, 2025
1 parent 1dc859f commit 2f21857
Show file tree
Hide file tree
Showing 366 changed files with 26,455 additions and 146 deletions.
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

0 comments on commit 2f21857

Please sign in to comment.