Skip to content

Commit

Permalink
generate effective fields instead of original fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchy committed Oct 15, 2024
1 parent cc7273a commit 11d6f16
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .codegen/_openapi_sha
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50dceaf3be6c7ff8d96c52574d0597662b412a62
cf9c61453990df0f9453670f2fe68e1b128647a2
61 changes: 50 additions & 11 deletions .codegen/model.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,47 @@ 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}}
{{- $data := dict "field" . }}
{{template "field" $data}}{{if .Entity.IsComputed }}{{ $data := dict "field" . "effective" true }}{{printf "\n"}}{{template "field" $data}}{{end}}{{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}}) {
{{- 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}}
{{- if .Entity.IsBool}}{{$type = "Bool"}}{{end}}
{{- if .Entity.IsInt64}}{{$type = "Int64"}}{{end}}
{{- if .Entity.IsFloat64}}{{$type = "Float64"}}{{end}}
{{- 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}}
Expand All @@ -37,15 +70,21 @@ type {{.PascalName}} struct {

{{- define "field-tag" -}}
{{- $annotations := "" -}}
{{- if not .field.Required -}}
{{- $annotations = (printf "%soptional," $annotations) -}}
{{- end -}}
{{- if .effective -}}
{{- $annotations = (printf "%scomputed," $annotations) -}}
{{- end -}}
{{- if gt (len $annotations) 0 -}}
{{- $annotations = (printf "%s" (trimSuffix "," $annotations)) -}}
{{- end -}}
{{- 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 -}}

Expand Down

0 comments on commit 11d6f16

Please sign in to comment.