feat: add ParseAs and NewSchema generics
Test / test (push) Successful in 1m38s

Assisted-by: GLM 5.3 Flash
This commit is contained in:
2026-09-22 00:36:10 +02:00
parent b02471c09a
commit 71bd82a7a5
4 changed files with 73 additions and 0 deletions
+23
View File
@@ -8,6 +8,7 @@ import (
"math"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
@@ -773,3 +774,25 @@ func TestValid(t *testing.T) {
t.Errorf("Valid(invalid) = %v, want a SyntaxError", err)
}
}
func TestParseAsAndNewSchema(t *testing.T) {
type Config struct {
Host string `toml:"host"`
Port int `toml:"port"`
}
cfg, err := ParseAs[Config]([]byte("host = \"db\"\nport = 5432\n"))
if err != nil {
t.Fatal(err)
}
if cfg.Host != "db" || cfg.Port != 5432 {
t.Errorf("decoded %+v", cfg)
}
if _, err := ParseAs[Config]([]byte("port =\n")); err == nil {
t.Error("ParseAs(invalid) succeeded, want an error and the zero value")
}
NewSchema[Config]()
if _, ok := structSchemaCache.Load(reflect.TypeFor[Config]()); !ok {
t.Error("NewSchema left no schema in the cache")
}
NewSchema[map[string]any]() // must not panic
}