From fffbd5c4cf9598156e410947c991346a5cf7a1b1 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 4 Jun 2023 22:36:24 +0800 Subject: [PATCH] style: update and fix some code style error --- Makefile | 2 +- cflag/ext.go | 4 ++-- fsutil/finder/matchers.go | 5 +---- internal/gendoc/main.go | 1 + reflects/check_test.go | 8 ++++++-- stdutil/check_test.go | 6 +++--- structs/init_test.go | 7 +++++++ structs/tags_test.go | 6 +++--- strutil/check.go | 12 +++--------- 9 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index f902a5f18..19b591e34 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ readme: readme-c: ## Generate or update README file and commit change to git readme-c: readme git add README.* internal - git commit -m "doc: update and re-generate README docs" + git commit -m ":memo: doc: update and re-generate README docs" csfix: ## Fix code style for all files by go fmt csfix: diff --git a/cflag/ext.go b/cflag/ext.go index 8ccd6e431..bcba84b22 100644 --- a/cflag/ext.go +++ b/cflag/ext.go @@ -27,7 +27,7 @@ type RepeatableFlag interface { // IntValue int, allow limit min and max value TODO type IntValue struct { - val string + val string //lint:ignore U1000 is TODO // validate Min, Max int } @@ -36,7 +36,7 @@ type IntValue struct { // // Implemented the flag.Value interface type IntsString struct { - val string // input + val string //nolint:unused ints []int // value and size validate ValueFn func(val int) error diff --git a/fsutil/finder/matchers.go b/fsutil/finder/matchers.go index f14e53b12..4d408a96c 100644 --- a/fsutil/finder/matchers.go +++ b/fsutil/finder/matchers.go @@ -180,10 +180,7 @@ func RegexMatch(pattern string) MatcherFunc { reg := regexp.MustCompile(pattern) return func(el Elem) bool { - if reg.MatchString(el.Name()) { - return true - } - return false + return reg.MatchString(el.Name()) } } diff --git a/internal/gendoc/main.go b/internal/gendoc/main.go index 01438961a..d18d832a5 100644 --- a/internal/gendoc/main.go +++ b/internal/gendoc/main.go @@ -57,6 +57,7 @@ type genOptsSt struct { tplDir string } +//lint:ignore U1000 for test func (o genOptsSt) filePattern() string { baseDir := genOpts.baseDir if baseDir == "/" || baseDir == "./" { diff --git a/reflects/check_test.go b/reflects/check_test.go index 85a9dca40..7b166d500 100644 --- a/reflects/check_test.go +++ b/reflects/check_test.go @@ -47,7 +47,9 @@ func TestIsEmpty(t *testing.T) { is.True(reflects.IsEmpty(reflect.ValueOf(uint(0)))) is.True(reflects.IsEmpty(reflect.ValueOf(float32(0)))) - type T struct{ v any } + type T struct { + v any //lint:ignore U1000 for test + } rv := reflect.ValueOf(T{}).Field(0) is.True(reflects.IsEmpty(rv)) } @@ -63,7 +65,9 @@ func TestIsEmptyValue(t *testing.T) { is.True(reflects.IsEmptyValue(reflect.ValueOf(uint(0)))) is.True(reflects.IsEmptyValue(reflect.ValueOf(float32(0)))) - type T struct{ v any } + type T struct { + v any //lint:ignore U1000 for test + } rv := reflect.ValueOf(T{}).Field(0) is.True(reflects.IsEmptyValue(rv)) } diff --git a/stdutil/check_test.go b/stdutil/check_test.go index 767860087..4537bedc1 100644 --- a/stdutil/check_test.go +++ b/stdutil/check_test.go @@ -10,7 +10,6 @@ import ( func TestIsNil(t *testing.T) { is := assert.New(t) - is.True(stdutil.IsNil(nil)) } @@ -23,7 +22,6 @@ func TestIsEmpty(t *testing.T) { func TestIsFunc(t *testing.T) { is := assert.New(t) - is.False(stdutil.IsFunc(nil)) } @@ -57,7 +55,9 @@ func TestValueIsEmpty(t *testing.T) { is.True(stdutil.ValueIsEmpty(reflect.ValueOf(nil))) is.True(stdutil.ValueIsEmpty(reflect.ValueOf(""))) - type T struct{ v any } + type T struct { + v any //lint:ignore U1000 for test + } rv := reflect.ValueOf(T{}).Field(0) is.True(stdutil.ValueIsEmpty(rv)) } diff --git a/structs/init_test.go b/structs/init_test.go index b86e59ca4..29608d616 100644 --- a/structs/init_test.go +++ b/structs/init_test.go @@ -22,6 +22,7 @@ func TestInitDefaults(t *testing.T) { assert.NoErr(t, err) assert.Eq(t, "inhere", u.Name) assert.Eq(t, 0, u.Age) + assert.Eq(t, "", u.city) // dump.P(u) type User1 struct { @@ -116,6 +117,12 @@ func TestInitDefaults_nestStruct(t *testing.T) { assert.Eq(t, 30, u.Age) assert.Eq(t, "chengdu", u.Extra.City) assert.Eq(t, "https://github.com/inhere", u.Extra.Github) + + u = &User{Extra: ExtraDefault{Github: "some url"}} + err = structs.InitDefaults(u) + dump.P(u) + assert.NoErr(t, err) + assert.Eq(t, "chengdu", u.Extra.City) } func TestInitDefaults_ptrStructField(t *testing.T) { diff --git a/structs/tags_test.go b/structs/tags_test.go index 263ec4b40..30880d45c 100644 --- a/structs/tags_test.go +++ b/structs/tags_test.go @@ -15,7 +15,7 @@ func ExampleTagParser_Parse() { type User struct { Age int `json:"age" yaml:"age" default:"23"` Name string `json:"name,omitempty" yaml:"name" default:"inhere"` - inner string + inner string //lint:ignore U1000 for test } u := &User{} @@ -99,7 +99,7 @@ func TestParseTags(t *testing.T) { type user struct { Age int `json:"age" default:"23"` Name string `json:"name" default:"inhere"` - inner string + inner string //lint:ignore U1000 unused } tags, err := structs.ParseTags(user{}, []string{"json", "default"}) @@ -123,7 +123,7 @@ func TestParseReflectTags(t *testing.T) { type user struct { Age int `json:"age" default:"23"` Name string `json:"name" default:"inhere"` - inner string + inner string //lint:ignore U1000 unused } rt := reflect.TypeOf(user{}) diff --git a/strutil/check.go b/strutil/check.go index a6bfa7260..8654d8412 100644 --- a/strutil/check.go +++ b/strutil/check.go @@ -260,12 +260,8 @@ func PathMatch(pattern, s string) bool { // Difference with PathMatch() is: `*` can match any char, contain `/`. func GlobMatch(pattern, s string) bool { // replace `/` to `S` for path.Match - if strings.Contains(pattern, "/") { - pattern = strings.Replace(pattern, "/", "S", -1) - } - if strings.Contains(s, "/") { - s = strings.Replace(s, "/", "S", -1) - } + pattern = strings.Replace(pattern, "/", "S", -1) + s = strings.Replace(s, "/", "S", -1) ok, err := path.Match(pattern, s) if err != nil { @@ -322,9 +318,7 @@ func MatchNodePath(pattern, s string, sep string) bool { } pattern = strings.Replace(pattern, sep, "/", -1) - if strings.Contains(s, sep) { - s = strings.Replace(s, sep, "/", -1) - } + s = strings.Replace(s, sep, "/", -1) ok, err := path.Match(pattern, s) if err != nil {