From ff2b2371885b9d1745a6fa28bbd5029b04620a6a Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 13:25:06 +0300 Subject: [PATCH 01/10] m --- ...TestCircularRef-DoNotShowPrivateFields.txt | 13 +++++ vars_test.go | 53 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt diff --git a/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt new file mode 100644 index 0000000..5819e95 --- /dev/null +++ b/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt @@ -0,0 +1,13 @@ + +[ +NAME: github.com/komuw/kama.ClientTestCircularRef +KIND: struct +SIGNATURE: [*kama.ClientTestCircularRef kama.ClientTestCircularRef] +FIELDS: [ + Public string + ] +METHODS: [] +SNIPPET: &ClientTestCircularRef{ + Public: "PublicName", +} +] diff --git a/vars_test.go b/vars_test.go index b9aa707..aeec3d7 100644 --- a/vars_test.go +++ b/vars_test.go @@ -560,3 +560,56 @@ func TestPublicPrivate(t *testing.T) { }) } } + +type ConfTestCircularRef struct { + Path string + Hclient *http.Client +} + +type ClientTestCircularRef struct { + Public string + c *ConfTestCircularRef + s srvTestCircularRef +} + +type srvTestCircularRef struct { + cli *ClientTestCircularRef +} + +func TestCircularRef(t *testing.T) { + // t.Parallel() // This cannot be ran in Parallel since it mutates a global var. + + oldCfg := cfg + + x := &ClientTestCircularRef{Public: "PublicName", c: &ConfTestCircularRef{Path: "path", Hclient: http.DefaultClient}} + x.s.cli = x + + { // Set the new config and schedule to return old config. + onceCfg = &sync.Once{} + t.Cleanup(func() { + cfg = oldCfg + }) + } + + for _, name := range []string{"ShowPrivateFields", "DoNotShowPrivateFields"} { + name := name + tName := fmt.Sprintf("TestCircularRef-%s", name) + + t.Run(tName, func(t *testing.T) { + // t.Parallel() // This cannot be ran in Parallel since it mutates a global var. + + var res string + switch name { + default: + t.Fatalf("option `%s` is not expected", name) + case "ShowPrivateFields": + res = Dir(x, Config{ShowPrivateFields: true}) + case "DoNotShowPrivateFields": + res = Dir(x, Config{ShowPrivateFields: false}) + } + + path := getDataPath(t, "vars_test.go", tName) + dealWithTestData(t, path, res) + }) + } +} From 78245fac0f8d3ffdc6ada74db5740c3a5d810af7 Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 13:58:33 +0300 Subject: [PATCH 02/10] m --- dump.go | 18 +++++++++++ .../TestCircularRef-ShowPrivateFields.txt | 31 +++++++++++++++++++ vars_test.go | 23 +++++++------- 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 testdata/vars_test/TestCircularRef-ShowPrivateFields.txt diff --git a/dump.go b/dump.go index da41548..9aebfdc 100644 --- a/dump.go +++ b/dump.go @@ -179,6 +179,21 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) typeName = "&" + typeName } + pointer := uintptr(0) + if isPointerValue(v) { + pointer = v.Pointer() + } + fmt.Println("dumpStruct. ", + "v: ", v, + "fromPtr: ", fromPtr, + "indentLevel: ", indentLevel, + "typeName: ", typeName, + "pointer: ", pointer, + ) + if indentLevel > 8 { + return typeName + " bad indentLevel: " + fmt.Sprint(indentLevel) + } + sep := "\n" fieldNameSep := strings.Repeat(" ", indentLevel) lastBracketSep := strings.Repeat(" ", indentLevel-1) @@ -201,6 +216,9 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) _ = cpt hzv := true val := dump(fieldd, hzv, indentLevel) + if strings.HasPrefix(val, typeName) { + val = "same thing" + } s = s + fieldNameSep + vtf.Name + ": " + val + fmt.Sprintf(",%s", sep) } } diff --git a/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt new file mode 100644 index 0000000..fa3d116 --- /dev/null +++ b/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt @@ -0,0 +1,31 @@ + +[ +NAME: github.com/komuw/kama.Client +KIND: struct +SIGNATURE: [*kama.Client kama.Client] +FIELDS: [ + Public string + ] +METHODS: [] +SNIPPET: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client bad indentLevel: 9, + }, + }, + }, + }, + }, + }, + }, +} +] diff --git a/vars_test.go b/vars_test.go index aeec3d7..e2b5665 100644 --- a/vars_test.go +++ b/vars_test.go @@ -561,19 +561,17 @@ func TestPublicPrivate(t *testing.T) { } } -type ConfTestCircularRef struct { - Path string - Hclient *http.Client -} +// type Conf struct { +// Path string +// } -type ClientTestCircularRef struct { +type Client struct { Public string - c *ConfTestCircularRef - s srvTestCircularRef + srv srvRef } -type srvTestCircularRef struct { - cli *ClientTestCircularRef +type srvRef struct { + cli *Client } func TestCircularRef(t *testing.T) { @@ -581,8 +579,11 @@ func TestCircularRef(t *testing.T) { oldCfg := cfg - x := &ClientTestCircularRef{Public: "PublicName", c: &ConfTestCircularRef{Path: "path", Hclient: http.DefaultClient}} - x.s.cli = x + x := &Client{ + Public: "PublicName", + // cfg: &Conf{Path: "path"}, + } + x.srv.cli = x { // Set the new config and schedule to return old config. onceCfg = &sync.Once{} From a4877a50b7454537aef0e4b6ab3cdbb5c48cdc1b Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 14:01:08 +0300 Subject: [PATCH 03/10] m --- vars_test.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/vars_test.go b/vars_test.go index e2b5665..8305cc0 100644 --- a/vars_test.go +++ b/vars_test.go @@ -561,10 +561,6 @@ func TestPublicPrivate(t *testing.T) { } } -// type Conf struct { -// Path string -// } - type Client struct { Public string srv srvRef @@ -579,10 +575,7 @@ func TestCircularRef(t *testing.T) { oldCfg := cfg - x := &Client{ - Public: "PublicName", - // cfg: &Conf{Path: "path"}, - } + x := &Client{Public: "PublicName"} x.srv.cli = x { // Set the new config and schedule to return old config. From ac1a29a2b390b7e64dfa2094b4b8ffd092015494 Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 14:52:56 +0300 Subject: [PATCH 04/10] m --- dump.go | 18 +++--------------- .../TestCircularRef-ShowPrivateFields.txt | 7 ++++++- .../vars_test/embedded_struct_with_tags.txt | 4 ++-- vars_test.go | 1 + 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/dump.go b/dump.go index 9aebfdc..453aa9d 100644 --- a/dump.go +++ b/dump.go @@ -105,6 +105,8 @@ func dump(val reflect.Value, hideZeroValues bool, indentLevel int) string { if v.IsValid() { if v.Type().Kind() == reflect.Struct { fromPtr := true + // TODO: should we pass in `val`, itself instead of `v` + // that way `val.Pointer()` would happen inside `dumpStruct` return dumpStruct(v, fromPtr, hideZeroValues, indentLevel) } else { return dumpNonStructPointer(v, hideZeroValues, indentLevel) @@ -179,18 +181,7 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) typeName = "&" + typeName } - pointer := uintptr(0) - if isPointerValue(v) { - pointer = v.Pointer() - } - fmt.Println("dumpStruct. ", - "v: ", v, - "fromPtr: ", fromPtr, - "indentLevel: ", indentLevel, - "typeName: ", typeName, - "pointer: ", pointer, - ) - if indentLevel > 8 { + if indentLevel > 10 { return typeName + " bad indentLevel: " + fmt.Sprint(indentLevel) } @@ -216,9 +207,6 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) _ = cpt hzv := true val := dump(fieldd, hzv, indentLevel) - if strings.HasPrefix(val, typeName) { - val = "same thing" - } s = s + fieldNameSep + vtf.Name + ": " + val + fmt.Sprintf(",%s", sep) } } diff --git a/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt index fa3d116..ad2bd48 100644 --- a/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt @@ -19,7 +19,12 @@ SNIPPET: &Client{ cli: &Client{ Public: "PublicName", srv: srvRef{ - cli: &Client bad indentLevel: 9, + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client bad indentLevel: 11, + }, + }, }, }, }, diff --git a/testdata/vars_test/embedded_struct_with_tags.txt b/testdata/vars_test/embedded_struct_with_tags.txt index c72dab8..e00840f 100644 --- a/testdata/vars_test/embedded_struct_with_tags.txt +++ b/testdata/vars_test/embedded_struct_with_tags.txt @@ -9,8 +9,8 @@ FIELDS: [ METHODS: [] SNIPPET: Hey{ Another: { - Allowed: true, - Name: "Jane", + Allowed: same thing, + Name: same thing, }, } ] diff --git a/vars_test.go b/vars_test.go index 8305cc0..fe2ce92 100644 --- a/vars_test.go +++ b/vars_test.go @@ -577,6 +577,7 @@ func TestCircularRef(t *testing.T) { x := &Client{Public: "PublicName"} x.srv.cli = x + // x.srv.cli = &Client{Public: "NewPub"} // TODO: also test this. { // Set the new config and schedule to return old config. onceCfg = &sync.Once{} From 14b7f0e1db27baa27691c9856b96048adc9d07c2 Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 14:59:35 +0300 Subject: [PATCH 05/10] m --- dump.go | 4 ++-- kama.go | 10 ++++++++++ .../TestCircularRef-ShowPrivateFields.txt | 14 +------------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/dump.go b/dump.go index 453aa9d..a2f72ae 100644 --- a/dump.go +++ b/dump.go @@ -181,8 +181,8 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) typeName = "&" + typeName } - if indentLevel > 10 { - return typeName + " bad indentLevel: " + fmt.Sprint(indentLevel) + if indentLevel > cfg.MaxIndentLevel { + return fmt.Sprintf("%v: kama warning(indentation `%d` exceeds max of `%d`. Possible circular reference)", typeName, indentLevel, cfg.MaxIndentLevel) } sep := "\n" diff --git a/kama.go b/kama.go index 8bfe238..bbac3de 100644 --- a/kama.go +++ b/kama.go @@ -28,6 +28,9 @@ type Config struct { MaxLength int // ShowPrivateFields dictates whether private struct fields will be dumped. ShowPrivateFields bool + // MaxIndentLevel is the maximum level of indentation/recursiveness to dump to. + // This is especially important to set if the thing you are dumping has circular references. + MaxIndentLevel int } // Dirp prints (to stdout) exported information of types, variables, packages, modules, imports @@ -59,6 +62,13 @@ func Dir(i interface{}, c ...Config) string { // https://github.com/golang/go/issues/38673#issuecomment-643885108 cfg.MaxLength = 10_000 } + + if cfg.MaxIndentLevel < 1 { + cfg.MaxIndentLevel = 5 // ie, the default + } + if cfg.MaxIndentLevel > 100 { + cfg.MaxIndentLevel = 100 + } }) } diff --git a/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt index ad2bd48..75b2b8e 100644 --- a/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt @@ -15,19 +15,7 @@ SNIPPET: &Client{ srv: srvRef{ cli: &Client{ Public: "PublicName", - srv: srvRef{ - cli: &Client{ - Public: "PublicName", - srv: srvRef{ - cli: &Client{ - Public: "PublicName", - srv: srvRef{ - cli: &Client bad indentLevel: 11, - }, - }, - }, - }, - }, + srv: srvRef: kama warning(indentation `6` exceeds max of `5`. Possible circular reference), }, }, }, From 22ddc2e5550b37df401d9af14b0262a92de8ce7c Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 15:05:10 +0300 Subject: [PATCH 06/10] m --- kama.go | 9 +++++++-- .../TestCircularRef-DoNotShowPrivateFields.txt | 17 ++++++++++++++--- .../vars_test/embedded_struct_with_tags.txt | 4 ++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/kama.go b/kama.go index bbac3de..cff7002 100644 --- a/kama.go +++ b/kama.go @@ -18,8 +18,13 @@ import ( ) var ( - cfg = Config{MaxLength: 14} //nolint:gochecknoglobals - onceCfg = &sync.Once{} //nolint:gochecknoglobals + cfg = Config{ + MaxLength: 14, + ShowPrivateFields: false, + MaxIndentLevel: 5, + } //nolint:gochecknoglobals + + onceCfg = &sync.Once{} //nolint:gochecknoglobals ) // Config controls how printing is going to be done. diff --git a/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt index 5819e95..75b2b8e 100644 --- a/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt @@ -1,13 +1,24 @@ [ -NAME: github.com/komuw/kama.ClientTestCircularRef +NAME: github.com/komuw/kama.Client KIND: struct -SIGNATURE: [*kama.ClientTestCircularRef kama.ClientTestCircularRef] +SIGNATURE: [*kama.Client kama.Client] FIELDS: [ Public string ] METHODS: [] -SNIPPET: &ClientTestCircularRef{ +SNIPPET: &Client{ Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef: kama warning(indentation `6` exceeds max of `5`. Possible circular reference), + }, + }, + }, + }, } ] diff --git a/testdata/vars_test/embedded_struct_with_tags.txt b/testdata/vars_test/embedded_struct_with_tags.txt index e00840f..c72dab8 100644 --- a/testdata/vars_test/embedded_struct_with_tags.txt +++ b/testdata/vars_test/embedded_struct_with_tags.txt @@ -9,8 +9,8 @@ FIELDS: [ METHODS: [] SNIPPET: Hey{ Another: { - Allowed: same thing, - Name: same thing, + Allowed: true, + Name: "Jane", }, } ] From e7dccd684331a11cbd6a73e3a6012ffcdf2fa92e Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 15:07:55 +0300 Subject: [PATCH 07/10] m --- ...-with-NO-cirlce-DoNotShowPrivateFields.txt | 19 +++++++++++ ...arRef-with-NO-cirlce-ShowPrivateFields.txt | 19 +++++++++++ ...ef-with-cirlce-DoNotShowPrivateFields.txt} | 0 ...ularRef-with-cirlce-ShowPrivateFields.txt} | 0 vars_test.go | 34 ++++++++++++++++--- 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt create mode 100644 testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt rename testdata/vars_test/{TestCircularRef-DoNotShowPrivateFields.txt => TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt} (100%) rename testdata/vars_test/{TestCircularRef-ShowPrivateFields.txt => TestCircularRef-with-cirlce-ShowPrivateFields.txt} (100%) diff --git a/testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt new file mode 100644 index 0000000..a1c630c --- /dev/null +++ b/testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt @@ -0,0 +1,19 @@ + +[ +NAME: github.com/komuw/kama.Client +KIND: struct +SIGNATURE: [*kama.Client kama.Client] +FIELDS: [ + Public string + ] +METHODS: [] +SNIPPET: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "NewPubNamw", + srv: srvRef{}, + }, + }, +} +] diff --git a/testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt new file mode 100644 index 0000000..a1c630c --- /dev/null +++ b/testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt @@ -0,0 +1,19 @@ + +[ +NAME: github.com/komuw/kama.Client +KIND: struct +SIGNATURE: [*kama.Client kama.Client] +FIELDS: [ + Public string + ] +METHODS: [] +SNIPPET: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "NewPubNamw", + srv: srvRef{}, + }, + }, +} +] diff --git a/testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt similarity index 100% rename from testdata/vars_test/TestCircularRef-DoNotShowPrivateFields.txt rename to testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt diff --git a/testdata/vars_test/TestCircularRef-ShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-cirlce-ShowPrivateFields.txt similarity index 100% rename from testdata/vars_test/TestCircularRef-ShowPrivateFields.txt rename to testdata/vars_test/TestCircularRef-with-cirlce-ShowPrivateFields.txt diff --git a/vars_test.go b/vars_test.go index fe2ce92..f9c182b 100644 --- a/vars_test.go +++ b/vars_test.go @@ -575,10 +575,6 @@ func TestCircularRef(t *testing.T) { oldCfg := cfg - x := &Client{Public: "PublicName"} - x.srv.cli = x - // x.srv.cli = &Client{Public: "NewPub"} // TODO: also test this. - { // Set the new config and schedule to return old config. onceCfg = &sync.Once{} t.Cleanup(func() { @@ -588,7 +584,35 @@ func TestCircularRef(t *testing.T) { for _, name := range []string{"ShowPrivateFields", "DoNotShowPrivateFields"} { name := name - tName := fmt.Sprintf("TestCircularRef-%s", name) + tName := fmt.Sprintf("TestCircularRef-with-cirlce-%s", name) + + x := &Client{Public: "PublicName"} + x.srv.cli = x // circular + + t.Run(tName, func(t *testing.T) { + // t.Parallel() // This cannot be ran in Parallel since it mutates a global var. + + var res string + switch name { + default: + t.Fatalf("option `%s` is not expected", name) + case "ShowPrivateFields": + res = Dir(x, Config{ShowPrivateFields: true}) + case "DoNotShowPrivateFields": + res = Dir(x, Config{ShowPrivateFields: false}) + } + + path := getDataPath(t, "vars_test.go", tName) + dealWithTestData(t, path, res) + }) + } + + for _, name := range []string{"ShowPrivateFields", "DoNotShowPrivateFields"} { + name := name + tName := fmt.Sprintf("TestCircularRef-with-NO-cirlce-%s", name) + + x := &Client{Public: "PublicName"} + x.srv.cli = &Client{Public: "NewPubNamw"} // no circle t.Run(tName, func(t *testing.T) { // t.Parallel() // This cannot be ran in Parallel since it mutates a global var. From 5c05b7ec022c510fe04cae20e068abb0de1dc80c Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 15:11:45 +0300 Subject: [PATCH 08/10] m --- kama.go | 4 ++-- ...cularRef-with-cirlce-DoNotShowPrivateFields.txt | 14 +++++++++++++- ...stCircularRef-with-cirlce-ShowPrivateFields.txt | 14 +++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/kama.go b/kama.go index cff7002..d69bda6 100644 --- a/kama.go +++ b/kama.go @@ -21,7 +21,7 @@ var ( cfg = Config{ MaxLength: 14, ShowPrivateFields: false, - MaxIndentLevel: 5, + MaxIndentLevel: 10, } //nolint:gochecknoglobals onceCfg = &sync.Once{} //nolint:gochecknoglobals @@ -69,7 +69,7 @@ func Dir(i interface{}, c ...Config) string { } if cfg.MaxIndentLevel < 1 { - cfg.MaxIndentLevel = 5 // ie, the default + cfg.MaxIndentLevel = 10 // ie, the default } if cfg.MaxIndentLevel > 100 { cfg.MaxIndentLevel = 100 diff --git a/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt index 75b2b8e..0e59198 100644 --- a/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt @@ -15,7 +15,19 @@ SNIPPET: &Client{ srv: srvRef{ cli: &Client{ Public: "PublicName", - srv: srvRef: kama warning(indentation `6` exceeds max of `5`. Possible circular reference), + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client: kama warning(indentation `11` exceeds max of `10`. Possible circular reference), + }, + }, + }, + }, + }, }, }, }, diff --git a/testdata/vars_test/TestCircularRef-with-cirlce-ShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-cirlce-ShowPrivateFields.txt index 75b2b8e..0e59198 100644 --- a/testdata/vars_test/TestCircularRef-with-cirlce-ShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-with-cirlce-ShowPrivateFields.txt @@ -15,7 +15,19 @@ SNIPPET: &Client{ srv: srvRef{ cli: &Client{ Public: "PublicName", - srv: srvRef: kama warning(indentation `6` exceeds max of `5`. Possible circular reference), + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client{ + Public: "PublicName", + srv: srvRef{ + cli: &Client: kama warning(indentation `11` exceeds max of `10`. Possible circular reference), + }, + }, + }, + }, + }, }, }, }, From bc6d2cd0bca5bb2022850fccd21b42a3f191dbdd Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 15:18:32 +0300 Subject: [PATCH 09/10] m --- kama.go | 4 ++-- ...-with-NO-cirlce-DoNotShowPrivateFields.txt | 6 ----- ...arRef-with-NO-cirlce-ShowPrivateFields.txt | 2 +- ...Ref-with-cirlce-DoNotShowPrivateFields.txt | 23 ------------------- vars_test.go | 16 ++++++++++++- 5 files changed, 18 insertions(+), 33 deletions(-) diff --git a/kama.go b/kama.go index d69bda6..37e52de 100644 --- a/kama.go +++ b/kama.go @@ -18,11 +18,11 @@ import ( ) var ( - cfg = Config{ + cfg = Config{ //nolint:gochecknoglobals MaxLength: 14, ShowPrivateFields: false, MaxIndentLevel: 10, - } //nolint:gochecknoglobals + } onceCfg = &sync.Once{} //nolint:gochecknoglobals ) diff --git a/testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt index a1c630c..0d60341 100644 --- a/testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-with-NO-cirlce-DoNotShowPrivateFields.txt @@ -9,11 +9,5 @@ FIELDS: [ METHODS: [] SNIPPET: &Client{ Public: "PublicName", - srv: srvRef{ - cli: &Client{ - Public: "NewPubNamw", - srv: srvRef{}, - }, - }, } ] diff --git a/testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt index a1c630c..8782d76 100644 --- a/testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-with-NO-cirlce-ShowPrivateFields.txt @@ -11,7 +11,7 @@ SNIPPET: &Client{ Public: "PublicName", srv: srvRef{ cli: &Client{ - Public: "NewPubNamw", + Public: "NewPubName", srv: srvRef{}, }, }, diff --git a/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt b/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt index 0e59198..0d60341 100644 --- a/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt +++ b/testdata/vars_test/TestCircularRef-with-cirlce-DoNotShowPrivateFields.txt @@ -9,28 +9,5 @@ FIELDS: [ METHODS: [] SNIPPET: &Client{ Public: "PublicName", - srv: srvRef{ - cli: &Client{ - Public: "PublicName", - srv: srvRef{ - cli: &Client{ - Public: "PublicName", - srv: srvRef{ - cli: &Client{ - Public: "PublicName", - srv: srvRef{ - cli: &Client{ - Public: "PublicName", - srv: srvRef{ - cli: &Client: kama warning(indentation `11` exceeds max of `10`. Possible circular reference), - }, - }, - }, - }, - }, - }, - }, - }, - }, } ] diff --git a/vars_test.go b/vars_test.go index f9c182b..3366584 100644 --- a/vars_test.go +++ b/vars_test.go @@ -592,6 +592,13 @@ func TestCircularRef(t *testing.T) { t.Run(tName, func(t *testing.T) { // t.Parallel() // This cannot be ran in Parallel since it mutates a global var. + { // Set the new config and schedule to return old config. + onceCfg = &sync.Once{} + t.Cleanup(func() { + cfg = oldCfg + }) + } + var res string switch name { default: @@ -612,11 +619,18 @@ func TestCircularRef(t *testing.T) { tName := fmt.Sprintf("TestCircularRef-with-NO-cirlce-%s", name) x := &Client{Public: "PublicName"} - x.srv.cli = &Client{Public: "NewPubNamw"} // no circle + x.srv.cli = &Client{Public: "NewPubName"} // no circle t.Run(tName, func(t *testing.T) { // t.Parallel() // This cannot be ran in Parallel since it mutates a global var. + { // Set the new config and schedule to return old config. + onceCfg = &sync.Once{} + t.Cleanup(func() { + cfg = oldCfg + }) + } + var res string switch name { default: From da6b53f6d87b239efb4a74aed1141bf3a70a31cb Mon Sep 17 00:00:00 2001 From: komuw Date: Thu, 22 Feb 2024 15:19:29 +0300 Subject: [PATCH 10/10] m --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96e1033..f4c14d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Most recent version is listed first. + +# v0.0.17 +- Add ability to dump items with circular references: https://github.com/komuw/kama/pull/66 + # v0.0.16 - Update Go version: https://github.com/komuw/kama/pull/62 - Add ability to dump private struct fields via config option: https://github.com/komuw/kama/pull/64