Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package encoding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTomlToJson(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expect string | ||
}{ | ||
{ | ||
input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"\n", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"\n", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.input, func(t *testing.T) { | ||
t.Parallel() | ||
got, err := TomlToJson([]byte(test.input)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expect, string(got)) | ||
}) | ||
} | ||
} | ||
|
||
func TestTomlToJsonError(t *testing.T) { | ||
_, err := TomlToJson([]byte("foo")) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestYamlToJson(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expect string | ||
}{ | ||
{ | ||
input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112\n", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
{ | ||
input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112\n", | ||
expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.input, func(t *testing.T) { | ||
t.Parallel() | ||
got, err := YamlToJson([]byte(test.input)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expect, string(got)) | ||
}) | ||
} | ||
} | ||
|
||
func TestYamlToJsonError(t *testing.T) { | ||
_, err := YamlToJson([]byte("':foo")) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestYamlToJsonSlice(t *testing.T) { | ||
b, err := YamlToJson([]byte(`foo: | ||
- bar | ||
- baz`)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, `{"foo":["bar","baz"]} | ||
`, string(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,27 @@ | ||
package mapping | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/zeromicro/go-zero/core/internal/encoding" | ||
) | ||
|
||
// To make .json & .yaml consistent, we just use json as the tag key. | ||
const yamlTagKey = "json" | ||
|
||
var ( | ||
// ErrUnsupportedType is an error that indicates the config format is not supported. | ||
ErrUnsupportedType = errors.New("only map-like configs are supported") | ||
|
||
yamlUnmarshaler = NewUnmarshaler(yamlTagKey) | ||
) | ||
|
||
// UnmarshalYamlBytes unmarshals content into v. | ||
func UnmarshalYamlBytes(content []byte, v interface{}) error { | ||
func UnmarshalYamlBytes(content []byte, v interface{}, opts ...UnmarshalOption) error { | ||
b, err := encoding.YamlToJson(content) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return unmarshalJsonBytes(b, v, yamlUnmarshaler) | ||
return UnmarshalJsonBytes(b, v, opts...) | ||
} | ||
|
||
// UnmarshalYamlReader unmarshals content from reader into v. | ||
func UnmarshalYamlReader(reader io.Reader, v interface{}) error { | ||
func UnmarshalYamlReader(reader io.Reader, v interface{}, opts ...UnmarshalOption) error { | ||
b, err := io.ReadAll(reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return UnmarshalYamlBytes(b, v) | ||
return UnmarshalYamlBytes(b, v, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters