From a40254156f72d7672ba7e18a062eb9c66a32cd8e Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Wed, 9 Feb 2022 17:22:52 +0800 Subject: [PATCH] refactor: refactor yaml unmarshaler (#1517) --- core/mapping/yamlunmarshaler.go | 79 ++++++++++---------- core/stores/redis/hook_test.go | 1 - tools/goctl/api/apigen/gen.go | 2 +- tools/goctl/api/apigen/template.go | 2 +- tools/goctl/api/dartgen/gen.go | 2 +- tools/goctl/api/docgen/gen.go | 2 +- tools/goctl/api/format/format.go | 2 +- tools/goctl/api/gogen/gen.go | 2 +- tools/goctl/api/gogen/template.go | 2 +- tools/goctl/api/javagen/gen.go | 2 +- tools/goctl/api/ktgen/cmd.go | 2 +- tools/goctl/api/new/newservice.go | 2 +- tools/goctl/api/new/template.go | 2 +- tools/goctl/api/parser/g4/ast/apiparser.go | 2 +- tools/goctl/api/parser/g4/ast/ast.go | 2 +- tools/goctl/api/tsgen/gen.go | 2 +- tools/goctl/api/validate/validate.go | 2 +- tools/goctl/bug/bug.go | 2 +- tools/goctl/docker/docker.go | 2 +- tools/goctl/docker/template.go | 2 +- tools/goctl/kube/kube.go | 2 +- tools/goctl/model/mongo/generate/template.go | 2 +- tools/goctl/model/mongo/mongo.go | 2 +- tools/goctl/model/sql/command/command.go | 2 +- tools/goctl/model/sql/gen/template.go | 2 +- tools/goctl/model/sql/parser/parser.go | 2 +- tools/goctl/plugin/plugin.go | 2 +- tools/goctl/rpc/cli/cli.go | 2 +- tools/goctl/rpc/generator/template.go | 2 +- tools/goctl/tpl/templates.go | 2 +- tools/goctl/upgrade/upgrade.go | 2 +- 31 files changed, 68 insertions(+), 70 deletions(-) diff --git a/core/mapping/yamlunmarshaler.go b/core/mapping/yamlunmarshaler.go index 11263821..01bf4469 100644 --- a/core/mapping/yamlunmarshaler.go +++ b/core/mapping/yamlunmarshaler.go @@ -3,8 +3,9 @@ package mapping import ( "encoding/json" "errors" - "gopkg.in/yaml.v2" "io" + + "gopkg.in/yaml.v2" ) // To make .json & .yaml consistent, we just use json as the tag key. @@ -27,45 +28,6 @@ func UnmarshalYamlReader(reader io.Reader, v interface{}) error { return unmarshalYamlReader(reader, v, yamlUnmarshaler) } -func unmarshalYamlBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error { - var o interface{} - if err := yamlUnmarshal(content, &o); err != nil { - return err - } - - if m, ok := o.(map[string]interface{}); ok { - return unmarshaler.Unmarshal(m, v) - } - - return ErrUnsupportedType -} - -func unmarshalYamlReader(reader io.Reader, v interface{}, unmarshaler *Unmarshaler) error { - var res interface{} - if err := yaml.NewDecoder(reader).Decode(&res); err != nil { - return err - } - - out := cleanupMapValue(res) - - if m, ok := out.(map[string]interface{}); ok { - return unmarshaler.Unmarshal(m, v) - } - - return ErrUnsupportedType -} - -// yamlUnmarshal YAML to map[string]interface{} instead of map[interface{}]interface{}. -func yamlUnmarshal(in []byte, out interface{}) error { - var res interface{} - if err := yaml.Unmarshal(in, &res); err != nil { - return err - } - - *out.(*interface{}) = cleanupMapValue(res) - return nil -} - func cleanupInterfaceMap(in map[interface{}]interface{}) map[string]interface{} { res := make(map[string]interface{}) for k, v := range in { @@ -100,3 +62,40 @@ func cleanupMapValue(v interface{}) interface{} { return Repr(v) } } + +func unmarshal(unmarshaler *Unmarshaler, o interface{}, v interface{}) error { + if m, ok := o.(map[string]interface{}); ok { + return unmarshaler.Unmarshal(m, v) + } + + return ErrUnsupportedType +} + +func unmarshalYamlBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error { + var o interface{} + if err := yamlUnmarshal(content, &o); err != nil { + return err + } + + return unmarshal(unmarshaler, o, v) +} + +func unmarshalYamlReader(reader io.Reader, v interface{}, unmarshaler *Unmarshaler) error { + var res interface{} + if err := yaml.NewDecoder(reader).Decode(&res); err != nil { + return err + } + + return unmarshal(unmarshaler, cleanupMapValue(res), v) +} + +// yamlUnmarshal YAML to map[string]interface{} instead of map[interface{}]interface{}. +func yamlUnmarshal(in []byte, out interface{}) error { + var res interface{} + if err := yaml.Unmarshal(in, &res); err != nil { + return err + } + + *out.(*interface{}) = cleanupMapValue(res) + return nil +} diff --git a/core/stores/redis/hook_test.go b/core/stores/redis/hook_test.go index 1d3dce5f..f1e993ab 100644 --- a/core/stores/redis/hook_test.go +++ b/core/stores/redis/hook_test.go @@ -8,7 +8,6 @@ import ( "time" red "github.com/go-redis/redis/v8" - "github.com/stretchr/testify/assert" ) diff --git a/tools/goctl/api/apigen/gen.go b/tools/goctl/api/apigen/gen.go index 7da3aa2f..32fc27d7 100644 --- a/tools/goctl/api/apigen/gen.go +++ b/tools/goctl/api/apigen/gen.go @@ -8,9 +8,9 @@ import ( "text/template" "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const apiTemplate = ` diff --git a/tools/goctl/api/apigen/template.go b/tools/goctl/api/apigen/template.go index 265ff80a..8237badd 100644 --- a/tools/goctl/api/apigen/template.go +++ b/tools/goctl/api/apigen/template.go @@ -3,8 +3,8 @@ package apigen import ( "fmt" - "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) const ( diff --git a/tools/goctl/api/dartgen/gen.go b/tools/goctl/api/dartgen/gen.go index 6edeb9da..3bb08a99 100644 --- a/tools/goctl/api/dartgen/gen.go +++ b/tools/goctl/api/dartgen/gen.go @@ -4,9 +4,9 @@ import ( "errors" "strings" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/tools/goctl/api/parser" - "github.com/urfave/cli" ) // DartCommand create dart network request code diff --git a/tools/goctl/api/docgen/gen.go b/tools/goctl/api/docgen/gen.go index 62cd37fa..f298576e 100644 --- a/tools/goctl/api/docgen/gen.go +++ b/tools/goctl/api/docgen/gen.go @@ -7,9 +7,9 @@ import ( "path/filepath" "strings" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) // DocCommand generate markdown doc file diff --git a/tools/goctl/api/format/format.go b/tools/goctl/api/format/format.go index 8f2fd9bc..79672952 100644 --- a/tools/goctl/api/format/format.go +++ b/tools/goctl/api/format/format.go @@ -11,11 +11,11 @@ import ( "path/filepath" "strings" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/core/errorx" "github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/api/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const ( diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 7e73f0b0..2d3a0f6a 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -12,6 +12,7 @@ import ( "time" "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/core/logx" apiformat "github.com/zeromicro/go-zero/tools/goctl/api/format" "github.com/zeromicro/go-zero/tools/goctl/api/parser" @@ -19,7 +20,6 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/config" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const tmpFile = "%s-%d" diff --git a/tools/goctl/api/gogen/template.go b/tools/goctl/api/gogen/template.go index 21addc6f..1089298f 100644 --- a/tools/goctl/api/gogen/template.go +++ b/tools/goctl/api/gogen/template.go @@ -3,8 +3,8 @@ package gogen import ( "fmt" - "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) const ( diff --git a/tools/goctl/api/javagen/gen.go b/tools/goctl/api/javagen/gen.go index 8698012b..e21ffb37 100644 --- a/tools/goctl/api/javagen/gen.go +++ b/tools/goctl/api/javagen/gen.go @@ -6,10 +6,10 @@ import ( "strings" "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) // JavaCommand the generate java code command entrance diff --git a/tools/goctl/api/ktgen/cmd.go b/tools/goctl/api/ktgen/cmd.go index 04bd214b..f0168cf4 100644 --- a/tools/goctl/api/ktgen/cmd.go +++ b/tools/goctl/api/ktgen/cmd.go @@ -3,8 +3,8 @@ package ktgen import ( "errors" - "github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/api/parser" ) // KtCommand the generate kotlin code command entrance diff --git a/tools/goctl/api/new/newservice.go b/tools/goctl/api/new/newservice.go index 0b161864..f978aeec 100644 --- a/tools/goctl/api/new/newservice.go +++ b/tools/goctl/api/new/newservice.go @@ -7,11 +7,11 @@ import ( "strings" "text/template" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/api/gogen" conf "github.com/zeromicro/go-zero/tools/goctl/config" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const apiTemplate = ` diff --git a/tools/goctl/api/new/template.go b/tools/goctl/api/new/template.go index 0e8f6dd1..eab11cb0 100644 --- a/tools/goctl/api/new/template.go +++ b/tools/goctl/api/new/template.go @@ -3,8 +3,8 @@ package new import ( "fmt" - "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) const ( diff --git a/tools/goctl/api/parser/g4/ast/apiparser.go b/tools/goctl/api/parser/g4/ast/apiparser.go index b77474f0..29d69da3 100644 --- a/tools/goctl/api/parser/g4/ast/apiparser.go +++ b/tools/goctl/api/parser/g4/ast/apiparser.go @@ -7,9 +7,9 @@ import ( "path/filepath" "strings" + "github.com/zeromicro/antlr" "github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api" "github.com/zeromicro/go-zero/tools/goctl/util/console" - "github.com/zeromicro/antlr" ) type ( diff --git a/tools/goctl/api/parser/g4/ast/ast.go b/tools/goctl/api/parser/g4/ast/ast.go index 22c825ac..6d31fbf0 100644 --- a/tools/goctl/api/parser/g4/ast/ast.go +++ b/tools/goctl/api/parser/g4/ast/ast.go @@ -5,9 +5,9 @@ import ( "sort" "strings" + "github.com/zeromicro/antlr" "github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api" "github.com/zeromicro/go-zero/tools/goctl/util/console" - "github.com/zeromicro/antlr" ) type ( diff --git a/tools/goctl/api/tsgen/gen.go b/tools/goctl/api/tsgen/gen.go index 6e007648..063f5f59 100644 --- a/tools/goctl/api/tsgen/gen.go +++ b/tools/goctl/api/tsgen/gen.go @@ -5,10 +5,10 @@ import ( "fmt" "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) // TsCommand provides the entry to generate typescript codes diff --git a/tools/goctl/api/validate/validate.go b/tools/goctl/api/validate/validate.go index 97b8d56e..ca430253 100644 --- a/tools/goctl/api/validate/validate.go +++ b/tools/goctl/api/validate/validate.go @@ -5,8 +5,8 @@ import ( "fmt" "github.com/logrusorgru/aurora" - "github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/api/parser" ) // GoValidateApi verifies whether the api has a syntax error diff --git a/tools/goctl/bug/bug.go b/tools/goctl/bug/bug.go index 33a6a874..a497e294 100644 --- a/tools/goctl/bug/bug.go +++ b/tools/goctl/bug/bug.go @@ -6,8 +6,8 @@ import ( "os/exec" "runtime" - "github.com/zeromicro/go-zero/tools/goctl/internal/version" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/internal/version" ) const ( diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 9b9c772d..e17165a7 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -10,9 +10,9 @@ import ( "time" "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const ( diff --git a/tools/goctl/docker/template.go b/tools/goctl/docker/template.go index ba0e94d4..929b8026 100644 --- a/tools/goctl/docker/template.go +++ b/tools/goctl/docker/template.go @@ -1,8 +1,8 @@ package docker import ( - "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) const ( diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 97bc7f4f..8befbb0a 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -6,9 +6,9 @@ import ( "text/template" "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const ( diff --git a/tools/goctl/model/mongo/generate/template.go b/tools/goctl/model/mongo/generate/template.go index 89c525ef..644250e8 100644 --- a/tools/goctl/model/mongo/generate/template.go +++ b/tools/goctl/model/mongo/generate/template.go @@ -3,9 +3,9 @@ package generate import ( "fmt" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/model/mongo/template" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const ( diff --git a/tools/goctl/model/mongo/mongo.go b/tools/goctl/model/mongo/mongo.go index 3a408e57..6d807476 100644 --- a/tools/goctl/model/mongo/mongo.go +++ b/tools/goctl/model/mongo/mongo.go @@ -5,11 +5,11 @@ import ( "path/filepath" "strings" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/config" "github.com/zeromicro/go-zero/tools/goctl/model/mongo/generate" file "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) // Action provides the entry for goctl mongo code generation. diff --git a/tools/goctl/model/sql/command/command.go b/tools/goctl/model/sql/command/command.go index 59753ee6..f313072e 100644 --- a/tools/goctl/model/sql/command/command.go +++ b/tools/goctl/model/sql/command/command.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/go-sql-driver/mysql" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/postgres" "github.com/zeromicro/go-zero/core/stores/sqlx" @@ -16,7 +17,6 @@ import ( file "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const ( diff --git a/tools/goctl/model/sql/gen/template.go b/tools/goctl/model/sql/gen/template.go index 6287e421..d9a664d4 100644 --- a/tools/goctl/model/sql/gen/template.go +++ b/tools/goctl/model/sql/gen/template.go @@ -3,9 +3,9 @@ package gen import ( "fmt" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/model/sql/template" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const ( diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go index 11c4c9a4..ab992083 100644 --- a/tools/goctl/model/sql/parser/parser.go +++ b/tools/goctl/model/sql/parser/parser.go @@ -6,6 +6,7 @@ import ( "sort" "strings" + "github.com/zeromicro/ddl-parser/parser" "github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/tools/goctl/model/sql/converter" "github.com/zeromicro/go-zero/tools/goctl/model/sql/model" @@ -13,7 +14,6 @@ import ( su "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/stringx" - "github.com/zeromicro/ddl-parser/parser" ) const timeImport = "time.Time" diff --git a/tools/goctl/plugin/plugin.go b/tools/goctl/plugin/plugin.go index c9cb97f0..50ea181e 100644 --- a/tools/goctl/plugin/plugin.go +++ b/tools/goctl/plugin/plugin.go @@ -13,11 +13,11 @@ import ( "path/filepath" "strings" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/api/spec" "github.com/zeromicro/go-zero/tools/goctl/rpc/execx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const pluginArg = "_plugin" diff --git a/tools/goctl/rpc/cli/cli.go b/tools/goctl/rpc/cli/cli.go index e02f6345..a139056d 100644 --- a/tools/goctl/rpc/cli/cli.go +++ b/tools/goctl/rpc/cli/cli.go @@ -6,12 +6,12 @@ import ( "path/filepath" "runtime" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/tools/goctl/rpc/generator" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/env" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) // Deprecated: use ZRPC instead. diff --git a/tools/goctl/rpc/generator/template.go b/tools/goctl/rpc/generator/template.go index 86497786..c703406b 100644 --- a/tools/goctl/rpc/generator/template.go +++ b/tools/goctl/rpc/generator/template.go @@ -3,8 +3,8 @@ package generator import ( "fmt" - "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) const ( diff --git a/tools/goctl/tpl/templates.go b/tools/goctl/tpl/templates.go index 3ffde0c5..e6127180 100644 --- a/tools/goctl/tpl/templates.go +++ b/tools/goctl/tpl/templates.go @@ -5,6 +5,7 @@ import ( "path/filepath" "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "github.com/zeromicro/go-zero/core/errorx" "github.com/zeromicro/go-zero/tools/goctl/api/apigen" "github.com/zeromicro/go-zero/tools/goctl/api/gogen" @@ -15,7 +16,6 @@ import ( modelgen "github.com/zeromicro/go-zero/tools/goctl/model/sql/gen" rpcgen "github.com/zeromicro/go-zero/tools/goctl/rpc/generator" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" - "github.com/urfave/cli" ) const templateParentPath = "/" diff --git a/tools/goctl/upgrade/upgrade.go b/tools/goctl/upgrade/upgrade.go index e2c91ce1..d8dac0d8 100644 --- a/tools/goctl/upgrade/upgrade.go +++ b/tools/goctl/upgrade/upgrade.go @@ -4,8 +4,8 @@ import ( "fmt" "runtime" - "github.com/zeromicro/go-zero/tools/goctl/rpc/execx" "github.com/urfave/cli" + "github.com/zeromicro/go-zero/tools/goctl/rpc/execx" ) // Upgrade gets the latest goctl by