From f54869f65f64b22eb297185ffba84b4665861137 Mon Sep 17 00:00:00 2001 From: Omar Hammami <58956785+puertomontt@users.noreply.github.com> Date: Wed, 19 Jul 2023 14:45:12 -0500 Subject: [PATCH] don't recurse on slice element (#468) * don't recurse on slice element * recurse on structs * ignore struct within slice * recurse on pointers --- changelog/v0.31.2/slices.yaml | 5 ++ codegen/doc/helm_values.go | 8 ++- codegen/doc/helm_values_test.go | 116 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 changelog/v0.31.2/slices.yaml diff --git a/changelog/v0.31.2/slices.yaml b/changelog/v0.31.2/slices.yaml new file mode 100644 index 000000000..4e0fb8ce0 --- /dev/null +++ b/changelog/v0.31.2/slices.yaml @@ -0,0 +1,5 @@ +changelog: + - type: NON_USER_FACING + issueLink: https://github.com/solo-io/gloo-mesh-enterprise/issues/10495 + description: > + Don't generate docs entries for slice elements. diff --git a/codegen/doc/helm_values.go b/codegen/doc/helm_values.go index d4e22f590..36a02a41c 100644 --- a/codegen/doc/helm_values.go +++ b/codegen/doc/helm_values.go @@ -138,11 +138,15 @@ func docReflect(addValue addValue, path []string, desc string, typ reflect.Type, // add entry for slice field itself addValue(HelmValue{Key: strings.Join(path, "."), Type: "[]" + typ.Elem().Kind().String(), DefaultValue: valToString(val), Description: desc}) - docReflect(addValue, path, desc, typ.Elem(), reflect.Value{}) + // if element is a struct or pointer, recurse + if typ.Elem().Kind() == reflect.Struct || typ.Elem().Kind() == reflect.Ptr { + docReflect(addValue, path, desc, typ.Elem(), reflect.Value{}) + } case reflect.Struct: // add entry for struct field itself, ignoring the top level struct - if len(path) > 0 { + // also ignore if the struct is inside a slice, since that was recorded in the slice entry + if len(path) > 0 && !strings.Contains(strings.Join(path, "."), "[]") { addValue(HelmValue{Key: strings.Join(path, "."), Type: typ.Kind().String(), DefaultValue: " ", Description: desc}) } diff --git a/codegen/doc/helm_values_test.go b/codegen/doc/helm_values_test.go index 36352c19d..f9663ad35 100644 --- a/codegen/doc/helm_values_test.go +++ b/codegen/doc/helm_values_test.go @@ -79,4 +79,120 @@ var _ = Describe("GenerateHelmValuesDoc", func() { } Expect(result).To(Equal(expected)) }) + + It("handles slices", func() { + type ChildType struct { + Field1 []string `json:"myCoolField" desc:"my field"` + } + + c := ChildType{ + Field1: []string{"default"}, + } + + result := doc.GenerateHelmValuesDoc( + c, + "test", + "my test", + ) + expected := doc.HelmValues{ + { + Key: "test", + Type: "struct", + DefaultValue: " ", + Description: "my test", + }, + { + Key: "test.myCoolField[]", + Type: "[]string", + DefaultValue: "[\"default\"]", + Description: "my field", + }, + } + Expect(result).To(Equal(expected)) + }) + + It("handles slices of structs", func() { + type Nested struct { + Nest []int + } + type ChildType2 struct { + Field2 string `json:"myCoolField2" desc:"my field 2"` + } + type ChildType struct { + Field1 []string `json:"myCoolField" desc:"my field"` + Nested []*Nested `json:"nested" desc:"nested struct"` + } + type Parent struct { + ChildType []ChildType `json:"childType" desc:"child type"` + ChildType2 ChildType2 `json:"childType2" desc:"child type 2"` + } + p := Parent{ + ChildType: []ChildType{ + { + Field1: []string{"default"}, + Nested: []*Nested{ + { + Nest: []int{1, 2, 3}, + }, + { + Nest: []int{4, 5, 6}, + }, + }, + }, + }, + ChildType2: ChildType2{ + Field2: "default", + }, + } + result := doc.GenerateHelmValuesDoc( + p, + "test", + "my test", + ) + expected := doc.HelmValues{ + { + Key: "test", + Type: "struct", + DefaultValue: " ", + Description: "my test", + }, + { + Key: "test.childType[]", + Type: "[]struct", + DefaultValue: "[{\"myCoolField\":[\"default\"],\"nested\":[{\"Nest\":[1,2,3]},{\"Nest\":[4,5,6]}]}]", + Description: "child type", + }, + { + Key: "test.childType[].myCoolField[]", + Type: "[]string", + DefaultValue: " ", + Description: "my field", + }, + { + Key: "test.childType[].nested[]", + Type: "[]ptr", + DefaultValue: " ", + Description: "nested struct", + }, + { + Key: "test.childType[].nested[][]", + Type: "[]int", + DefaultValue: " ", + Description: "", + }, + { + Key: "test.childType2", + Type: "struct", + DefaultValue: " ", + Description: "child type 2", + }, + { + Key: "test.childType2.myCoolField2", + Type: "string", + DefaultValue: "default", + Description: "my field 2", + }, + } + Expect(result).To(Equal(expected)) + }) })