Validate final identity fields as application payload
This commit is contained in:
@@ -1,12 +1,60 @@
|
||||
package configkit_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ConfigKit"
|
||||
)
|
||||
|
||||
func TestFinalSequenceIdentityTreatsLateExtendsFieldsAsPayload(t *testing.T) {
|
||||
for _, phase := range []string{"defaults", "resolution"} {
|
||||
for _, component := range []any{"application-data", nil} {
|
||||
for _, duplicate := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("%s/%v/duplicate=%t", phase, component, duplicate), func(t *testing.T) {
|
||||
items := []any{map[string]any{"extends": component}}
|
||||
if duplicate {
|
||||
items = append(items, map[string]any{"extends": component})
|
||||
}
|
||||
definition := configkit.Definition[map[string]any]{
|
||||
SequenceIdentities: []configkit.SequenceIdentity{configkit.SequenceIdentityAt(configkit.Keys("items"), "extends")},
|
||||
Defaults: func() map[string]any {
|
||||
if phase == "defaults" {
|
||||
return map[string]any{"items": items}
|
||||
}
|
||||
return map[string]any{}
|
||||
},
|
||||
}
|
||||
if phase == "resolution" {
|
||||
definition.Resolve = func(value *map[string]any, resolution *configkit.Resolution) error {
|
||||
(*value)["items"] = items
|
||||
resolution.Record(configkit.Keys("items"), configkit.ChangeNormalization, "supply application payload")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
loaded, err := configkit.Load(configkit.NoSource(), configkit.Options[map[string]any]{Definition: definition})
|
||||
if duplicate {
|
||||
loadErr := requireFacadeLoadError(t, err)
|
||||
diagnostics := loadErr.Diagnostics()
|
||||
if loaded != nil || len(diagnostics) != 1 || diagnostics[0].Code != configkit.CodeSequenceConflict || diagnostics[0].Path.String() != "/items" {
|
||||
t.Fatalf("duplicate effective payload diagnostics = %#v", diagnostics)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil || loaded == nil {
|
||||
t.Fatalf("ordinary effective identity was interpreted as a directive: %v", err)
|
||||
}
|
||||
value := loaded.Config["items"].([]any)[0].(map[string]any)
|
||||
if actual, present := value["extends"]; !present || actual != component {
|
||||
t.Fatalf("effective payload = %#v", value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalSequenceIdentityValidatesLateValues(t *testing.T) {
|
||||
identity := configkit.SequenceIdentityAt(configkit.Keys("items"), "id")
|
||||
for _, source := range []string{"defaults", "CLI", "resolution"} {
|
||||
|
||||
Reference in New Issue
Block a user