From d069ea4495f522427d7070b48449c2aea23f6d99 Mon Sep 17 00:00:00 2001 From: Komu Wairagu Date: Tue, 21 Feb 2023 19:04:27 +0300 Subject: [PATCH] issues/24: Add tests for embedded structs with tags (#41) - Updates: https://github.com/komuw/kama/issues/24 --- CHANGELOG.md | 1 + vars.go | 5 ++++- vars_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9efb703..d13f12c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/vars.go b/vars.go index d511737..2ab1c25 100644 --- a/vars.go +++ b/vars.go @@ -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++ { @@ -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) } } @@ -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) } diff --git a/vars_test.go b/vars_test.go index 2de0c28..b9f1b9b 100644 --- a/vars_test.go +++ b/vars_test.go @@ -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() @@ -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", + }, }`, }, },