Add config loader with strict stdlib-only TOML subset parser
Parse keyproxy.toml (listen address, [auth] token_ref, and the mpk-<name>
ref map) using a deliberate TOML subset implemented with the standard
library only. The surface is strict: unknown keys, malformed refs, and
backend-specific misconfigurations (e.g. env refs setting key, file refs
missing key) fail loudly at startup. The bearer-token ref must resolve
through the file backend. Parse errors carry line numbers, never line
contents.
💘 Generated with Crush
Assisted-by: Crush:glm-5.2
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseTOML(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
want map[string]any // dotted paths -> expected values
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "scalars and comments",
|
||||
in: "# top\nlisten = \"127.0.0.1:8082\" # inline\nflag = true\nnum = 42\n",
|
||||
want: map[string]any{"listen": "127.0.0.1:8082", "flag": true, "num": int64(42)},
|
||||
},
|
||||
{
|
||||
name: "tables with quoted dotted keys",
|
||||
in: "[refs.\"mpk-redmine\"]\nbackend = \"file\"\n\n[refs.\"mpk-env\"]\nbackend = \"env\"\n",
|
||||
want: map[string]any{"refs.mpk-redmine.backend": "file", "refs.mpk-env.backend": "env"},
|
||||
},
|
||||
{
|
||||
name: "escapes in basic strings",
|
||||
in: "a = \"x\\ty\\\"z\"\n",
|
||||
want: map[string]any{"a": "x\ty\"z"},
|
||||
},
|
||||
{
|
||||
name: "literal strings keep backslashes",
|
||||
in: "a = 'C:\\path'\n",
|
||||
want: map[string]any{"a": `C:\path`},
|
||||
},
|
||||
{
|
||||
name: "duplicate key",
|
||||
in: "a = \"1\"\na = \"2\"\n",
|
||||
wantErr: "duplicate key",
|
||||
},
|
||||
{
|
||||
name: "unterminated string",
|
||||
in: "a = \"oops\n",
|
||||
wantErr: "unterminated string",
|
||||
},
|
||||
{
|
||||
name: "unsupported richer syntax fails loudly",
|
||||
in: "a = [\"x\", \"y\"]\n",
|
||||
wantErr: "unsupported value",
|
||||
},
|
||||
{
|
||||
name: "malformed table header",
|
||||
in: "[refs\n",
|
||||
wantErr: "malformed table header",
|
||||
},
|
||||
{
|
||||
name: "bare line without equals",
|
||||
in: "garbage\n",
|
||||
wantErr: "expected key = value",
|
||||
},
|
||||
{
|
||||
name: "table conflicts with scalar",
|
||||
in: "refs = \"x\"\n[refs.\"mpk-a\"]\nbackend = \"file\"\n",
|
||||
wantErr: "conflicts",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
doc, err := parseTOML([]byte(tt.in))
|
||||
if tt.wantErr != "" {
|
||||
if err == nil {
|
||||
t.Fatalf("want error %q, got doc %v", tt.wantErr, doc)
|
||||
}
|
||||
if !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Fatalf("error %q does not contain %q", err, tt.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
for path, want := range tt.want {
|
||||
segs := strings.Split(path, ".")
|
||||
cur := any(doc)
|
||||
for _, seg := range segs {
|
||||
m, ok := cur.(Doc)
|
||||
if !ok {
|
||||
t.Fatalf("path %s: %v is not a table", path, cur)
|
||||
}
|
||||
cur = m[seg]
|
||||
}
|
||||
if cur != want {
|
||||
t.Fatalf("path %s: got %#v, want %#v", path, cur, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user