Reject unsupported inline pointer schemas safely
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package configkit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -302,3 +303,40 @@ func mustPath(t *testing.T, pointer string) Path {
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestInlineMapPointersFailBeforeDefaults(t *testing.T) {
|
||||
type cfg struct {
|
||||
Extra *map[string]int `yaml:",inline"`
|
||||
}
|
||||
for _, nonNil := range []bool{false, true} {
|
||||
called := false
|
||||
_, err := Load(NoSource(), Options[cfg]{Definition: Definition[cfg]{Defaults: func() cfg {
|
||||
called = true
|
||||
if nonNil {
|
||||
m := map[string]int{"value": 1}
|
||||
return cfg{Extra: &m}
|
||||
}
|
||||
return cfg{}
|
||||
}}})
|
||||
var loadErr *LoadError
|
||||
if !errors.As(err, &loadErr) || called {
|
||||
t.Fatalf("invalid inline schema reached defaults: called=%v error=%v", called, err)
|
||||
}
|
||||
if len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != CodeSchemaInline {
|
||||
t.Fatal("missing inline schema diagnostic")
|
||||
}
|
||||
}
|
||||
type pointer *pointer
|
||||
type recursive struct {
|
||||
Extra pointer `yaml:",inline"`
|
||||
}
|
||||
if _, err := Load[recursive](NoSource()); err == nil {
|
||||
t.Fatal("recursive inline pointer was accepted")
|
||||
}
|
||||
type double struct {
|
||||
Extra **map[string]int `yaml:",inline"`
|
||||
}
|
||||
if _, err := Load[double](NoSource()); err == nil {
|
||||
t.Fatal("double inline map pointer was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user