-
Notifications
You must be signed in to change notification settings - Fork 138
Add support for SDK native types in dyn conversion #4385
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
12
commits into
main
Choose a base branch
from
sdk-duration-type
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c9c0879
Add support for SDK native types in dyn conversion
pietern 03301b6
Add end-to-end tests for SDK native types using postgres service types
pietern f9c72c3
Fix linter issues in SDK native types test
pietern e58db24
Address PR feedback on SDK native types implementation
pietern 2e98c02
Remove redundant SDK native types tests
pietern 39a483c
Consolidate SDK native types edge cases into table tests
pietern 3044c14
Eliminate type switches in roundtrip tests
pietern a7e5497
Inline equality assertions into roundtrip tests
pietern 07bedda
Simplify edge case tests by removing fake table tests
pietern 828feb7
Add time_invalid_format test case for SDK native types
pietern 41520d5
Clean up SDK native types test file
pietern df65828
Remove verbose comments from SDK native types tests
pietern 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package convert | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "reflect" | ||
| "slices" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn" | ||
| sdkduration "github.com/databricks/databricks-sdk-go/common/types/duration" | ||
| sdkfieldmask "github.com/databricks/databricks-sdk-go/common/types/fieldmask" | ||
| sdktime "github.com/databricks/databricks-sdk-go/common/types/time" | ||
| ) | ||
|
|
||
| // sdkNativeTypes is a list of SDK native types that use custom JSON marshaling | ||
| // and should be treated as strings in dyn.Value. These types all implement | ||
| // json.Marshaler and json.Unmarshaler interfaces. | ||
| var sdkNativeTypes = []reflect.Type{ | ||
| reflect.TypeFor[sdkduration.Duration](), // Protobuf duration format (e.g., "300s") | ||
| reflect.TypeFor[sdktime.Time](), // RFC3339 timestamp format (e.g., "2023-12-25T10:30:00Z") | ||
| reflect.TypeFor[sdkfieldmask.FieldMask](), // Comma-separated paths (e.g., "name,age,email") | ||
| } | ||
|
|
||
| // isSDKNativeType checks if the given type is one of the SDK's native types | ||
| // that use custom JSON marshaling and should be treated as strings. | ||
| func isSDKNativeType(typ reflect.Type) bool { | ||
| for typ.Kind() == reflect.Ptr { | ||
| typ = typ.Elem() | ||
| } | ||
| for _, sdkType := range sdkNativeTypes { | ||
| if typ == sdkType { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // fromTypedSDKNative converts SDK native types to dyn.Value. | ||
| // SDK native types (duration.Duration, time.Time, fieldmask.FieldMask) use | ||
| // custom JSON marshaling with string representations. | ||
| func fromTypedSDKNative(src reflect.Value, ref dyn.Value, options ...fromTypedOptions) (dyn.Value, error) { | ||
| // Check for zero value first. | ||
| if src.IsZero() && !slices.Contains(options, includeZeroValues) { | ||
| return dyn.NilValue, nil | ||
| } | ||
|
|
||
| // Use JSON marshaling since SDK native types implement json.Marshaler. | ||
| jsonBytes, err := json.Marshal(src.Interface()) | ||
| if err != nil { | ||
| return dyn.InvalidValue, err | ||
| } | ||
|
|
||
| // All SDK native types marshal to JSON strings. Unmarshal to get the raw string value. | ||
| // For example: duration.Duration(300s) -> JSON "300s" -> string "300s" | ||
| var str string | ||
| if err := json.Unmarshal(jsonBytes, &str); err != nil { | ||
| return dyn.InvalidValue, err | ||
| } | ||
|
|
||
| // Handle empty string as zero value. | ||
| if str == "" && !slices.Contains(options, includeZeroValues) { | ||
| return dyn.NilValue, nil | ||
| } | ||
|
|
||
| return dyn.V(str), nil | ||
| } | ||
|
|
||
| // toTypedSDKNative converts a dyn.Value to an SDK native type. | ||
| // SDK native types (duration.Duration, time.Time, fieldmask.FieldMask) use | ||
| // custom JSON marshaling with string representations. | ||
| func toTypedSDKNative(dst reflect.Value, src dyn.Value) error { | ||
| switch src.Kind() { | ||
| case dyn.KindString: | ||
| // Use JSON unmarshaling since SDK native types implement json.Unmarshaler. | ||
| // Marshal the string to create a valid JSON string literal for unmarshaling. | ||
| jsonBytes, err := json.Marshal(src.MustString()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return json.Unmarshal(jsonBytes, dst.Addr().Interface()) | ||
| case dyn.KindNil: | ||
| dst.SetZero() | ||
| return nil | ||
| default: | ||
| // Fall through to the error case. | ||
| } | ||
|
|
||
| return TypeError{ | ||
| value: src, | ||
| msg: fmt.Sprintf("expected a string, found a %s", src.Kind()), | ||
| } | ||
| } | ||
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 this be a variable reference?