Skip to content

Commit 563463d

Browse files
committed
rearrange
1 parent 71b96e5 commit 563463d

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

libs/structaccess/get.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"github.com/databricks/cli/libs/structdiff/structtag"
1010
)
1111

12-
// Get returns the value at the given path inside v.
12+
// GetByString returns the value at the given path inside v.
13+
// This is a convenience function that parses the path string and calls Get.
1314
//
1415
// Path grammar (compatible with dyn path):
1516
// - Struct field names and map keys separated by '.' (e.g., connection.id)
@@ -23,6 +24,20 @@ import (
2324
// - For maps: a key indexes map[string]T (or string alias key types).
2425
// - For slices/arrays: an index [N] selects the N-th element.
2526
// - Wildcards ("*" or "[*]") are not supported and return an error.
27+
func GetByString(v any, path string) (any, error) {
28+
if path == "" {
29+
return v, nil
30+
}
31+
32+
dynPath, err := dyn.NewPathFromString(path)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return Get(v, dynPath)
38+
}
39+
40+
// Get returns the value at the given path inside v.
2641
func Get(v any, path dyn.Path) (any, error) {
2742
if len(path) == 0 {
2843
return v, nil
@@ -83,34 +98,6 @@ func Get(v any, path dyn.Path) (any, error) {
8398
return cur.Interface(), nil
8499
}
85100

86-
// GetByString returns the value at the given path inside v.
87-
// This is a convenience function that parses the path string and calls Get.
88-
//
89-
// Path grammar (compatible with dyn path):
90-
// - Struct field names and map keys separated by '.' (e.g., connection.id)
91-
// - (Note, this prevents maps keys that are not id-like from being referenced, but this general problem with references today.)
92-
// - Numeric indices in brackets for arrays/slices (e.g., items[0].name)
93-
// - Leading '.' is allowed (e.g., .connection.id)
94-
//
95-
// Behavior:
96-
// - For structs: a key matches a field by its json tag name (if present and not "-").
97-
// Embedded anonymous structs are searched.
98-
// - For maps: a key indexes map[string]T (or string alias key types).
99-
// - For slices/arrays: an index [N] selects the N-th element.
100-
// - Wildcards ("*" or "[*]") are not supported and return an error.
101-
func GetByString(v any, path string) (any, error) {
102-
if path == "" {
103-
return v, nil
104-
}
105-
106-
dynPath, err := dyn.NewPathFromString(path)
107-
if err != nil {
108-
return nil, err
109-
}
110-
111-
return Get(v, dynPath)
112-
}
113-
114101
// accessKey returns the field or map entry value selected by key from v.
115102
// v must be non-pointer, non-interface reflect.Value.
116103
func accessKey(v reflect.Value, key, prefix string) (reflect.Value, error) {

0 commit comments

Comments
 (0)