Skip to content

Commit

Permalink
Add reference fields to the InitProvider
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Dec 21, 2023
1 parent 807fb9d commit eb239f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 15 additions & 6 deletions pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, r
if paramType.Underlying().String() != emptyStruct {
field := types.NewField(token.NoPos, g.Package, f.Name.Camel, types.NewSlice(paramType), false)
r.addParameterField(f, field)
r.addInitField(f, field, g.Package)
r.addInitField(f, field, g, nil)
}
default:
if paramType == nil {
Expand Down Expand Up @@ -400,7 +400,7 @@ func (r *resource) addParameterField(f *Field, field *types.Var) {
r.paramFields = append(r.paramFields, field)
}

func (r *resource) addInitField(f *Field, field *types.Var, pkg *types.Package) {
func (r *resource) addInitField(f *Field, field *types.Var, g *Builder, typeNames *types.TypeName) {
// If the field is not an init field, we don't add it.
if !f.isInit() {
return
Expand All @@ -410,10 +410,14 @@ func (r *resource) addInitField(f *Field, field *types.Var, pkg *types.Package)

// If the field is a nested type, we need to add it as the init type.
if f.InitType != nil {
field = types.NewField(token.NoPos, pkg, f.Name.Camel, f.InitType, false)
field = types.NewField(token.NoPos, g.Package, f.Name.Camel, f.InitType, false)
}

r.initFields = append(r.initFields, field)

if f.Reference != nil {
r.addReferenceFields(g, typeNames, f, true)
}
}

func (r *resource) addObservationField(f *Field, field *types.Var) {
Expand All @@ -429,10 +433,15 @@ func (r *resource) addObservationField(f *Field, field *types.Var) {
r.obsTags = append(r.obsTags, fmt.Sprintf(`json:"%s" tf:"%s"`, f.JSONTag, f.TFTag))
}

func (r *resource) addReferenceFields(g *Builder, paramName *types.TypeName, field *Field) {
func (r *resource) addReferenceFields(g *Builder, paramName *types.TypeName, field *Field, isInit bool) {
refFields, refTags := g.generateReferenceFields(paramName, field)
r.paramTags = append(r.paramTags, refTags...)
r.paramFields = append(r.paramFields, refFields...)
if isInit {
r.initTags = append(r.initTags, refTags...)
r.initFields = append(r.initFields, refFields...)
} else {
r.paramTags = append(r.paramTags, refTags...)
r.paramFields = append(r.paramFields, refFields...)
}
}

// generateTypeName generates a unique name for the type if its original name
Expand Down
6 changes: 3 additions & 3 deletions pkg/types/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ func (f *Field) AddToResource(g *Builder, r *resource, typeNames *TypeNames, add
f.TFTag = strings.TrimSuffix(f.TFTag, ",omitempty")
}
r.addParameterField(f, field)
r.addInitField(f, field, g.Package)
r.addInitField(f, field, g, typeNames.InitTypeName)
}

if f.Reference != nil {
r.addReferenceFields(g, typeNames.ParameterTypeName, f)
r.addReferenceFields(g, typeNames.ParameterTypeName, f, false)
}

// Note(lsviben): All fields are optional because observation fields are
Expand Down Expand Up @@ -401,7 +401,7 @@ func (f *Field) AddToResource(g *Builder, r *resource, typeNames *TypeNames, add
// an earlier step, so they cannot be included as well. Plus probably they
// should also not change for Create and Update steps.
func (f *Field) isInit() bool {
return !f.Identifier && f.Reference == nil && (f.TFTag != "-" || f.Injected)
return !f.Identifier && f.TFTag != "-"
}

func getDescription(s string) string {
Expand Down

0 comments on commit eb239f4

Please sign in to comment.