Skip to content

Commit

Permalink
don't recurse on slice element (#468)
Browse files Browse the repository at this point in the history
* don't recurse on slice element

* recurse on structs

* ignore struct within slice

* recurse on pointers
  • Loading branch information
puertomontt authored Jul 19, 2023
1 parent e3f6c13 commit f54869f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog/v0.31.2/slices.yaml
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 6 additions & 2 deletions codegen/doc/helm_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}

Expand Down
116 changes: 116 additions & 0 deletions codegen/doc/helm_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

0 comments on commit f54869f

Please sign in to comment.