Skip to content

Commit bb5f8d6

Browse files
committed
CLOUDP-350407: refactor translate - improve unstructured tests
1 parent c71896b commit bb5f8d6

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

internal/autogen/translate/unstructured/unstructured.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626

2727
var ErrNotFound = errors.New("not found")
2828

29+
var ErrNilObject = errors.New("nil object")
30+
2931
// ToUnstructured returns an unstructured map holding the public field values
3032
// from the original input obj value
3133
func ToUnstructured(obj any) (map[string]any, error) {
@@ -56,6 +58,9 @@ func FromUnstructured[T any](target *T, source map[string]any) error {
5658
// AccessField gets the value of a named path within the given unstructured map
5759
func AccessField[T any](obj map[string]any, fields ...string) (T, error) {
5860
var zeroValue T
61+
if obj == nil {
62+
return zeroValue, ErrNilObject
63+
}
5964
rawValue, ok, err := unstructured.NestedFieldNoCopy(obj, fields...)
6065
if !ok {
6166
return zeroValue, fmt.Errorf("path %v %w", fields, ErrNotFound)
@@ -80,7 +85,7 @@ func CreateField[T any](obj map[string]any, value T, fields ...string) error {
8085
if rawNext, exists := current[fields[i]]; exists {
8186
next, typeOk := rawNext.(map[string]any)
8287
if !typeOk {
83-
return fmt.Errorf("intermediate path %v exists but is of type %T", path, next)
88+
return fmt.Errorf("intermediate path %v exists but is of type %T", path, rawNext)
8489
}
8590
current = next
8691
} else {

internal/autogen/translate/unstructured/unstructured_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,60 @@ func TestSkipKeysAndFieldsOf(t *testing.T) {
207207
})
208208
}
209209
}
210+
211+
func TestAccessOrCreateField(t *testing.T) {
212+
for _, tc := range []struct {
213+
title string
214+
obj map[string]any
215+
path []string
216+
defaultValue any
217+
want any
218+
wantErr string
219+
}{
220+
{
221+
title: "create missing",
222+
obj: map[string]any{},
223+
path: []string{"deep", "field"},
224+
defaultValue: "some string",
225+
want: "some string",
226+
},
227+
{
228+
title: "read existing",
229+
obj: map[string]any{
230+
"deep": map[string]any{
231+
"field": "other string",
232+
},
233+
},
234+
path: []string{"deep", "field"},
235+
defaultValue: "some string",
236+
want: "other string",
237+
},
238+
{
239+
title: "nil object",
240+
obj: nil,
241+
path: []string{"deep", "field"},
242+
defaultValue: "some string",
243+
wantErr: "nil object",
244+
},
245+
{
246+
title: "wrong path type",
247+
obj: map[string]any{
248+
"deep": "an string",
249+
},
250+
path: []string{"deep", "field"},
251+
defaultValue: "some string",
252+
wantErr: "intermediate path [deep] exists but is of type string",
253+
},
254+
} {
255+
t.Run(tc.title, func(t *testing.T) {
256+
got, err := unstructured.AccessOrCreateField(tc.obj, tc.defaultValue, tc.path...)
257+
if tc.wantErr != "" {
258+
require.Nil(t, got)
259+
assert.ErrorContains(t, err, tc.wantErr)
260+
} else {
261+
require.NoError(t, err)
262+
assert.Equal(t, tc.want, got)
263+
}
264+
})
265+
}
266+
}

0 commit comments

Comments
 (0)