v0.4.0 Unmapped reads any parsed value, UnknownKeys refuses what it names, decode errors name the key path
Test / test (push) Canceled after 0s
Test / test (push) Canceled after 0s
This commit is contained in:
5 files changed
+80
-16
No files matched your search
+24
-5
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -253,12 +254,30 @@ func (d *Document) Tree() map[string]any { return d.tree }
|
||||
func (d *Document) Decode(v any) error { return Decode(d.tree, v) }
|
||||
|
||||
// Unmapped names the keys of the document that no field of v's type takes, as
|
||||
// Decode, which ignores them, would match them; each once and sorted. A key under
|
||||
// a map or interface destination is taken, and one under an array of tables is
|
||||
// named without its index.
|
||||
func (d *Document) Unmapped(v any) []Key {
|
||||
// the function Unmapped does
|
||||
func (d *Document) Unmapped(v any) []Key { return Unmapped(d.tree, v) }
|
||||
|
||||
// UnknownKeys is the error of a caller that refuses the keys Unmapped names
|
||||
type UnknownKeys []Key
|
||||
|
||||
func (u UnknownKeys) Error() string {
|
||||
q := make([]string, len(u))
|
||||
for i, k := range u {
|
||||
q[i] = strconv.Quote(k.String())
|
||||
}
|
||||
if len(q) == 1 {
|
||||
return "unknown key " + q[0]
|
||||
}
|
||||
return "unknown keys " + strings.Join(q, ", ")
|
||||
}
|
||||
|
||||
// Unmapped names the keys of data, a value Parse produced, that no field of v's
|
||||
// type takes, as Decode, which ignores them, would match them; each once and
|
||||
// sorted. A key under a map or interface destination is taken, and one under an
|
||||
// array of tables is named without its index.
|
||||
func Unmapped(data any, v any) []Key {
|
||||
seen := make(map[string]Key)
|
||||
unmapped(d.tree, reflect.TypeOf(v), nil, seen, 0)
|
||||
unmapped(data, reflect.TypeOf(v), nil, seen, 0)
|
||||
out := make([]Key, 0, len(seen))
|
||||
for _, k := range seen {
|
||||
out = append(out, k)
|
||||
|
||||
Reference in new issue
Block a user