99 "go/token"
1010 "go/types"
1111 "sort"
12- "strings"
1312
1413 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1514 twtypes "github.com/muvaf/typewriter/pkg/types"
@@ -168,7 +167,7 @@ func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPa
168167 return nil , nil , nil , err
169168 }
170169 }
171- f .AddToResource (g , r , typeNames , cfg .SchemaElementOptions . AddToObservation ( cPath ))
170+ f .AddToResource (g , r , typeNames , ptr . Deref ( cfg .SchemaElementOptions [ cPath ], config. SchemaElementOption {} ))
172171 }
173172
174173 paramType , obsType , initType := g .AddToBuilder (typeNames , r )
@@ -282,8 +281,10 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, cp
282281 tParam = types .NewSlice (paramType )
283282 tInit = types .NewSlice (initType )
284283 }
284+ opt := ptr .Deref (cfg .SchemaElementOptions [cpath ], config.SchemaElementOption {})
285+ initProviderOverrides := ptr .Deref (opt .InitProviderOverrides , config.InitProviderOverrides {})
285286 r .addParameterField (f , types .NewField (token .NoPos , g .Package , f .Name .Camel , tParam , false ))
286- r .addInitField (f , types .NewField (token .NoPos , g .Package , f .Name .Camel , tInit , false ), g , nil )
287+ r .addInitField (f , types .NewField (token .NoPos , g .Package , f .Name .Camel , tInit , false ), g , nil , initProviderOverrides . TagOverrides )
287288 }
288289 default :
289290 if paramType == nil {
@@ -400,32 +401,30 @@ func (r *resource) addParameterField(f *Field, field *types.Var) {
400401 requiredBySchema = false
401402 // If the field is not a terraform field, we should not require it in init,
402403 // as it is not an initProvider field.
403- r .topLevelRequiredParams = append (r .topLevelRequiredParams , newTopLevelRequiredParam (f .TransformedName , f .TFTag != "-" ))
404+ r .topLevelRequiredParams = append (r .topLevelRequiredParams , newTopLevelRequiredParam (f .TransformedName , ! f .TFTag . AlwaysOmitted () ))
404405 }
405406
406407 // Note(lsviben): Only fields which are not also initProvider fields should have a required kubebuilder comment.
407- f .Comment .Required = ptr .To (requiredBySchema && ! f .isInit ())
408+ f .Comment .KubebuilderOptions . Required = ptr .To (requiredBySchema && ! f .isInit ())
408409
409410 // For removing omitempty tag from json tag, we are just checking if the field is required by the schema.
410411 if requiredBySchema {
411412 // Required fields should not have omitempty tag in json tag.
412- // TODO(muvaf): This overrides user intent if they provided custom
413- // JSON tag.
414- r .paramTags = append (r .paramTags , fmt .Sprintf (`json:"%s" tf:"%s"` , strings .TrimSuffix (f .JSONTag , ",omitempty" ), f .TFTag ))
413+ r .paramTags = append (r .paramTags , fmt .Sprintf ("%s %s" , f .JSONTag .NoOmit (), f .TFTag ))
415414 } else {
416- r .paramTags = append (r .paramTags , fmt .Sprintf (`json: "%s" tf:" %s"` , f .JSONTag , f .TFTag ))
415+ r .paramTags = append (r .paramTags , fmt .Sprintf ("%s %s" , f .JSONTag , f .TFTag ))
417416 }
418417
419418 r .paramFields = append (r .paramFields , field )
420419}
421420
422- func (r * resource ) addInitField (f * Field , field * types.Var , g * Builder , typeNames * types.TypeName ) {
421+ func (r * resource ) addInitField (f * Field , field * types.Var , g * Builder , typeNames * types.TypeName , o config. TagOverrides ) {
423422 // If the field is not an init field, we don't add it.
424423 if ! f .isInit () {
425424 return
426425 }
427426
428- r .initTags = append (r .initTags , fmt .Sprintf (`json: "%s" tf:" %s"` , f .JSONTag , f .TFTag ))
427+ r .initTags = append (r .initTags , fmt .Sprintf ("%s %s" , f .JSONTag . OverrideFrom ( o . JSONTag ) , f .TFTag . OverrideFrom ( o . TFTag ) ))
429428
430429 // If the field is a nested type, we need to add it as the init type.
431430 if f .InitType != nil {
@@ -449,7 +448,7 @@ func (r *resource) addObservationField(f *Field, field *types.Var) {
449448 }
450449 }
451450 r .obsFields = append (r .obsFields , field )
452- r .obsTags = append (r .obsTags , fmt .Sprintf (`json: "%s" tf:" %s"` , f .JSONTag , f .TFTag ))
451+ r .obsTags = append (r .obsTags , fmt .Sprintf ("%s %s" , f .JSONTag , f .TFTag ))
453452}
454453
455454func (r * resource ) addReferenceFields (g * Builder , paramName * types.TypeName , field * Field , isInit bool ) {
0 commit comments