Преглед на файлове

refactor: refactor yaml unmarshaler (#1517)

Kevin Wan преди 3 години
родител
ревизия
a40254156f

+ 39 - 40
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
+}

+ 0 - 1
core/stores/redis/hook_test.go

@@ -8,7 +8,6 @@ import (
 	"time"
 
 	red "github.com/go-redis/redis/v8"
-
 	"github.com/stretchr/testify/assert"
 )
 

+ 1 - 1
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 = `

+ 1 - 1
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 (

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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 (

+ 1 - 1
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"

+ 1 - 1
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 (

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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 = `

+ 1 - 1
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 (

+ 1 - 1
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 (

+ 1 - 1
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 (

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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 (

+ 1 - 1
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 (

+ 1 - 1
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 (

+ 1 - 1
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 (

+ 1 - 1
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 (

+ 1 - 1
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.

+ 1 - 1
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 (

+ 1 - 1
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 (

+ 1 - 1
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"

+ 1 - 1
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"

+ 1 - 1
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.

+ 1 - 1
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 (

+ 1 - 1
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 = "/"

+ 1 - 1
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