Skip to content

feat: Update variable getter to handle nested fields #138

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

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pkg/capi/clustertopology/variables/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,41 @@ import (
"encoding/json"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
)

// Get finds and parses variable to given type.
func Get[T any](
variables map[string]apiextensionsv1.JSON,
name string,
fields ...string,
) (value T, found bool, err error) {
variable, found, err := topologymutation.GetVariable(variables, name)
if err != nil || !found {
return value, found, err
}

err = json.Unmarshal(variable.Raw, &value)
jsonValue := variable.Raw

if len(fields) > 0 {
var unstr map[string]interface{}
err = json.Unmarshal(jsonValue, &unstr)
if err != nil {
return value, found, err
}

nestedField, found, err := unstructured.NestedFieldCopy(unstr, fields...)
if err != nil || !found {
return value, found, err
}

jsonValue, err = json.Marshal(nestedField)
if err != nil {
return value, found, err
}
}

err = json.Unmarshal(jsonValue, &value)
return value, err == nil, err
}
54 changes: 54 additions & 0 deletions pkg/capi/clustertopology/variables/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,57 @@ func TestGetVariable_ParseError(t *testing.T) {
g.Expect(found).To(BeFalse())
g.Expect(parsed).To(BeEmpty())
}

func TestGet_ValidNestedFieldAsStruct(t *testing.T) {
g := NewWithT(t)

type nestedStruct struct {
Bar string `json:"bar"`
}
sampleValue := []byte(`{"foo": {"bar": "baz"}}`)
vars := map[string]apiextensionsv1.JSON{
"sampleVar": {Raw: sampleValue},
}
parsed, found, err := variables.Get[nestedStruct](vars, "sampleVar", "foo")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())
g.Expect(parsed).To(Equal(nestedStruct{
Bar: "baz",
}))
}

func TestGet_ValidNestedFieldAsScalar(t *testing.T) {
g := NewWithT(t)

sampleValue := []byte(`{"foo": {"bar": "baz"}}`)
vars := map[string]apiextensionsv1.JSON{
"sampleVar": {Raw: sampleValue},
}
parsed, found, err := variables.Get[string](vars, "sampleVar", "foo", "bar")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())
g.Expect(parsed).To(Equal("baz"))
}

func TestGet_InvalidNestedFieldType(t *testing.T) {
g := NewWithT(t)

sampleValue := []byte(`{"foo": {"bar": "baz"}}`)
vars := map[string]apiextensionsv1.JSON{
"sampleVar": {Raw: sampleValue},
}
_, _, err := variables.Get[int](vars, "sampleVar", "foo", "bar")
g.Expect(err).To(HaveOccurred())
}

func TestGet_MissingNestedField(t *testing.T) {
g := NewWithT(t)

sampleValue := []byte(`{"foo": {"bar": "baz"}}`)
vars := map[string]apiextensionsv1.JSON{
"sampleVar": {Raw: sampleValue},
}
_, found, err := variables.Get[string](vars, "sampleVar", "foo", "nonexistent")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeFalse())
}