From 4143c5b303c1488310f26b988ae9d173480b3afc Mon Sep 17 00:00:00 2001 From: piaoxue1949 <891837834@qq.com> Date: Thu, 11 Jul 2024 10:03:36 +0800 Subject: [PATCH 1/9] fix: dumper.printRValue panic when unexported fields in the structure are of type time.Time; (#183) Co-authored-by: yujin <91837834@qq.com> --- dump/dumper.go | 7 ++++++- dump/dumper_test.go | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/dump/dumper.go b/dump/dumper.go index bb2d69e02..0861d8cbd 100644 --- a/dump/dumper.go +++ b/dump/dumper.go @@ -279,7 +279,12 @@ func (d *Dumper) printRValue(t reflect.Type, v reflect.Value) { // up: special handel time.Time struct if t == timeType { - timeStr := v.Interface().(time.Time).Format(time.RFC3339) + var timeStr string + if v.CanInterface() { + timeStr = v.Interface().(time.Time).Format(time.RFC3339) + } else { + timeStr = v.String() + } d.printf("time.Time(%s),\n", d.ColorTheme.string(timeStr)) break } diff --git a/dump/dumper_test.go b/dump/dumper_test.go index 525c43398..55778701b 100644 --- a/dump/dumper_test.go +++ b/dump/dumper_test.go @@ -7,6 +7,7 @@ import ( "os" "reflect" "testing" + "time" "unsafe" "github.com/gookit/color" @@ -425,17 +426,19 @@ func TestStruct_WithNested(_ *testing.T) { func TestDumper_Dump_userType(_ *testing.T) { type testSt struct { - name string - mod fs.FileMode - Mod2 fs.FileMode - Age int + name string + mod fs.FileMode + Mod2 fs.FileMode + Age int + createdAt time.Time } st := testSt{ - name: "inhere", - mod: 0777, - Mod2: 0775, - Age: 23, + name: "inhere", + mod: 0777, + Mod2: 0775, + Age: 23, + createdAt: time.Now(), } fmt.Println("------ use dumper ------") From ebc746c0cd47d4ad30801cb488ea570c5002c058 Mon Sep 17 00:00:00 2001 From: inhere Date: Fri, 27 Sep 2024 20:20:23 +0800 Subject: [PATCH 2/9] :bug: fix: structs - fix TryToMap panic on handle invalid field value - reflect: call of reflect.Value.CanInterface on zero Value. close #192 --- structs/convert.go | 4 ++++ structs/convert_test.go | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/structs/convert.go b/structs/convert.go index 8a379fe02..3158a922a 100644 --- a/structs/convert.go +++ b/structs/convert.go @@ -146,6 +146,10 @@ func structToMap(obj reflect.Value, opt *MapOptions, mp map[string]any) (map[str } field := reflect.Indirect(obj.Field(i)) + if !field.IsValid() { + continue + } + if field.Kind() == reflect.Struct { // collect anonymous struct values to parent. if ft.Anonymous && opt.MergeAnonymous { diff --git a/structs/convert_test.go b/structs/convert_test.go index fa6e8dc51..fe96cb205 100644 --- a/structs/convert_test.go +++ b/structs/convert_test.go @@ -217,3 +217,47 @@ func TestTryToMap_customTag(t *testing.T) { assert.ContainsKeys(t, mp, []string{"name", "age", "full_name"}) } + +// https://github.com/gookit/goutil/issues/192 +func TestIssue192(t *testing.T) { + // go code + type Structure struct { + // I don't want to expose too many infos, but here it goes only a set of float64 numbers, without any complex type or pointers + } + + type MyStruct struct { + ID string `json:"id" gorm:"column:id"` + RefId int `json:"ref_id" gorm:"column:ref_id"` + PhotoURL string `json:"photoURL" gorm:"column:photo_url"` + Year int `json:"year" gorm:"column:year"` + SomeDate string `json:"someDate" gorm:"column:some_date"` + Trim string `json:"trim" gorm:"column:trim"` + Type string `json:"type" gorm:"column:type"` + BodyType string `json:"bodyType" gorm:"column:body_type"` + Counter int `json:"counter" gorm:"column:counter"` + Used bool `json:"used" gorm:"column:used"` + Price float64 `json:"price" gorm:"column:price"` + Value float64 `json:"value" gorm:"column:value"` + Amount float64 `json:"amount" gorm:"-:all"` + MaxAmount float64 `json:"maxAmount" gorm:"column:max_amount"` + StockNo string `json:"stockNo" gorm:"column:stock_no"` + ExpiresAt *string `json:"expiresAt" gorm:"column:expires_at"` + UpdatedBy *string `json:"updatedBy" gorm:"column:updated_by"` + Location string `json:"location" gorm:"column:location"` + IsFavorite bool `json:"is_favorite,omitempty" gorm:"-"` + Structure *Structure `json:"structure,omitempty" gorm:"-:all"` + } + + data := MyStruct{ + ID: "ABC123", + RefId: 1, + Year: 1990, + SomeDate: "2020-01-03 15:02:15", + Counter: 9001, + Price: 20, + } + + toMap, err := structs.TryToMap(data) // <- it panics! + assert.NoErr(t, err) + dump.P(toMap) +} From c341f6855845322e5b8c477e5775f8d849fe61c3 Mon Sep 17 00:00:00 2001 From: inhere Date: Fri, 27 Sep 2024 20:27:30 +0800 Subject: [PATCH 3/9] :bug: fix: structs - InitDefaults: can't set slice to array. close #172 --- structs/init.go | 14 ++++++++++---- structs/init_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/structs/init.go b/structs/init.go index 0aa6ca04c..9c42b159c 100644 --- a/structs/init.go +++ b/structs/init.go @@ -20,7 +20,7 @@ type InitOptFunc func(opt *InitOptions) // InitOptions struct type InitOptions struct { // TagName default value tag name. tag: default - TagName string + TagName string EnvPrefixTagName string // ParseEnv var name on default value. eg: `default:"${APP_ENV}"` // @@ -181,7 +181,7 @@ func initDefaults(rv reflect.Value, opt *InitOptions, envPrefix string) error { func enhanceDefaultVar(val string, envPrefix string) string { cleaned_var := strings.TrimSpace(val) if strings.HasPrefix(cleaned_var, "${") && strings.HasSuffix(cleaned_var, "}") { - parts := strings.SplitN(cleaned_var[2 : len(cleaned_var) - 1], "|", 2) + parts := strings.SplitN(cleaned_var[2:len(cleaned_var)-1], "|", 2) if len(parts) > 0 { env := strings.TrimSpace(parts[0]) if env != "" { @@ -189,7 +189,7 @@ func enhanceDefaultVar(val string, envPrefix string) string { return fmt.Sprintf("${%s%s}", envPrefix, env) } else { return fmt.Sprintf("${%s%s|%s}", envPrefix, env, parts[1]) - } + } } } } @@ -214,7 +214,13 @@ func initDefaultValue(fv reflect.Value, val string, parseEnv bool, envPrefix str ss := strutil.SplitTrimmed(val, ",") valRv, err := reflects.ConvSlice(reflect.ValueOf(ss), fv.Type().Elem()) if err == nil { - reflects.SetRValue(fv, valRv) + if fv.Kind() == reflect.Array { + for i := 0; i < valRv.Len(); i++ { + fv.Index(i).Set(valRv.Index(i)) + } + } else { + reflects.SetRValue(fv, valRv) + } } return err } diff --git a/structs/init_test.go b/structs/init_test.go index df91a6466..9b8730e04 100644 --- a/structs/init_test.go +++ b/structs/init_test.go @@ -261,3 +261,17 @@ func TestInitDefaults_ptrField(t *testing.T) { assert.Eq(t, 30, *u.Age) assert.Eq(t, "sh", u.City) } + +// https://github.com/gookit/goutil/issues/172 +// panic: reflect.Set: value of type []int is not assignable to type [3]int +func TestIssues172(t *testing.T) { + type Config struct { + Ints [3]int `default:"1,2,3"` + } + + var c Config + + err := structs.InitDefaults(&c) + assert.NoErr(t, err) + assert.Eq(t, [3]int{1, 2, 3}, c.Ints) +} From 3c4153fa70df235d27a19195a068e166db2724f6 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 20:29:40 +0800 Subject: [PATCH 4/9] build(deps): bump morphy2k/revive-action from 2.5.7 to 2.5.10 (#191) --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 684912ea0..50493a296 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -32,7 +32,7 @@ jobs: go-version: ${{ matrix.go_version }} - name: Revive check - uses: morphy2k/revive-action@v2.5.7 + uses: morphy2k/revive-action@v2.5.10 if: ${{ matrix.os == 'ubuntu-latest' && matrix.go_version == '1.20' }} with: # Exclude patterns, separated by semicolons (optional) From 5c4823d41bf819ac425bdd9e62defec37983eb14 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 20:29:58 +0800 Subject: [PATCH 5/9] build(deps): bump golang.org/x/term from 0.22.0 to 0.23.0 (#188) --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 04feb6b40..3a24cda45 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.19 require ( github.com/gookit/color v1.5.4 golang.org/x/sync v0.7.0 - golang.org/x/sys v0.22.0 - golang.org/x/term v0.22.0 + golang.org/x/sys v0.23.0 + golang.org/x/term v0.23.0 golang.org/x/text v0.16.0 ) diff --git a/go.sum b/go.sum index c79f88641..23790a3e2 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,10 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJu golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= -golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= +golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= From d81967d1265b1160c143ff8c59d127c58e182578 Mon Sep 17 00:00:00 2001 From: inhere Date: Fri, 27 Sep 2024 20:38:25 +0800 Subject: [PATCH 6/9] ci: remote the morphy2k/revive-action --- .github/workflows/go.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 50493a296..3cac46184 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -19,7 +19,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - go_version: [1.19, '1.20', 1.21, 1.22] + go_version: [1.19, '1.20', 1.21, 1.22, 1.23] steps: - name: Check out code @@ -31,16 +31,16 @@ jobs: with: go-version: ${{ matrix.go_version }} - - name: Revive check - uses: morphy2k/revive-action@v2.5.10 - if: ${{ matrix.os == 'ubuntu-latest' && matrix.go_version == '1.20' }} - with: - # Exclude patterns, separated by semicolons (optional) - exclude: "./internal/..." +# - name: Revive check +# uses: morphy2k/revive-action@v2.5.10 +# if: ${{ matrix.os == 'ubuntu-latest' && matrix.go_version == '1.22' }} +# with: +# # Exclude patterns, separated by semicolons (optional) +# exclude: "./internal/..." - name: Run staticcheck uses: reviewdog/action-staticcheck@v1 - if: ${{ matrix.os == 'ubuntu-latest' && matrix.go_version == '1.20' }} + if: ${{ matrix.os == 'ubuntu-latest' && matrix.go_version == '1.22' }} with: github_token: ${{ secrets.github_token }} # Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review]. From 717b4f5b8931853213dbd54986840440e6b479e4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 20:53:34 +0800 Subject: [PATCH 7/9] build(deps): bump golang.org/x/sys from 0.23.0 to 0.25.0 (#193) --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 3a24cda45..f0e8a0b13 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/gookit/color v1.5.4 golang.org/x/sync v0.7.0 - golang.org/x/sys v0.23.0 + golang.org/x/sys v0.25.0 golang.org/x/term v0.23.0 golang.org/x/text v0.16.0 ) diff --git a/go.sum b/go.sum index 23790a3e2..e5f136089 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJu golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= From 0a4c580103f70fc53d07aa923cf9dc75a71fa0a5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 21:02:27 +0800 Subject: [PATCH 8/9] build(deps): bump golang.org/x/text from 0.16.0 to 0.18.0 (#194) --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index f0e8a0b13..2057c0b55 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,10 @@ go 1.19 require ( github.com/gookit/color v1.5.4 - golang.org/x/sync v0.7.0 + golang.org/x/sync v0.8.0 golang.org/x/sys v0.25.0 golang.org/x/term v0.23.0 - golang.org/x/text v0.16.0 + golang.org/x/text v0.18.0 ) require github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect diff --git a/go.sum b/go.sum index e5f136089..1bffcbc04 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,12 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= From b511ebd822fc0a839a992736376ceb0619691dde Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 28 Sep 2024 11:56:29 +0800 Subject: [PATCH 9/9] build(deps): bump golang.org/x/term from 0.23.0 to 0.24.0 (#195) --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 2057c0b55..0a73e5dc6 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/gookit/color v1.5.4 golang.org/x/sync v0.8.0 golang.org/x/sys v0.25.0 - golang.org/x/term v0.23.0 + golang.org/x/term v0.24.0 golang.org/x/text v0.18.0 ) diff --git a/go.sum b/go.sum index 1bffcbc04..f701daf14 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= -golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= +golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM= +golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8= golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=