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

Add reference fields to the InitProvider #315

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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...)
}
Comment on lines +438 to +444
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reference fields will now always be added to the spec.forProvider and spec.initProvider, is this correct? If we don't have the branching here, what would cause an issue is the fact that this function is called once for the forProvider and once for initProvider, is this correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the both are correct. Now we always generate the reference fields for both. And if we do not have a branch here, this will cause an issue.

}

// 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)
sergenyalcin marked this conversation as resolved.
Show resolved Hide resolved
return !f.Identifier && f.TFTag != "-"
sergenyalcin marked this conversation as resolved.
Show resolved Hide resolved
}

func getDescription(s string) string {
Expand Down
Loading