diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-12-14 15:19:04 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-12-14 18:03:47 +0100 |
commit | 2a81a4949921d1d04cefc45bd6e1ab583c388279 (patch) | |
tree | ee25eb7a341efa7b4639fa9023c2a724d19200f3 | |
parent | e30d711c29b3a8e93a2e2062149d8f8d9c841f5e (diff) | |
download | hugo-2a81a4949921d1d04cefc45bd6e1ab583c388279.tar.gz hugo-2a81a4949921d1d04cefc45bd6e1ab583c388279.zip |
parser/metadecoders: Simplify nil check in Unmarshal
-rw-r--r-- | parser/metadecoders/decoder.go | 2 | ||||
-rw-r--r-- | parser/metadecoders/decoder_test.go | 25 |
2 files changed, 14 insertions, 13 deletions
diff --git a/parser/metadecoders/decoder.go b/parser/metadecoders/decoder.go index a31bcd5cd..3f322ad92 100644 --- a/parser/metadecoders/decoder.go +++ b/parser/metadecoders/decoder.go @@ -112,7 +112,7 @@ func (d Decoder) UnmarshalStringTo(data string, typ any) (any, error) { // Unmarshal will unmarshall data in format f into an interface{}. // This is what's needed for Hugo's /data handling. func (d Decoder) Unmarshal(data []byte, f Format) (any, error) { - if data == nil || len(data) == 0 { + if len(data) == 0 { switch f { case CSV: return make([][]string, 0), nil diff --git a/parser/metadecoders/decoder_test.go b/parser/metadecoders/decoder_test.go index fbeab41a4..5e917beff 100644 --- a/parser/metadecoders/decoder_test.go +++ b/parser/metadecoders/decoder_test.go @@ -116,22 +116,23 @@ func TestUnmarshalToInterface(t *testing.T) { d := Default for i, test := range []struct { - data string + data []byte format Format expect any }{ - {`[ "Brecker", "Blake", "Redman" ]`, JSON, []any{"Brecker", "Blake", "Redman"}}, - {`{ "a": "b" }`, JSON, expect}, - {``, JSON, map[string]any{}}, - {`#+a: b`, ORG, expect}, - {`#+DATE: <2020-06-26 Fri>`, ORG, map[string]any{"date": "2020-06-26"}}, - {`a = "b"`, TOML, expect}, - {`a: "b"`, YAML, expect}, - {`<root><a>b</a></root>`, XML, expect}, - {`a,b,c`, CSV, [][]string{{"a", "b", "c"}}}, - {"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(`[ "Brecker", "Blake", "Redman" ]`), JSON, []any{"Brecker", "Blake", "Redman"}}, + {[]byte(`{ "a": "b" }`), JSON, expect}, + {[]byte(``), JSON, map[string]any{}}, + {[]byte(nil), JSON, map[string]any{}}, + {[]byte(`#+a: b`), ORG, expect}, + {[]byte(`#+DATE: <2020-06-26 Fri>`), ORG, map[string]any{"date": "2020-06-26"}}, + {[]byte(`a = "b"`), TOML, expect}, + {[]byte(`a: "b"`), YAML, expect}, + {[]byte(`<root><a>b</a></root>`), XML, expect}, + {[]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 - {`a = "`, TOML, false}, + {[]byte(`a = "`), TOML, false}, } { msg := qt.Commentf("%d: %s", i, test.format) m, err := d.Unmarshal([]byte(test.data), test.format) |