refactor: refactor yaml unmarshaler (#1517)

master tools/goctl/v1.3.0
Kevin Wan 3 years ago committed by GitHub
parent 05cc62f5ff
commit a40254156f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,8 +3,9 @@ package mapping
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"gopkg.in/yaml.v2"
"io" "io"
"gopkg.in/yaml.v2"
) )
// To make .json & .yaml consistent, we just use json as the tag key. // 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) 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{} { func cleanupInterfaceMap(in map[interface{}]interface{}) map[string]interface{} {
res := make(map[string]interface{}) res := make(map[string]interface{})
for k, v := range in { for k, v := range in {
@ -100,3 +62,40 @@ func cleanupMapValue(v interface{}) interface{} {
return Repr(v) 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
}

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

@ -8,9 +8,9 @@ import (
"text/template" "text/template"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const apiTemplate = ` const apiTemplate = `

@ -3,8 +3,8 @@ package apigen
import ( import (
"fmt" "fmt"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
) )
const ( const (

@ -4,9 +4,9 @@ import (
"errors" "errors"
"strings" "strings"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/urfave/cli"
) )
// DartCommand create dart network request code // DartCommand create dart network request code

@ -7,9 +7,9 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
// DocCommand generate markdown doc file // DocCommand generate markdown doc file

@ -11,11 +11,11 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/errorx" "github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser" "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/api/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const ( const (

@ -12,6 +12,7 @@ import (
"time" "time"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
apiformat "github.com/zeromicro/go-zero/tools/goctl/api/format" apiformat "github.com/zeromicro/go-zero/tools/goctl/api/format"
"github.com/zeromicro/go-zero/tools/goctl/api/parser" "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/config"
"github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const tmpFile = "%s-%d" const tmpFile = "%s-%d"

@ -3,8 +3,8 @@ package gogen
import ( import (
"fmt" "fmt"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
) )
const ( const (

@ -6,10 +6,10 @@ import (
"strings" "strings"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
// JavaCommand the generate java code command entrance // JavaCommand the generate java code command entrance

@ -3,8 +3,8 @@ package ktgen
import ( import (
"errors" "errors"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
) )
// KtCommand the generate kotlin code command entrance // KtCommand the generate kotlin code command entrance

@ -7,11 +7,11 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/api/gogen" "github.com/zeromicro/go-zero/tools/goctl/api/gogen"
conf "github.com/zeromicro/go-zero/tools/goctl/config" 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"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const apiTemplate = ` const apiTemplate = `

@ -3,8 +3,8 @@ package new
import ( import (
"fmt" "fmt"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
) )
const ( const (

@ -7,9 +7,9 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/zeromicro/antlr"
"github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api" "github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api"
"github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/console"
"github.com/zeromicro/antlr"
) )
type ( type (

@ -5,9 +5,9 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/zeromicro/antlr"
"github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api" "github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api"
"github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/console"
"github.com/zeromicro/antlr"
) )
type ( type (

@ -5,10 +5,10 @@ import (
"fmt" "fmt"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser" "github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
// TsCommand provides the entry to generate typescript codes // TsCommand provides the entry to generate typescript codes

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
) )
// GoValidateApi verifies whether the api has a syntax error // GoValidateApi verifies whether the api has a syntax error

@ -6,8 +6,8 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
) )
const ( const (

@ -10,9 +10,9 @@ import (
"time" "time"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const ( const (

@ -1,8 +1,8 @@
package docker package docker
import ( import (
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
) )
const ( const (

@ -6,9 +6,9 @@ import (
"text/template" "text/template"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const ( const (

@ -3,9 +3,9 @@ package generate
import ( import (
"fmt" "fmt"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/model/mongo/template" "github.com/zeromicro/go-zero/tools/goctl/model/mongo/template"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const ( const (

@ -5,11 +5,11 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/config" "github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/model/mongo/generate" "github.com/zeromicro/go-zero/tools/goctl/model/mongo/generate"
file "github.com/zeromicro/go-zero/tools/goctl/util" file "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
// Action provides the entry for goctl mongo code generation. // Action provides the entry for goctl mongo code generation.

@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/postgres" "github.com/zeromicro/go-zero/core/stores/postgres"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
@ -16,7 +17,6 @@ import (
file "github.com/zeromicro/go-zero/tools/goctl/util" 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/console"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const ( const (

@ -3,9 +3,9 @@ package gen
import ( import (
"fmt" "fmt"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template" "github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const ( const (

@ -6,6 +6,7 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/zeromicro/ddl-parser/parser"
"github.com/zeromicro/go-zero/core/collection" "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/converter"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/model" "github.com/zeromicro/go-zero/tools/goctl/model/sql/model"
@ -13,7 +14,6 @@ import (
su "github.com/zeromicro/go-zero/tools/goctl/util" 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/console"
"github.com/zeromicro/go-zero/tools/goctl/util/stringx" "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
"github.com/zeromicro/ddl-parser/parser"
) )
const timeImport = "time.Time" const timeImport = "time.Time"

@ -13,11 +13,11 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/api/parser" "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/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx" "github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const pluginArg = "_plugin" const pluginArg = "_plugin"

@ -6,12 +6,12 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/rpc/generator" "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"
"github.com/zeromicro/go-zero/tools/goctl/util/console" "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/env"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
// Deprecated: use ZRPC instead. // Deprecated: use ZRPC instead.

@ -3,8 +3,8 @@ package generator
import ( import (
"fmt" "fmt"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
) )
const ( const (

@ -5,6 +5,7 @@ import (
"path/filepath" "path/filepath"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/core/errorx" "github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/tools/goctl/api/apigen" "github.com/zeromicro/go-zero/tools/goctl/api/apigen"
"github.com/zeromicro/go-zero/tools/goctl/api/gogen" "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" modelgen "github.com/zeromicro/go-zero/tools/goctl/model/sql/gen"
rpcgen "github.com/zeromicro/go-zero/tools/goctl/rpc/generator" rpcgen "github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
) )
const templateParentPath = "/" const templateParentPath = "/"

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
) )
// Upgrade gets the latest goctl by // Upgrade gets the latest goctl by

Loading…
Cancel
Save