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

[Internal] Generate Effective Fields #4057

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Changes from all commits
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
58 changes: 56 additions & 2 deletions .codegen/model.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,74 @@ import (
"github.com/databricks/databricks-sdk-go/marshal"
"github.com/hashicorp/terraform-plugin-framework/types"
)
{{- $excluded := dict "ShareInfo" (list "CreatedAt" "CreatedBy" "UpdatedAt" "UpdatedBy")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will need to be done for rest of the resources as well? If yes then this dictionary might become long and something we have to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will soon implement a new annotation (ServerProposedIfEmpty) for these effective fields. This annotation will not be applied to the excluded fields, allowing us to eliminate the exclusion list. As a result, the list will not expand to include other resources.

"SharedDataObject" (list "AddedAt" "AddedBy" "Status") -}}
{{range .Types}}
{{- if or .Fields .IsEmpty}}
{{.Comment "// " 80}}
type {{.PascalName}} struct {
{{- $excluded := getOrDefault $excluded .PascalName (list) -}}
{{- range .Fields}}
{{.Comment " // " 80}}
{{.PascalName}} {{template "type" .Entity}} `{{template "field-tag" . }}`{{end}}
{{- $data := dict "field" . "excluded" $excluded }}
{{template "field" $data}}{{if and .Entity.IsComputed (not (in $excluded .PascalName))}}{{ $data := dict "field" . "excluded" $excluded "effective" true }}{{printf "\n"}}{{template "field" $data}}{{end}}{{end}}
}

func (newState *{{.PascalName}}) SyncEffectiveFieldsDuringCreateOrUpdate(plan {{.PascalName}}) {
{{- range .Fields -}}
{{- if and .Entity.IsComputed (or .Entity.IsString .Entity.IsBool .Entity.IsInt64 .Entity.IsFloat64 .Entity.IsInt .Entity.Enum) -}}
{{- if not (in $excluded .PascalName)}}
newState.Effective{{.PascalName}} = newState.{{.PascalName}}
newState.{{.PascalName}} = plan.{{.PascalName}}
{{- end}}
{{- end}}
{{- end}}
}

func (newState *{{.PascalName}}) SyncEffectiveFieldsDuringRead(existingState {{.PascalName}}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For my understanding, where would we be calling these in the read? Asking because in case there is an error in the read, we shouldn't sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SyncEffectiveFieldsDuringRead is a function that should be invoked before saving to the state during a Read operation, while SyncEffectiveFieldsDuringCreateOrUpdate should be called before saving to the state during a Create or Update operation. We might consider introducing an interface here to allow this syncing to be done automatically by the Resource. However, I would prefer to postpone this refactoring for the time being.

{{- range .Fields -}}
{{- if and .Entity.IsComputed (or .Entity.IsString .Entity.IsBool .Entity.IsInt64 .Entity.IsFloat64 .Entity.IsInt .Entity.Enum) -}}
{{- if not (in $excluded .PascalName) -}}
{{- $type := "" -}}
{{- if .Entity.IsString}}{{$type = "String"}}{{end}}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am guessing -- we don't support other types like Object, list, map for now and this is to unblock share migration right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. For the moment, I chose primitives because I didn't see a use case for other options. We should revisit this decision in the future if we encounter any relevant cases.

{{- if .Entity.IsBool}}{{$type = "Bool"}}{{end}}
{{- if .Entity.IsInt64}}{{$type = "Int64"}}{{end}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to also add Int32? (types.Int32)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly, but I was only transferring what was already in place, so I wasn't aware of the consequences of making changes that fall outside the scope of this PR.

{{- if .Entity.IsFloat64}}{{$type = "Float64"}}{{end}}
Copy link
Contributor

Choose a reason for hiding this comment

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

same for float32

{{- if .Entity.IsInt}}{{$type = "Int64"}}{{end}}
{{- if .Entity.Enum}}{{$type = "String"}}{{end}}
if existingState.Effective{{.PascalName}}.Value{{$type}}() == newState.{{.PascalName}}.Value{{$type}}() {
newState.{{.PascalName}} = existingState.{{.PascalName}}
}
{{- end}}
{{- end}}
{{- end}}
}

{{end}}
{{end}}

{{- define "field" -}}
{{if .effective}}Effective{{end}}{{.field.PascalName}} {{template "type" .field.Entity}} `{{template "field-tag" . }}`
{{- end -}}

{{- define "field-tag" -}}
{{if .IsJson}}tfsdk:"{{if and (ne .Entity.Terraform nil) (ne .Entity.Terraform.Alias "") }}{{.Entity.Terraform.Alias}}{{else}}{{.Name}}{{end}}" tf:"{{- $first := true -}}{{- if not .Required -}}{{- if not $first -}},{{end}}optional{{- $first = false -}}{{- end -}}{{- if .Entity.IsObject -}}{{- if not $first -}},{{end}}object{{- $first = false -}}{{- end -}}"{{else}}tfsdk:"-"{{end -}}
{{- $annotations := "" -}}
{{- if in .excluded .field.PascalName -}}
{{- $annotations = (printf "%scomputed,optional," $annotations) -}}
{{- else if .effective -}}
{{- $annotations = (printf "%scomputed,optional," $annotations) -}}
{{- else -}}
{{- if not .field.Required -}}
{{- $annotations = (printf "%soptional," $annotations) -}}
{{- end -}}
{{- if .field.Entity.IsObject -}}
{{- $annotations = (printf "%sobject," $annotations) -}}
{{- end -}}
{{- end -}}
{{- if gt (len $annotations) 0 -}}
{{- $annotations = (printf "%s" (trimSuffix "," $annotations)) -}}
{{- end -}}
{{if .field.IsJson}}tfsdk:"{{if and (ne .field.Entity.Terraform nil) (ne .field.Entity.Terraform.Alias "") }}{{.field.Entity.Terraform.Alias}}{{else}}{{if .effective}}effective_{{end}}{{.field.Name}}{{end}}" tf:"{{$annotations}}"{{else}}tfsdk:"-"{{end -}}
{{- end -}}

{{- define "type" -}}
Expand Down
Loading