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 (
"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
}

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

@ -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 = `

@ -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 (

@ -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

@ -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

@ -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 (

@ -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"

@ -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 (

@ -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

@ -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

@ -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 = `

@ -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 (

@ -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 (

@ -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 (

@ -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

@ -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

@ -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 (

@ -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,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 (

@ -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 (

@ -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 (

@ -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.

@ -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 (

@ -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 (

@ -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"

@ -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"

@ -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.

@ -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 (

@ -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 = "/"

@ -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

Loading…
Cancel
Save