mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parser/metadecoders: Simplify nil check in Unmarshal
This commit is contained in:
parent
e30d711c29
commit
2a81a49499
2 changed files with 14 additions and 13 deletions
|
@ -112,7 +112,7 @@ func (d Decoder) UnmarshalStringTo(data string, typ any) (any, error) {
|
||||||
// Unmarshal will unmarshall data in format f into an interface{}.
|
// Unmarshal will unmarshall data in format f into an interface{}.
|
||||||
// This is what's needed for Hugo's /data handling.
|
// This is what's needed for Hugo's /data handling.
|
||||||
func (d Decoder) Unmarshal(data []byte, f Format) (any, error) {
|
func (d Decoder) Unmarshal(data []byte, f Format) (any, error) {
|
||||||
if data == nil || len(data) == 0 {
|
if len(data) == 0 {
|
||||||
switch f {
|
switch f {
|
||||||
case CSV:
|
case CSV:
|
||||||
return make([][]string, 0), nil
|
return make([][]string, 0), nil
|
||||||
|
|
|
@ -116,22 +116,23 @@ func TestUnmarshalToInterface(t *testing.T) {
|
||||||
d := Default
|
d := Default
|
||||||
|
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
data string
|
data []byte
|
||||||
format Format
|
format Format
|
||||||
expect any
|
expect any
|
||||||
}{
|
}{
|
||||||
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []any{"Brecker", "Blake", "Redman"}},
|
{[]byte(`[ "Brecker", "Blake", "Redman" ]`), JSON, []any{"Brecker", "Blake", "Redman"}},
|
||||||
{`{ "a": "b" }`, JSON, expect},
|
{[]byte(`{ "a": "b" }`), JSON, expect},
|
||||||
{``, JSON, map[string]any{}},
|
{[]byte(``), JSON, map[string]any{}},
|
||||||
{`#+a: b`, ORG, expect},
|
{[]byte(nil), JSON, map[string]any{}},
|
||||||
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]any{"date": "2020-06-26"}},
|
{[]byte(`#+a: b`), ORG, expect},
|
||||||
{`a = "b"`, TOML, expect},
|
{[]byte(`#+DATE: <2020-06-26 Fri>`), ORG, map[string]any{"date": "2020-06-26"}},
|
||||||
{`a: "b"`, YAML, expect},
|
{[]byte(`a = "b"`), TOML, expect},
|
||||||
{`<root><a>b</a></root>`, XML, expect},
|
{[]byte(`a: "b"`), YAML, expect},
|
||||||
{`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},
|
{[]byte(`<root><a>b</a></root>`), XML, expect},
|
||||||
{"a: Easy!\nb:\n c: 2\n d: [3, 4]", YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
|
{[]byte(`a,b,c`), CSV, [][]string{{"a", "b", "c"}}},
|
||||||
|
{[]byte("a: Easy!\nb:\n c: 2\n d: [3, 4]"), YAML, map[string]any{"a": "Easy!", "b": map[string]any{"c": 2, "d": []any{3, 4}}}},
|
||||||
// errors
|
// errors
|
||||||
{`a = "`, TOML, false},
|
{[]byte(`a = "`), TOML, false},
|
||||||
} {
|
} {
|
||||||
msg := qt.Commentf("%d: %s", i, test.format)
|
msg := qt.Commentf("%d: %s", i, test.format)
|
||||||
m, err := d.Unmarshal([]byte(test.data), test.format)
|
m, err := d.Unmarshal([]byte(test.data), test.format)
|
||||||
|
|
Loading…
Reference in a new issue