Skip to content

Commit 918c505

Browse files
authored
Rename crapi's objmap package (#3035)
1 parent 120e627 commit 918c505

File tree

11 files changed

+98
-98
lines changed

11 files changed

+98
-98
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515

16-
package unstructured
16+
package objmap
1717

1818
import (
1919
"errors"
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515

16-
package unstructured_test
16+
package objmap_test
1717

1818
import (
1919
"sort"
@@ -22,15 +22,15 @@ import (
2222
"github.com/stretchr/testify/assert"
2323
"github.com/stretchr/testify/require"
2424

25-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/unstructured"
25+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/objmap"
2626
)
2727

2828
func TestCreateAndAccessField(t *testing.T) {
29-
sampleMap, err := unstructured.ToUnstructured(sample)
29+
sampleMap, err := objmap.ToObjectMap(sample)
3030
require.NoError(t, err)
3131
want := "value"
32-
require.NoError(t, unstructured.RecursiveCreateField(sampleMap, "value", "SubObj", "field"))
33-
got, err := unstructured.GetField[string](sampleMap, "SubObj", "field")
32+
require.NoError(t, objmap.RecursiveCreateField(sampleMap, "value", "SubObj", "field"))
33+
got, err := objmap.GetField[string](sampleMap, "SubObj", "field")
3434
require.NoError(t, err)
3535
assert.Equal(t, want, got)
3636
}
@@ -152,7 +152,7 @@ func TestGetOrCreateField(t *testing.T) {
152152
},
153153
} {
154154
t.Run(tc.title, func(t *testing.T) {
155-
got, err := unstructured.GetOrCreateField(tc.obj, tc.defaultValue, tc.path...)
155+
got, err := objmap.GetOrCreateField(tc.obj, tc.defaultValue, tc.path...)
156156
if tc.wantErr != "" {
157157
require.Nil(t, got)
158158
assert.ErrorContains(t, err, tc.wantErr)
@@ -186,7 +186,7 @@ func TestRecursiveCreateField(t *testing.T) {
186186
},
187187
} {
188188
t.Run(tc.title, func(t *testing.T) {
189-
err := unstructured.RecursiveCreateField(tc.obj, tc.value, tc.path...)
189+
err := objmap.RecursiveCreateField(tc.obj, tc.value, tc.path...)
190190
if tc.wantErr == "" {
191191
assert.NoError(t, err)
192192
assert.Equal(t, tc.want, tc.obj)
@@ -272,7 +272,7 @@ func TestGetFieldObject(t *testing.T) {
272272
},
273273
} {
274274
t.Run(tc.title, func(t *testing.T) {
275-
got, err := unstructured.GetFieldObject(tc.obj, tc.path...)
275+
got, err := objmap.GetFieldObject(tc.obj, tc.path...)
276276
if tc.wantErr == "" {
277277
assert.NoError(t, err)
278278
assert.Equal(t, tc.want, got)
@@ -284,9 +284,9 @@ func TestGetFieldObject(t *testing.T) {
284284
}
285285

286286
func TestFieldsOf(t *testing.T) {
287-
sampleMap, err := unstructured.ToUnstructured(sample)
287+
sampleMap, err := objmap.ToObjectMap(sample)
288288
require.NoError(t, err)
289-
fields := unstructured.FieldsOf(sampleMap)
289+
fields := objmap.FieldsOf(sampleMap)
290290
want := []string{"SubStruct", "ID", "YesNo", "Text", "Data", "Timestamp"}
291291
sort.Strings(want)
292292
sort.Strings(fields)

internal/crapi/unstructured/unstructured.go renamed to internal/crapi/objmap/objmap.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515

16-
package unstructured
16+
package objmap
1717

1818
import (
1919
"encoding/json"
@@ -35,9 +35,9 @@ var (
3535
ErrNotArray = errors.New("not an array")
3636
)
3737

38-
// ToUnstructured returns an unstructured map holding the public field values
38+
// ToObjectMap returns an object map holding the public field values
3939
// from the original input obj value
40-
func ToUnstructured(obj any) (map[string]any, error) {
40+
func ToObjectMap(obj any) (map[string]any, error) {
4141
js, err := json.Marshal(obj)
4242
if err != nil {
4343
return nil, fmt.Errorf("failed to marshal object into JSON: %w", err)
@@ -49,9 +49,9 @@ func ToUnstructured(obj any) (map[string]any, error) {
4949
return result, nil
5050
}
5151

52-
// FromUnstructured fills a target value with the field values from an
53-
// unstructured map
54-
func FromUnstructured(target any, source map[string]any) error {
52+
// FromObjectMap fills a target value with the field values from an
53+
// object map
54+
func FromObjectMap(target any, source map[string]any) error {
5555
js, err := json.Marshal(source)
5656
if err != nil {
5757
return fmt.Errorf("failed to marshal map into JSON: %w", err)

internal/crapi/unstructured/unstructured_test.go renamed to internal/crapi/objmap/objmap_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515

16-
package unstructured_test
16+
package objmap_test
1717

1818
import (
1919
"testing"
@@ -23,7 +23,7 @@ import (
2323
"github.com/stretchr/testify/require"
2424
admin2025 "go.mongodb.org/atlas-sdk/v20250312009/admin"
2525

26-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/unstructured"
26+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/objmap"
2727
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
2828
)
2929

@@ -69,15 +69,15 @@ var sample = testStruct{
6969
}
7070

7171
func TestParamsFill(t *testing.T) {
72-
unstructuredSample := map[string]any{
72+
objMapSample := map[string]any{
7373
"groupid": "62b6e34b3d91647abb20e7b8",
7474
"groupalertsconfig": map[string]any{
7575
"enabled": true,
7676
"eventtypename": "some-event",
7777
},
7878
}
7979
result := admin2025.CreateAlertConfigApiParams{}
80-
require.NoError(t, unstructured.FromUnstructured(&result, unstructuredSample))
80+
require.NoError(t, objmap.FromObjectMap(&result, objMapSample))
8181
assert.Equal(t, admin2025.CreateAlertConfigApiParams{
8282
GroupId: "62b6e34b3d91647abb20e7b8",
8383
GroupAlertsConfig: &admin2025.GroupAlertsConfig{
@@ -87,12 +87,12 @@ func TestParamsFill(t *testing.T) {
8787
}, result)
8888
}
8989

90-
func TestToAndFromAndCopyUnstructured(t *testing.T) {
91-
sampleMap, err := unstructured.ToUnstructured(sample)
90+
func TestToAndFromAndCopyObjectMap(t *testing.T) {
91+
sampleMap, err := objmap.ToObjectMap(sample)
9292
require.NoError(t, err)
9393
clone := map[string]any{}
94-
unstructured.CopyFields(clone, sampleMap)
94+
objmap.CopyFields(clone, sampleMap)
9595
result := testStruct{}
96-
require.NoError(t, unstructured.FromUnstructured(&result, sampleMap))
96+
require.NoError(t, objmap.FromObjectMap(&result, sampleMap))
9797
assert.Equal(t, sample, result)
9898
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515

16-
package unstructured
16+
package objmap
1717

1818
import "strings"
1919

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
// limitations under the License.
1414
//
1515

16-
package unstructured_test
16+
package objmap_test
1717

1818
import (
1919
"testing"
2020

2121
"github.com/stretchr/testify/assert"
2222

23-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/unstructured"
23+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/objmap"
2424
)
2525

2626
func TestAsPath(t *testing.T) {
@@ -51,7 +51,7 @@ func TestAsPath(t *testing.T) {
5151
},
5252
} {
5353
t.Run(tc.title, func(t *testing.T) {
54-
assert.Equal(t, tc.want, unstructured.AsPath(tc.input))
54+
assert.Equal(t, tc.want, objmap.AsPath(tc.input))
5555
})
5656
}
5757
}
@@ -79,7 +79,7 @@ func TestBase(t *testing.T) {
7979
},
8080
} {
8181
t.Run(tc.title, func(t *testing.T) {
82-
assert.Equal(t, tc.want, unstructured.Base(tc.input))
82+
assert.Equal(t, tc.want, objmap.Base(tc.input))
8383
})
8484
}
8585
}

internal/crapi/refs/kube.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"k8s.io/apimachinery/pkg/runtime/schema"
2424
"sigs.k8s.io/controller-runtime/pkg/client"
2525

26-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/unstructured"
26+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/objmap"
2727
)
2828

2929
// ErrNoMatchingPropertySelector when no property selectors matched the value
@@ -66,7 +66,7 @@ func (km KubeMapping) fetchReferencedValue(mc *kubeset, target string, reference
6666
if refPath == "" {
6767
return nil, fmt.Errorf("cannot solve reference without a %s.nameSelector", xKubeMappingKey)
6868
}
69-
refName, err := unstructured.GetField[string](reference, unstructured.AsPath(refPath)...)
69+
refName, err := objmap.GetField[string](reference, objmap.AsPath(refPath)...)
7070
if err != nil {
7171
return nil, fmt.Errorf("failed to access field %q at %v: %w", refPath, reference, err)
7272
}
@@ -85,18 +85,18 @@ func (km KubeMapping) fetchReferencedValue(mc *kubeset, target string, reference
8585
if km.Type.Kind != "" && !km.equal(gvk) {
8686
return nil, fmt.Errorf("resource %q had to be a %q but got %q", refName, km.gvk(), gvk)
8787
}
88-
resourceMap, err := unstructured.ToUnstructured(resource)
88+
resourceMap, err := objmap.ToObjectMap(resource)
8989
if err != nil {
90-
return nil, fmt.Errorf("failed to turn resource %q into an unstuctued map: %w", refName, err)
90+
return nil, fmt.Errorf("failed to turn resource %q into an object map: %w", refName, err)
9191
}
9292
value, err := km.fetchFromProperties(resourceMap)
93-
if err != nil && !errors.Is(err, unstructured.ErrNotFound) {
93+
if err != nil && !errors.Is(err, objmap.ErrNotFound) {
9494
return nil, fmt.Errorf("failed to resolve reference properties: %w", err)
9595
}
96-
if errors.Is(err, unstructured.ErrNotFound) {
96+
if errors.Is(err, objmap.ErrNotFound) {
9797
var err error
9898
value, err = km.fetchFromPropertySelectors(resourceMap, target)
99-
if errors.Is(err, unstructured.ErrNotFound) {
99+
if errors.Is(err, objmap.ErrNotFound) {
100100
return nil, fmt.Errorf("failed to resolve reference properties or property selectors: %w", err)
101101
}
102102
if err != nil {
@@ -126,16 +126,16 @@ func (km KubeMapping) encode(refSolver *resolver, value any) (any, error) {
126126
func (km KubeMapping) fetchFromProperties(resource map[string]any) (any, error) {
127127
for _, prop := range km.Properties {
128128
path := resolveXPath(prop)
129-
value, err := unstructured.GetField[any](resource, path...)
130-
if errors.Is(err, unstructured.ErrNotFound) {
129+
value, err := objmap.GetField[any](resource, path...)
130+
if errors.Is(err, objmap.ErrNotFound) {
131131
continue
132132
}
133133
if err != nil {
134134
return nil, fmt.Errorf("failed to access property as %v: %w", path, err)
135135
}
136136
return value, nil
137137
}
138-
return nil, unstructured.ErrNotFound
138+
return nil, objmap.ErrNotFound
139139
}
140140

141141
func (km KubeMapping) fetchFromPropertySelectors(resource map[string]any, target string) (any, error) {
@@ -145,42 +145,42 @@ func (km KubeMapping) fetchFromPropertySelectors(resource map[string]any, target
145145
prop = fmt.Sprintf("%s.%s", prop[:len(prop)-2], target)
146146
}
147147
path := resolveXPath(prop)
148-
value, err := unstructured.GetField[any](resource, path...)
149-
if errors.Is(err, unstructured.ErrNotFound) {
148+
value, err := objmap.GetField[any](resource, path...)
149+
if errors.Is(err, objmap.ErrNotFound) {
150150
continue
151151
}
152152
if err != nil {
153153
return nil, fmt.Errorf("failed to access selected property as %v: %w", path, err)
154154
}
155155
return value, nil
156156
}
157-
return nil, unstructured.ErrNotFound
157+
return nil, objmap.ErrNotFound
158158
}
159159

160160
func (km KubeMapping) setAtPropertySelectors(refSolver *resolver, gvr string, obj map[string]any, target string, value any) (client.Object, error) {
161161
for _, selector := range km.PropertySelectors {
162162
prop := selector
163163
if strings.HasSuffix(prop, propertySelectorSuffix) {
164164
targetPath := resolveXPath(target)
165-
prop = fmt.Sprintf("%s.%s", prop[:len(prop)-2], unstructured.Base(targetPath))
165+
prop = fmt.Sprintf("%s.%s", prop[:len(prop)-2], objmap.Base(targetPath))
166166
}
167167
path := resolveXPath(prop)
168-
if err := unstructured.RecursiveCreateField(obj, value, path...); err != nil {
168+
if err := objmap.RecursiveCreateField(obj, value, path...); err != nil {
169169
return nil, fmt.Errorf("failed to set value at %q: %w", path, err)
170170
}
171171
obj, err := initializedKubeObjectFor(refSolver, gvr, obj)
172172
if err != nil {
173173
return nil, fmt.Errorf("failed to initialize Kubernetes object: %w", err)
174174
}
175-
unstructuredCopy, err := unstructured.ToUnstructured(obj)
175+
objMapCopy, err := objmap.ToObjectMap(obj)
176176
if err != nil {
177177
return nil, fmt.Errorf("failed to read Kubernetes object contents: %w", err)
178178
}
179-
valueCopy, err := unstructured.GetField[any](unstructuredCopy, path...)
179+
valueCopy, err := objmap.GetField[any](objMapCopy, path...)
180180
if reflect.DeepEqual(value, valueCopy) {
181181
return obj, nil
182182
}
183-
if err != nil && !errors.Is(err, unstructured.ErrNotFound) {
183+
if err != nil && !errors.Is(err, objmap.ErrNotFound) {
184184
return nil, fmt.Errorf("failed to check Kubernetes object contents: %w", err)
185185
}
186186
}

internal/crapi/refs/openapi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package refs
1717
import (
1818
"strings"
1919

20-
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/unstructured"
20+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/crapi/objmap"
2121
)
2222

2323
type OpenAPIMapping struct {
@@ -31,7 +31,7 @@ func (oam OpenAPIMapping) targetPath() []string {
3131

3232
func resolveXPath(xpath string) []string {
3333
if strings.HasPrefix(xpath, "$.") {
34-
return unstructured.AsPath(xpath[1:])
34+
return objmap.AsPath(xpath[1:])
3535
}
36-
return unstructured.AsPath(xpath)
36+
return objmap.AsPath(xpath)
3737
}

0 commit comments

Comments
 (0)