Skip to content

Commit

Permalink
refactor: refactor yaml unmarshaler (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Feb 9, 2022
1 parent 05cc62f commit a402541
Show file tree
Hide file tree
Showing 31 changed files with 68 additions and 70 deletions.
79 changes: 39 additions & 40 deletions core/mapping/yamlunmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
1 change: 0 additions & 1 deletion core/stores/redis/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

red "github.com/go-redis/redis/v8"

"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/apigen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/apigen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/dartgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/docgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ 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"
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
"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"
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/javagen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/ktgen/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/new/newservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/new/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/parser/g4/ast/apiparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/parser/g4/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/tsgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/bug/bug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/docker/template.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/mongo/generate/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/sql/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ 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"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/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/stringx"
"github.com/zeromicro/ddl-parser/parser"
)

const timeImport = "time.Time"
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit a402541

Please sign in to comment.