Skip to content

Commit

Permalink
issues/24: Add tests for embedded structs with tags (#41)
Browse files Browse the repository at this point in the history
- Updates: #24
  • Loading branch information
komuw committed Feb 21, 2023
1 parent 406f831 commit d069ea4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Most recent version is listed first.
- Update to Go 1.20: https://github.com/komuw/kama/pull/34
- Better formatting for zero-length slices and maps: https://github.com/komuw/kama/pull/36
- Zero value pointers should not panic: https://github.com/komuw/kama/pull/37
- Add tests for embedded structs with tags: https://github.com/komuw/kama/pull/41

# v0.0.6
- Update dependencies: https://github.com/komuw/kama/pull/32
Expand Down
5 changes: 4 additions & 1 deletion vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func getFields(iType reflect.Type) []string {
// TODO: If a structField happens to be a func,
// We should enhance that signature.
// Look at `dumpFunc()` for implementation
// https://github.com/komuw/kama/issues/38

numFields := iType.NumField()
for i := 0; i < numFields; i++ {
Expand All @@ -192,7 +193,8 @@ func getFields(iType reflect.Type) []string {
// private field
continue
}
fields = append(fields, f.Name+" "+f.Type.String())
name := f.Name + " " + f.Type.String()
fields = append(fields, name)
}
}

Expand Down Expand Up @@ -244,6 +246,7 @@ func getMethods(iType reflect.Type) []string {
// func(main.Foo, int, int) int
// it would be cooler to display as;
// func(main.Foo, price int, commission int) int
// https://github.com/komuw/kama/issues/39
methods = append(methods, methName+" "+methSig)
}

Expand Down
52 changes: 52 additions & 0 deletions vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ type SomeStructWIthSlice struct {

var zeroValuePointer *http.Request

type StructWithTags struct {
Allowed bool `json:"enabled"`
Name string `json:"their_name"`
}

type Hey struct {
Another struct {
Allowed bool `json:"enabled"`
Name string `json:"their_name"`
}
}

func TestBasicVariables(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -395,6 +407,46 @@ func TestBasicVariables(t *testing.T) {
int(18),
int(19),
...<9980 more redacted>..},
}`,
},
},
{
tName: "struct with tags",
variable: StructWithTags{},
expected: vari{
Name: "github.com/komuw/kama.StructWithTags",
Kind: reflect.Struct,
Signature: []string{"kama.StructWithTags", "*kama.StructWithTags"},
Fields: []string{"Allowed bool", "Name string"},
Methods: []string{},
Val: `StructWithTags{
Allowed: false,
Name: "",
}`,
},
},
{
tName: "embedded struct with tags",
variable: Hey{Another: struct {
Allowed bool `json:"enabled"`
Name string `json:"their_name"`
}{
Allowed: true,
Name: "Jane",
}},
expected: vari{
Name: "github.com/komuw/kama.Hey",
Kind: reflect.Struct,
Signature: []string{"kama.Hey", "*kama.Hey"},
Fields: []string{
`Another struct { Allowed bool "json:\"enabled\""; Name string "json:\"their_name\"" }`,
},
Methods: []string{},
Val: `Hey{
Another: {
Allowed: true,
Name: "Jane",
},
}`,
},
},
Expand Down

0 comments on commit d069ea4

Please sign in to comment.