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 inlineFileProperties configuration #5202

Merged
merged 6 commits into from
Nov 18, 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
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 @@ -29,6 +29,7 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
includeReadme,
config.Whitelabel,
config.AlwaysSendRequiredProperties,
config.InlineFileProperties,
config.Organization,
config.Version,
config.IrFilepath,
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 @@ -29,6 +29,7 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
includeReadme,
config.Whitelabel,
config.AlwaysSendRequiredProperties,
config.InlineFileProperties,
config.Organization,
config.Version,
config.IrFilepath,
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 @@ -29,6 +29,7 @@ func run(config *cmd.Config, coordinator *coordinator.Client) ([]*generator.File
includeReadme,
config.Whitelabel,
config.AlwaysSendRequiredProperties,
config.InlineFileProperties,
config.Organization,
config.Version,
config.IrFilepath,
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 @@ -56,6 +56,7 @@ type Config struct {
IncludeLegacyClientOptions bool
Whitelabel bool
AlwaysSendRequiredProperties bool
InlineFileProperties bool
Organization string
CoordinatorURL string
CoordinatorTaskID string
Expand Down Expand Up @@ -200,6 +201,7 @@ func newConfig(configFilename string) (*Config, error) {

return &Config{
DryRun: config.DryRun,
InlineFileProperties: customConfig.InlineFileProperties,
IncludeLegacyClientOptions: customConfig.IncludeLegacyClientOptions,
EnableExplicitNull: customConfig.EnableExplicitNull,
Organization: config.Organization,
Expand Down Expand Up @@ -255,6 +257,7 @@ func readConfig(configFilename string) (*generatorexec.GeneratorConfig, error) {

type customConfig struct {
EnableExplicitNull bool `json:"enableExplicitNull,omitempty"`
InlineFileProperties bool `json:"inlineFileProperties,omitempty"`
IncludeLegacyClientOptions bool `json:"includeLegacyClientOptions,omitempty"`
AlwaysSendRequiredProperties bool `json:"alwaysSendRequiredProperties,omitempty"`
ImportPath string `json:"importPath,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions generators/go/internal/generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
IncludeReadme bool
Whitelabel bool
AlwaysSendRequiredProperties bool
InlineFileProperties bool
Organization string
Version string
IRFilepath string
Expand Down Expand Up @@ -57,6 +58,7 @@ func NewConfig(
includeReadme bool,
whitelabel bool,
alwaysSendRequiredProperties bool,
inlineFileProperties bool,
organization string,
version string,
irFilepath string,
Expand All @@ -79,6 +81,7 @@ func NewConfig(
Organization: organization,
Whitelabel: whitelabel,
AlwaysSendRequiredProperties: alwaysSendRequiredProperties,
InlineFileProperties: inlineFileProperties,
Version: version,
IRFilepath: irFilepath,
SnippetFilepath: snippetFilepath,
Expand Down
2 changes: 1 addition & 1 deletion generators/go/internal/generator/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (f *fileWriter) WriteFiberRequestType(fernFilepath *ir.FernFilepath, endpoi
}
return nil
}
requestBody, err := requestBodyToFieldDeclaration(endpoint.RequestBody, f, importPath, bodyField, includeGenericOptionals)
requestBody, err := requestBodyToFieldDeclaration(endpoint.RequestBody, f, importPath, bodyField, includeGenericOptionals, false /* inlineFileProperties */)
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions generators/go/internal/generator/file_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type fileWriter struct {
baseImportPath string
whitelabel bool
alwaysSendRequiredProperties bool
inlineFileProperties bool
unionVersion UnionVersion
scope *gospec.Scope
types map[ir.TypeId]*ir.TypeDeclaration
Expand All @@ -49,6 +50,7 @@ func newFileWriter(
baseImportPath string,
whitelabel bool,
alwaysSendRequiredProperties bool,
inlineFileProperties bool,
unionVersion UnionVersion,
types map[ir.TypeId]*ir.TypeDeclaration,
errors map[ir.ErrorId]*ir.ErrorDeclaration,
Expand Down Expand Up @@ -89,6 +91,7 @@ func newFileWriter(
baseImportPath: baseImportPath,
whitelabel: whitelabel,
alwaysSendRequiredProperties: alwaysSendRequiredProperties,
inlineFileProperties: inlineFileProperties,
unionVersion: unionVersion,
scope: scope,
types: types,
Expand Down Expand Up @@ -125,6 +128,7 @@ func (f *fileWriter) File() (*File, error) {

formatted, err := removeUnusedImports(f.filename, append(header.buffer.Bytes(), f.buffer.Bytes()...))
if err != nil {
fmt.Println(string(append(header.buffer.Bytes(), f.buffer.Bytes()...)))
return nil, err
}

Expand Down Expand Up @@ -163,6 +167,7 @@ func (f *fileWriter) clone() *fileWriter {
f.baseImportPath,
f.whitelabel,
f.alwaysSendRequiredProperties,
f.inlineFileProperties,
f.unionVersion,
f.types,
f.errors,
Expand Down
62 changes: 56 additions & 6 deletions generators/go/internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (g *Generator) Generate(mode Mode) ([]*File, error) {
}

func (g *Generator) generateModelTypes(ir *fernir.IntermediateRepresentation, mode Mode, rootPackageName string) ([]*File, error) {
fileInfoToTypes, err := fileInfoToTypes(rootPackageName, ir.Types, ir.Services, ir.ServiceTypeReferenceInfo)
fileInfoToTypes, err := fileInfoToTypes(rootPackageName, ir.Types, ir.Services, ir.ServiceTypeReferenceInfo, g.config.InlineFileProperties)
if err != nil {
return nil, err
}
Expand All @@ -144,6 +144,7 @@ func (g *Generator) generateModelTypes(ir *fernir.IntermediateRepresentation, mo
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -167,6 +168,7 @@ func (g *Generator) generateModelTypes(ir *fernir.IntermediateRepresentation, mo
typeToGenerate.Service.Headers,
ir.IdempotencyHeaders,
g.config.EnableExplicitNull,
g.config.InlineFileProperties,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -251,6 +253,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
"",
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
nil,
nil,
Expand All @@ -270,6 +273,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
"",
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
nil,
nil,
Expand Down Expand Up @@ -312,6 +316,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand Down Expand Up @@ -341,6 +346,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -364,6 +370,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand Down Expand Up @@ -392,6 +399,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -412,6 +420,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -435,6 +444,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -457,6 +467,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -480,13 +491,15 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
files = append(files, newFileParamFile(g.coordinator, rootPackageName, generatedNames))
files = append(files, newHttpCoreFile(g.coordinator))
files = append(files, newHttpInternalFile(g.coordinator))
files = append(files, newMultipartFile(g.coordinator))
files = append(files, newMultipartTestFile(g.coordinator))
files = append(files, newPointerFile(g.coordinator, rootPackageName, generatedNames))
files = append(files, newQueryFile(g.coordinator))
files = append(files, newQueryTestFile(g.coordinator))
files = append(files, newRetrierFile(g.coordinator, g.config.ImportPath))
files = append(files, newRetrierTestFile(g.coordinator, g.config.ImportPath))
if needsFileUploadHelpers(ir) {
files = append(files, newMultipartFile(g.coordinator))
files = append(files, newMultipartTestFile(g.coordinator))
}
if ir.SdkConfig.HasStreamingEndpoints {
files = append(files, newStreamFile(g.coordinator))
files = append(files, newStreamerFile(g.coordinator, g.config.ImportPath))
Expand All @@ -508,6 +521,7 @@ func (g *Generator) generate(ir *fernir.IntermediateRepresentation, mode Mode) (
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand Down Expand Up @@ -661,6 +675,7 @@ func (g *Generator) generateService(
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -677,6 +692,7 @@ func (g *Generator) generateService(
ir.ErrorDiscriminationStrategy,
originalFernFilepath,
rootClientInstantiation,
g.config.InlineFileProperties,
)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -705,6 +721,7 @@ func (g *Generator) generateServiceWithoutEndpoints(
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -721,6 +738,7 @@ func (g *Generator) generateServiceWithoutEndpoints(
ir.ErrorDiscriminationStrategy,
originalFernFilepath,
rootClientInstantiation,
g.config.InlineFileProperties,
); err != nil {
return nil, err
}
Expand All @@ -743,6 +761,7 @@ func (g *Generator) generateRootServiceWithoutEndpoints(
g.config.ImportPath,
g.config.Whitelabel,
g.config.AlwaysSendRequiredProperties,
g.config.InlineFileProperties,
g.config.UnionVersion,
ir.Types,
ir.Errors,
Expand All @@ -759,6 +778,7 @@ func (g *Generator) generateRootServiceWithoutEndpoints(
ir.ErrorDiscriminationStrategy,
fernFilepath,
rootClientInstantiation,
g.config.InlineFileProperties,
)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -995,6 +1015,7 @@ func newClientTestFile(
baseImportPath,
false,
false,
false,
UnionVersionUnspecified,
nil,
nil,
Expand Down Expand Up @@ -1352,12 +1373,12 @@ func generatedPackagesFromIR(ir *fernir.IntermediateRepresentation) map[string]s
}

// shouldSkipRequestType returns true if the request type should not be generated.
func shouldSkipRequestType(irEndpoint *fernir.HttpEndpoint) bool {
func shouldSkipRequestType(irEndpoint *fernir.HttpEndpoint, inlineFileProperties bool) bool {
if irEndpoint.SdkRequest == nil || irEndpoint.SdkRequest.Shape == nil || irEndpoint.SdkRequest.Shape.Wrapper == nil {
// This endpoint doesn't have any in-lined request types that need to be generated.
return true
}
return !needsRequestParameter(irEndpoint)
return !needsRequestParameter(irEndpoint, inlineFileProperties)
}

// fileUploadHasBodyProperties returns true if the file upload request has at least
Expand All @@ -1376,6 +1397,22 @@ func fileUploadHasBodyProperties(fileUpload *fernir.FileUploadRequest) bool {
return false
}

// fileUploadHasFileProperties returns true if the file upload request has at least
// one file property.
func fileUploadHasFileProperties(fileUpload *fernir.FileUploadRequest) bool {
if fileUpload == nil {
return false
}
// If this request is a file upload, there must be at least one body property
// in order for us to generate the in-lined request type.
for _, property := range fileUpload.Properties {
if property.File != nil {
return true
}
}
return false
}

func packagePathForDocs(fernFilepath *fernir.FernFilepath) []string {
var packages []string
for _, packageName := range fernFilepath.PackagePath {
Expand Down Expand Up @@ -1416,11 +1453,12 @@ func fileInfoToTypes(
irTypes map[fernir.TypeId]*fernir.TypeDeclaration,
irServices map[fernir.ServiceId]*fernir.HttpService,
irServiceTypeReferenceInfo *fernir.ServiceTypeReferenceInfo,
inlineFileProperties bool,
) (map[fileInfo][]*typeToGenerate, error) {
result := make(map[fileInfo][]*typeToGenerate)
for _, irService := range irServices {
for _, irEndpoint := range irService.Endpoints {
if shouldSkipRequestType(irEndpoint) {
if shouldSkipRequestType(irEndpoint, inlineFileProperties) {
continue
}
fileInfo := fileInfoForType(rootPackageName, irService.Name.FernFilepath)
Expand Down Expand Up @@ -1677,6 +1715,18 @@ func needsPaginationHelpers(ir *fernir.IntermediateRepresentation) bool {
return false
}

// needsFileUploadHelpers returns true if at least endpoint specifies a file upload.
func needsFileUploadHelpers(ir *fernir.IntermediateRepresentation) bool {
for _, irService := range ir.Services {
for _, irEndpoint := range irService.Endpoints {
if irEndpoint.RequestBody != nil && irEndpoint.RequestBody.FileUpload != nil {
return true
}
}
}
return false
}

func isReservedFilename(filename string) bool {
_, ok := reservedFilenames[filename]
return ok
Expand Down
Loading
Loading