@@ -71,14 +71,14 @@ func NewCfgFns(rootPackage, componentPackage string) map[string]any {
7171 return name
7272 },
7373 "camelVar" : CamelVar ,
74- "formatDefaultValue" : func (md * ConfigMetadata , name string , defaultValue DefaultValue ) string {
74+ "formatDefaultValue" : func (md * ConfigMetadata , name string , defaultValue any ) string {
7575 return FormatDefaultValue (md , name , defaultValue , rootPackage , componentPackage )
7676 },
77- "formatBaseValue" : func (md * ConfigMetadata , name string , defaultValue DefaultValue ) string {
77+ "formatBaseValue" : func (md * ConfigMetadata , name string , defaultValue any ) string {
7878 return FormatBaseValue (md , name , defaultValue , rootPackage , componentPackage )
7979 },
8080 "wrapDefaultValue" : WrapDefaultValue ,
81- "mapCustomDefaults" : func (schema * ConfigMetadata , defaultValue DefaultValue ) []string {
81+ "mapCustomDefaults" : func (schema * ConfigMetadata , defaultValue any ) []string {
8282 return MapCustomDefaults (schema , defaultValue , rootPackage , componentPackage )
8383 },
8484 "hasDefaultValue" : hasDefaultValue ,
@@ -222,6 +222,9 @@ func collectImports(md *ConfigMetadata, imports map[string]bool, rootPackage, co
222222 }
223223 refDesc := NewRef (md .ResolvedFrom )
224224 if ! refDesc .isInternal () {
225+ if err := collectCustomDefaultImports (md , md .Default , imports , rootPackage , componentPackage ); err != nil {
226+ return err
227+ }
225228 return nil
226229 }
227230 }
@@ -267,6 +270,42 @@ func collectImports(md *ConfigMetadata, imports map[string]bool, rootPackage, co
267270 return nil
268271}
269272
273+ func collectCustomDefaultImports (md * ConfigMetadata , defaultValue any , imports map [string ]bool , rootPackage , componentPackage string ) error {
274+ if md == nil || md .GoStruct .IgnoreDefault {
275+ return nil
276+ }
277+
278+ switch typedValue := defaultValue .(type ) {
279+ case map [string ]any :
280+ if md .AdditionalProperties != nil {
281+ return nil
282+ }
283+ for key , value := range typedValue {
284+ prop := md .Properties [key ]
285+ if prop == nil {
286+ continue
287+ }
288+ if err := collectImports (prop , imports , rootPackage , componentPackage ); err != nil {
289+ return err
290+ }
291+ if err := collectCustomDefaultImports (prop , value , imports , rootPackage , componentPackage ); err != nil {
292+ return err
293+ }
294+ }
295+ case []any :
296+ if md .Items == nil || md .Items .Type != "object" {
297+ return nil
298+ }
299+ for _ , item := range typedValue {
300+ if err := collectCustomDefaultImports (md .Items , item , imports , rootPackage , componentPackage ); err != nil {
301+ return err
302+ }
303+ }
304+ }
305+
306+ return nil
307+ }
308+
270309// FormatTypeName resolves a reference string to a Go type expression using GoTypeRef.
271310func FormatTypeName (ref , rootPackage , componentPackage string ) (string , error ) {
272311 tr , err := ResolveGoTypeRef (ref , rootPackage , componentPackage )
@@ -447,12 +486,12 @@ func generateValidatorName(propName string, desc *CustomValidatorConfig) string
447486 return "validate" + id
448487}
449488
450- func MapCustomDefaults (schema * ConfigMetadata , defaultValue DefaultValue , rootPackage , componentPackage string ) []string {
451- if ! defaultValue . IsSet () {
489+ func MapCustomDefaults (schema * ConfigMetadata , defaultValue any , rootPackage , componentPackage string ) []string {
490+ if schema . GoStruct . IgnoreDefault {
452491 return nil
453492 }
454493 exps := make ([]string , 0 )
455- switch typedValue := defaultValue .Get (). (type ) {
494+ switch typedValue := defaultValue .(type ) {
456495 case map [string ]any :
457496 // is nested struct
458497 if schema .AdditionalProperties == nil {
@@ -462,7 +501,7 @@ func MapCustomDefaults(schema *ConfigMetadata, defaultValue DefaultValue, rootPa
462501 panic ("schema does not contain required property: " + key )
463502 }
464503 varName , _ := helpers .FormatIdentifier (key , true )
465- exp := fmt .Sprintf (".%s = %s" , varName , FormatDefaultValue (propSchema , key , NewDefaultValue ( value ) , rootPackage , componentPackage ))
504+ exp := fmt .Sprintf (".%s = %s" , varName , FormatDefaultValue (propSchema , key , value , rootPackage , componentPackage ))
466505 exps = append (exps , exp )
467506 }
468507 } else if schema .AdditionalProperties .Type == "object" { // is a map of object
@@ -474,7 +513,7 @@ func MapCustomDefaults(schema *ConfigMetadata, defaultValue DefaultValue, rootPa
474513 panic ("unsupported default value type for custom mapping" )
475514 }
476515 for i , item := range typedValue {
477- nestedExps := MapCustomDefaults (schema .Items , NewDefaultValue ( item ) , rootPackage , componentPackage )
516+ nestedExps := MapCustomDefaults (schema .Items , item , rootPackage , componentPackage )
478517 for _ , exp := range nestedExps {
479518 exps = append (exps , fmt .Sprintf ("[%d]%s" , i , exp ))
480519 }
@@ -484,8 +523,8 @@ func MapCustomDefaults(schema *ConfigMetadata, defaultValue DefaultValue, rootPa
484523 return exps
485524}
486525
487- func FormatDefaultValue (md * ConfigMetadata , name string , defaultValue DefaultValue , rootPackage , componentPackage string ) string {
488- if defaultValue . IsSet () && defaultValue . Get () == nil {
526+ func FormatDefaultValue (md * ConfigMetadata , name string , defaultValue any , rootPackage , componentPackage string ) string {
527+ if md . GoStruct . IgnoreDefault || ( defaultValue == nil && ! hasDefaultValue ( md )) {
489528 if md .IsPointer {
490529 return "nil"
491530 }
@@ -510,7 +549,7 @@ func FormatDefaultValue(md *ConfigMetadata, name string, defaultValue DefaultVal
510549
511550// FormatBaseValue returns the default value expression without IsPointer/IsOptional wrappers.
512551// Use this when initializing a local variable that will be mutated before wrapping.
513- func FormatBaseValue (md * ConfigMetadata , name string , defaultValue DefaultValue , rootPackage , componentPackage string ) string {
552+ func FormatBaseValue (md * ConfigMetadata , name string , defaultValue any , rootPackage , componentPackage string ) string {
514553 return formatSimpleValue (md , name , defaultValue , rootPackage , componentPackage )
515554}
516555
@@ -531,7 +570,7 @@ func WrapDefaultValue(md *ConfigMetadata, varName string) string {
531570}
532571
533572func hasDefaultValue (md * ConfigMetadata ) bool {
534- if md .Default . IsSet () {
573+ if ! md .GoStruct . IgnoreDefault && md . Default != nil {
535574 return true
536575 }
537576 for _ , prop := range md .Properties {
@@ -552,7 +591,7 @@ func CamelVar(ref string) string {
552591 return name
553592}
554593
555- func formatSimpleValue (md * ConfigMetadata , name string , defaultValue DefaultValue , rootPackage , componentPackage string ) string {
594+ func formatSimpleValue (md * ConfigMetadata , name string , defaultValue any , rootPackage , componentPackage string ) string {
556595 // handle references
557596 isReference := md .ResolvedFrom != ""
558597 isSubStruct := md .Type == "object" && md .AdditionalProperties == nil
@@ -577,19 +616,19 @@ func formatSimpleValue(md *ConfigMetadata, name string, defaultValue DefaultValu
577616 return ""
578617 }
579618
580- // do not process further if "default " attribute not defined
581- if ! defaultValue . IsSet () {
619+ // do not process further if "ignore_default " attribute set
620+ if md . GoStruct . IgnoreDefault {
582621 return ""
583622 }
584623
585624 switch md .Type {
586625 case "array" :
587626 typeExpr , err := resolveGoType (md .Items , name + "_item" , "" , "" )
588627 if err == nil {
589- if defaultValues , ok := defaultValue .Get (). ([]any ); ok {
628+ if defaultValues , ok := defaultValue .([]any ); ok {
590629 exps := make ([]string , 0 , len (defaultValues ))
591630 for _ , defaultValue := range defaultValues {
592- exps = append (exps , FormatDefaultValue (md .Items , name + "_item" , NewDefaultValue ( defaultValue ) , rootPackage , componentPackage ))
631+ exps = append (exps , FormatDefaultValue (md .Items , name + "_item" , defaultValue , rootPackage , componentPackage ))
593632 }
594633 return fmt .Sprintf ("[]%s{%s}" , typeExpr , strings .Join (exps , ", " ))
595634 }
@@ -599,13 +638,13 @@ func formatSimpleValue(md *ConfigMetadata, name string, defaultValue DefaultValu
599638 case "object" :
600639 typeExpr , err := resolveGoType (md .AdditionalProperties , name , "" , "" )
601640 if err == nil {
602- if defaultValues , ok := defaultValue .Get (). (map [string ]any ); ok {
641+ if defaultValues , ok := defaultValue .(map [string ]any ); ok {
603642 exps := make ([]string , 0 , len (defaultValues ))
604643 for _ , keyName := range slices .Sorted (maps .Keys (defaultValues )) {
605644 value := defaultValues [keyName ]
606645 exps = append (
607646 exps ,
608- fmt .Sprintf ("%q: %v" , keyName , FormatDefaultValue (md .AdditionalProperties , name , NewDefaultValue ( value ) , rootPackage , componentPackage )))
647+ fmt .Sprintf ("%q: %v" , keyName , FormatDefaultValue (md .AdditionalProperties , name , value , rootPackage , componentPackage )))
609648 }
610649 return fmt .Sprintf ("map[string]%s{%s}" , typeExpr , strings .Join (exps , ", " ))
611650 }
@@ -615,14 +654,14 @@ func formatSimpleValue(md *ConfigMetadata, name string, defaultValue DefaultValu
615654 case "string" :
616655 switch md .GoType {
617656 case "time.Duration" :
618- if durationExpr , ok := renderDurationExpr (defaultValue . Get () ); ok {
657+ if durationExpr , ok := renderDurationExpr (defaultValue ); ok {
619658 return durationExpr
620659 }
621660 default :
622- return fmt .Sprintf ("%q" , defaultValue . Get () )
661+ return fmt .Sprintf ("%q" , defaultValue )
623662 }
624663 default :
625- return fmt .Sprintf ("%v" , defaultValue . Get () )
664+ return fmt .Sprintf ("%v" , defaultValue )
626665 }
627666
628667 panic ("unreachable" )
0 commit comments