Skip to content

Commit

Permalink
generator-go-sdk: rename setting for omitting discriminated values, i…
Browse files Browse the repository at this point in the history
…mprove doc comment
  • Loading branch information
manicminer committed Sep 23, 2024
1 parent 65c75d7 commit ce231e0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions tools/generator-go-sdk/internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (g GenerateCommand) Run(args []string) int {
}

if g.sourceDataType == models.MicrosoftGraphSourceDataType {
input.settings.EnableModelBehaviors = true
input.settings.AllowOmittingDiscriminatedValue = true
input.settings.GenerateDescriptionsForModels = true
input.settings.RecurseParentModels = false

Expand All @@ -72,7 +72,7 @@ func (g GenerateCommand) Run(args []string) int {
"beta": models.MicrosoftGraphMetaDataSourceDataOrigin,
}
} else if g.sourceDataType == models.ResourceManagerSourceDataType {
input.settings.EnableModelBehaviors = false
input.settings.AllowOmittingDiscriminatedValue = false
input.settings.GenerateDescriptionsForModels = false
input.settings.RecurseParentModels = true

Expand Down
46 changes: 23 additions & 23 deletions tools/generator-go-sdk/internal/generator/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type GeneratorData struct {
// the package name for the API version
versionPackageName string

// whether model behaviors are enabled
enableModelBehaviors bool
// whether callers can configure models to omit the discriminated value field when marshalling
allowOmittingDiscriminatedValue bool

// whether descriptions should be generated for model fields etc.
generateDescriptionsForModels bool
Expand Down Expand Up @@ -113,27 +113,27 @@ func (i ServiceGeneratorInput) generatorData(settings Settings) GeneratorData {
}

return GeneratorData{
apiVersion: i.VersionName,
canonicalApiVersion: settings.CanonicalApiVersion(i.VersionName),
baseClientPackage: baseClientPackageForSdk(i.Type),
commonTypes: i.CommonTypes,
commonTypesIncludePath: commonTypesIncludePath,
commonTypesPackageName: commonTypesPackageName,
constants: i.ResourceDetails.Constants,
enableModelBehaviors: settings.EnableModelBehaviors,
generateDescriptionsForModels: settings.GenerateDescriptionsForModels,
isDataPlane: models.SourceDataTypeIsDataPlane(i.Type),
models: i.ResourceDetails.Models,
operations: i.ResourceDetails.Operations,
packageName: resourcePackageName,
recurseParentModels: settings.RecurseParentModels,
resourceIds: i.ResourceDetails.ResourceIDs,
resourceOutputPath: resourceOutputPath,
serviceClientName: fmt.Sprintf("%sClient", strings.Title(i.ResourceName)),
servicePackageName: strings.ToLower(i.ServiceName),
source: i.Source,
sourceType: i.Type,
useNewBaseLayer: useNewBaseLayer,
apiVersion: i.VersionName,
canonicalApiVersion: settings.CanonicalApiVersion(i.VersionName),
baseClientPackage: baseClientPackageForSdk(i.Type),
commonTypes: i.CommonTypes,
commonTypesIncludePath: commonTypesIncludePath,
commonTypesPackageName: commonTypesPackageName,
constants: i.ResourceDetails.Constants,
allowOmittingDiscriminatedValue: settings.AllowOmittingDiscriminatedValue,
generateDescriptionsForModels: settings.GenerateDescriptionsForModels,
isDataPlane: models.SourceDataTypeIsDataPlane(i.Type),
models: i.ResourceDetails.Models,
operations: i.ResourceDetails.Operations,
packageName: resourcePackageName,
recurseParentModels: settings.RecurseParentModels,
resourceIds: i.ResourceDetails.ResourceIDs,
resourceOutputPath: resourceOutputPath,
serviceClientName: fmt.Sprintf("%sClient", strings.Title(i.ResourceName)),
servicePackageName: strings.ToLower(i.ServiceName),
source: i.Source,
sourceType: i.Type,
useNewBaseLayer: useNewBaseLayer,
}
}

Expand Down
14 changes: 7 additions & 7 deletions tools/generator-go-sdk/internal/generator/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

type Settings struct {
CanonicalApiVersions map[string]string
CommonTypesPackageName string
EnableModelBehaviors bool
GenerateDescriptionsForModels bool
RecurseParentModels bool
VersionsToGenerateCommonTypes map[string]models.SourceDataOrigin
servicesUsingOldBaseLayer map[string]struct{}
CanonicalApiVersions map[string]string
CommonTypesPackageName string
AllowOmittingDiscriminatedValue bool
GenerateDescriptionsForModels bool
RecurseParentModels bool
VersionsToGenerateCommonTypes map[string]models.SourceDataOrigin
servicesUsingOldBaseLayer map[string]struct{}
}

func (s *Settings) CanonicalApiVersion(version string) *string {
Expand Down
4 changes: 2 additions & 2 deletions tools/generator-go-sdk/internal/generator/templater_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type %[1]s interface {

// Determine any behavioral struct field lines, if enabled
behavioralStructLines := ""
if data.enableModelBehaviors {
if data.allowOmittingDiscriminatedValue {
if c.model.FieldNameContainingDiscriminatedValue != nil {
behavioralStructLines += strings.ReplaceAll(`
// Model Behaviors
Expand Down Expand Up @@ -631,7 +631,7 @@ func (s %[1]s) MarshalJSON() ([]byte, error) {
}
}

if data.enableModelBehaviors {
if data.allowOmittingDiscriminatedValue {
output += fmt.Sprintf(`
if !s.OmitDiscriminatedValue {
decoded[%[1]q] = %[2]q
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func TestTemplaterModelsImplementationWithModelBehaviors(t *testing.T) {
DiscriminatedValue: stringPointer("train"),
},
}.template(GeneratorData{
enableModelBehaviors: true,
packageName: "somepackage",
allowOmittingDiscriminatedValue: true,
packageName: "somepackage",
models: map[string]models.SDKModel{
"ModeOfTransit": {
FieldNameContainingDiscriminatedValue: stringPointer("Type"),
Expand Down

0 comments on commit ce231e0

Please sign in to comment.