-
Notifications
You must be signed in to change notification settings - Fork 400
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
"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}}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{{- 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}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good to also add Int32? (types.Int32) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" -}} | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.