From 0b0406f41a9ae945ee18d307f047f5d84706bdea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=82=E6=B0=91233?= Date: Wed, 11 Aug 2021 17:57:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3golint=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E8=AD=A6=E5=91=8A=20(#897)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 解决goreportcard的警报 ps: warning: if block ends with a return statement, so drop this else and outdent its block (golint) * feat: 优化golint警告,将processFieldPrimitiveWithJsonNumber 改成 processFieldPrimitiveWithJSONNumber unmarshaler.go:248:23: method processFieldPrimitiveWithJsonNumber should be processFieldPrimitiveWithJSONNumber * feat: 添加 WithCanonicalKeyFunc 注释 * feat: 添加DisableStat的注释 * feat: 添加 RegisterGoctlHome 注释 * feat: 添加 PostgreSqlJoin 注释 * feat: 解决goline警告should not use basic type string as key in context.WithValue问题 * feat: 解决golint警告信息: should not use basic type string as key in context.WithValue * fix: 定义自定义字段类型,导致go test fail 问题 * update: 恢复原有测试用例 * fix golint warning --- core/contextx/unmarshaler_test.go | 5 ++++- core/contextx/valueonlycontext_test.go | 4 +++- core/logx/logs.go | 1 + core/mapping/unmarshaler.go | 6 ++++-- tools/goctl/model/sql/builderx/builder.go | 1 + tools/goctl/util/file.go | 1 + 6 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core/contextx/unmarshaler_test.go b/core/contextx/unmarshaler_test.go index df390236..42f9db73 100644 --- a/core/contextx/unmarshaler_test.go +++ b/core/contextx/unmarshaler_test.go @@ -8,6 +8,7 @@ import ( ) func TestUnmarshalContext(t *testing.T) { + type Person struct { Name string `ctx:"name"` Age int `ctx:"age"` @@ -47,9 +48,11 @@ func TestUnmarshalContextWithMissing(t *testing.T) { Name string `ctx:"name"` Age int `ctx:"age"` } + type name string + const PersonNameKey name = "name" ctx := context.Background() - ctx = context.WithValue(ctx, "name", "kevin") + ctx = context.WithValue(ctx, PersonNameKey, "kevin") var person Person err := For(ctx, &person) diff --git a/core/contextx/valueonlycontext_test.go b/core/contextx/valueonlycontext_test.go index d3f1ddad..b749720a 100644 --- a/core/contextx/valueonlycontext_test.go +++ b/core/contextx/valueonlycontext_test.go @@ -9,7 +9,9 @@ import ( ) func TestContextCancel(t *testing.T) { - c := context.WithValue(context.Background(), "key", "value") + type key string + var nameKey key = "name" + c := context.WithValue(context.Background(), nameKey, "value") c1, cancel := context.WithCancel(c) o := ValueOnlyFrom(c1) c2, cancel2 := context.WithCancel(o) diff --git a/core/logx/logs.go b/core/logx/logs.go index 03025625..0e8ba160 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -197,6 +197,7 @@ func Disable() { }) } +// DisableStat to disable stat logs func DisableStat() { atomic.StoreUint32(&disableStat, 1) } diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index 35a61de1..abf2df99 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -230,7 +230,7 @@ func (u *Unmarshaler) processFieldPrimitive(field reflect.StructField, value ref default: switch v := mapValue.(type) { case json.Number: - return u.processFieldPrimitiveWithJsonNumber(field, value, v, opts, fullName) + return u.processFieldPrimitiveWithJSONNumber(field, value, v, opts, fullName) default: if typeKind == valueKind { if err := validateValueInOptions(opts.options(), mapValue); err != nil { @@ -245,7 +245,7 @@ func (u *Unmarshaler) processFieldPrimitive(field reflect.StructField, value ref return newTypeMismatchError(fullName) } -func (u *Unmarshaler) processFieldPrimitiveWithJsonNumber(field reflect.StructField, value reflect.Value, +func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(field reflect.StructField, value reflect.Value, v json.Number, opts *fieldOptionsWithContext, fullName string) error { fieldType := field.Type fieldKind := fieldType.Kind() @@ -524,6 +524,7 @@ func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int, baseKind re return errTypeMismatch } + ithVal.Set(reflect.ValueOf(value)) return nil } @@ -626,6 +627,7 @@ func WithStringValues() UnmarshalOption { } } +// WithCanonicalKeyFunc customizes a Unmarshaler with Canonical Key func func WithCanonicalKeyFunc(f func(string) string) UnmarshalOption { return func(opt *unmarshalOptions) { opt.canonicalKey = f diff --git a/tools/goctl/model/sql/builderx/builder.go b/tools/goctl/model/sql/builderx/builder.go index 3c609194..0106f021 100644 --- a/tools/goctl/model/sql/builderx/builder.go +++ b/tools/goctl/model/sql/builderx/builder.go @@ -121,6 +121,7 @@ func RawFieldNames(in interface{}, postgresSql ...bool) []string { return out } +//PostgreSqlJoin concatenate the given elements into a string func PostgreSqlJoin(elems []string) string { b := new(strings.Builder) for index, e := range elems { diff --git a/tools/goctl/util/file.go b/tools/goctl/util/file.go index d8320e15..b8ef6bf2 100644 --- a/tools/goctl/util/file.go +++ b/tools/goctl/util/file.go @@ -19,6 +19,7 @@ const ( var goctlHome string +// RegisterGoctlHome register goctl home path func RegisterGoctlHome(home string) { goctlHome = home }