-
Notifications
You must be signed in to change notification settings - Fork 191
direct: Fix state serialization for zero-value fields on deeply-embedded SDK specs #5782
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
Open
pietern
wants to merge
1
commit into
main
Choose a base branch
from
enable-pg-native-login-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ package convert | |
| import ( | ||
| "reflect" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn" | ||
|
|
@@ -28,9 +30,12 @@ type structInfo struct { | |
| // Maps JSON-name of the field to Golang struct name | ||
| GolangNames map[string]string | ||
|
|
||
| // ForceSendFieldsStructKey maps the JSON-name of the field to which ForceSendFields slice it belongs to: | ||
| // -1 for direct fields, embedded struct index for embedded fields | ||
| ForceSendFieldsStructKey map[string]int | ||
| // ForceSendFieldsStructKey maps the JSON-name of the field to the struct that | ||
| // owns the ForceSendFields slice it belongs to. The value is the index path to | ||
| // that struct (see indexPathKey); "" is the top-level struct. The path can be | ||
| // more than one element deep because a field's declaring struct may be embedded | ||
| // several levels down (e.g. PostgresProject -> PostgresProjectConfig -> ProjectSpec). | ||
| ForceSendFieldsStructKey map[string]string | ||
| } | ||
|
|
||
| // structInfoCache caches type information. | ||
|
|
@@ -61,7 +66,7 @@ func buildStructInfo(typ reflect.Type) structInfo { | |
| Fields: make(map[string][]int), | ||
| ForceEmpty: make(map[string]bool), | ||
| GolangNames: make(map[string]string), | ||
| ForceSendFieldsStructKey: make(map[string]int), | ||
| ForceSendFieldsStructKey: make(map[string]string), | ||
|
Contributor
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. why can't we keep precalculated index? the structs are static, so it should always resolve to the same index, right? |
||
| } | ||
|
|
||
| // Queue holds the indexes of the structs to visit. | ||
|
|
@@ -119,14 +124,9 @@ func buildStructInfo(typ reflect.Type) structInfo { | |
| } | ||
| out.GolangNames[name] = sf.Name | ||
|
|
||
| // Determine which ForceSendFields this field belongs to | ||
| if len(prefix) == 0 { | ||
| // Direct field on the main struct | ||
| out.ForceSendFieldsStructKey[name] = -1 | ||
| } else { | ||
| // Field on embedded struct | ||
| out.ForceSendFieldsStructKey[name] = prefix[0] | ||
| } | ||
| // The field is declared directly in the struct reached by prefix, so | ||
| // its ForceSendFields lives there. prefix is empty for the top-level struct. | ||
| out.ForceSendFieldsStructKey[name] = indexPathKey(prefix) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -192,33 +192,45 @@ func (s *structInfo) FieldValues(v reflect.Value) []FieldValue { | |
| // Type of [dyn.Value]. | ||
| var configValueType = reflect.TypeFor[dyn.Value]() | ||
|
|
||
| // getForceSendFieldsValues collects ForceSendFields reflect.Values | ||
| // Returns map[structKey]reflect.Value where structKey is -1 for direct fields, embedded index for embedded fields | ||
| func getForceSendFieldsValues(v reflect.Value) map[int]reflect.Value { | ||
| if !v.IsValid() || v.Type().Kind() != reflect.Struct { | ||
| return make(map[int]reflect.Value) | ||
| } | ||
| // getForceSendFieldsValues collects the ForceSendFields slice declared directly | ||
| // by the top-level struct and by every embedded struct reachable through anonymous | ||
| // fields, at any depth. The result is keyed by the index path to each owning struct | ||
| // (see indexPathKey), matching structInfo.ForceSendFieldsStructKey. Embedding can be | ||
| // arbitrarily deep (e.g. PostgresProject -> PostgresProjectConfig -> ProjectSpec), | ||
| // so each level is recorded under its own path rather than collapsed to one index. | ||
| func getForceSendFieldsValues(v reflect.Value) map[string]reflect.Value { | ||
| result := make(map[string]reflect.Value) | ||
| collectForceSendFieldsValues(v, nil, result) | ||
| return result | ||
| } | ||
|
|
||
| result := make(map[int]reflect.Value) | ||
| func collectForceSendFieldsValues(v reflect.Value, path []int, result map[string]reflect.Value) { | ||
| v = deref(v) | ||
| if !v.IsValid() || v.Kind() != reflect.Struct { | ||
| return | ||
| } | ||
|
|
||
| for i := range v.Type().NumField() { | ||
| field := v.Type().Field(i) | ||
| fieldValue := v.Field(i) | ||
|
|
||
| if field.Name == "ForceSendFields" && !field.Anonymous { | ||
| // Direct ForceSendFields (structKey = -1) | ||
| result[-1] = fieldValue | ||
| } else if field.Anonymous { | ||
| // Embedded struct - check for ForceSendFields inside it | ||
| if embeddedStruct := deref(fieldValue); embeddedStruct.IsValid() { | ||
| if forceSendField := embeddedStruct.FieldByName("ForceSendFields"); forceSendField.IsValid() { | ||
| result[i] = forceSendField | ||
| } | ||
| } | ||
| switch { | ||
| case field.Name == "ForceSendFields" && !field.Anonymous: | ||
| result[indexPathKey(path)] = fieldValue | ||
| case field.Anonymous: | ||
| collectForceSendFieldsValues(fieldValue, append(path, i), result) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| // indexPathKey renders a reflect index path as a stable map key. The empty path | ||
| // (the top-level struct) renders as "". | ||
| func indexPathKey(path []int) string { | ||
| parts := make([]string, len(path)) | ||
| for i, x := range path { | ||
| parts[i] = strconv.Itoa(x) | ||
| } | ||
| return strings.Join(parts, ".") | ||
| } | ||
|
|
||
| // deref dereferences a pointer, returning invalid value if nil | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
can we also add this to invariant tests?
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.
Included this in the project resource config for the invariant tests.