Add ability to parse quoted strings in meta

This should actually make migration from Caddy easier, because
Caddy's YAML front matter seems to support quoted strings? IDK,
the current Holanet has them. Closes #11
This commit is contained in:
Sasha Koshka 2024-12-09 12:56:16 -05:00
parent b14d92205c
commit 43084fb5bb
2 changed files with 32 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package step
import "strconv"
import "strings"
const metaRule = "---"
@ -52,6 +53,13 @@ func ParseMeta (input string) (Meta, error) {
}
key = strings.ToLower(strings.TrimSpace(key))
value = strings.TrimSpace(value)
if strings.HasPrefix(value, "\"") || strings.HasPrefix(value, "'") {
unquoted, err := strconv.Unquote(value)
if err != nil {
return nil, err
}
value = unquoted
}
if key == "" {
return nil, ErrMetaMalformed
}

View File

@ -85,3 +85,27 @@ func TestSplitMetaCRLF (test *testing.T) {
if value3 != "that" { test.Fatal("value is not correct") }
if value4 != quickBrownFox { test.Fatal("value is not correct") }
}
func TestParseMeta (test *testing.T) {
meta, err := ParseMeta(
`thing: " Quoted string!!!!! "
Other-Thing: askdjlksajd
number: 3849
`)
if err != nil {
test.Fatal(err)
}
test.Log("META:")
test.Log(meta)
value0, ok := meta["thing"]
if !ok { test.Fatal("missing key") }
value1, ok := meta["other-thing"]
if !ok { test.Fatal("missing key") }
value2, ok := meta["number"]
if !ok { test.Fatal("missing key") }
if value0 != "\tQuoted string!!!!!m " { test.Fatal("value is not correct") }
if value1 != "askdjlksajd" { test.Fatal("value is not correct") }
if value2 != "3849" { test.Fatal("value is not correct") }
}