Load shared references with ConfigKit
This commit is contained in:
@@ -140,6 +140,8 @@ selected upstream commit, and `git diff --check`.
|
||||
|
||||
## Stage 2 — Load Shared References Through ConfigKit
|
||||
|
||||
**Status:** Complete.
|
||||
|
||||
**Goal:** Delegate parsing and resolution for both serializations to ConfigKit.
|
||||
|
||||
**Inspect:** ConfigKit's pinned public `Load`, `Bytes`, and dynamic-value contracts;
|
||||
|
||||
27
internal/integrations/domainrefs/load.go
Normal file
27
internal/integrations/domainrefs/load.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Package domainrefs loads shared domain reference documents.
|
||||
package domainrefs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
// Load resolves a materialized shared reference using ConfigKit's native rules.
|
||||
func Load(label, mediaType string, content []byte) (map[string]any, error) {
|
||||
switch mediaType {
|
||||
case "application/json":
|
||||
if !json.Valid(content) {
|
||||
return nil, fmt.Errorf("reference %q: invalid JSON syntax", label)
|
||||
}
|
||||
case "application/yaml", "application/x-yaml":
|
||||
default:
|
||||
return nil, fmt.Errorf("reference %q: unsupported media type %q", label, mediaType)
|
||||
}
|
||||
result, err := configkit.Load[map[string]any](configkit.Bytes(label, content))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reference %q: %w", label, err)
|
||||
}
|
||||
return result.Config, nil
|
||||
}
|
||||
70
internal/integrations/domainrefs/load_test.go
Normal file
70
internal/integrations/domainrefs/load_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package domainrefs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestLoadEquivalentStructuredReferences(t *testing.T) {
|
||||
yaml := []byte("name: Example\nnumber: 42\nfraction: 1.5\ninstruction: data\nnested: {enabled: true}\n")
|
||||
json := []byte(`{"name":"Example","number":42,"fraction":1.5,"instruction":"data","nested":{"enabled":true}}`)
|
||||
want, err := Load("example.yml", "application/yaml", yaml)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, input := range []struct {
|
||||
media string
|
||||
data []byte
|
||||
}{
|
||||
{"application/x-yaml", yaml}, {"application/json", json},
|
||||
} {
|
||||
got, err := Load("example", input.media, input.data)
|
||||
if err != nil || !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("Load(%s) = %#v, %v; want %#v", input.media, got, err, want)
|
||||
}
|
||||
}
|
||||
if want["number"] != int64(42) || want["fraction"] != float64(1.5) || want["instruction"] != "data" {
|
||||
t.Fatalf("unexpected native values: %#v", want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadPreservesNativeFailure(t *testing.T) {
|
||||
_, err := Load("roster.yml", "application/yaml", []byte("name: one\nname: two\n"))
|
||||
var native *configkit.LoadError
|
||||
if !errors.As(err, &native) || !strings.Contains(err.Error(), "roster.yml") {
|
||||
t.Fatalf("expected contextual native error, got %v", err)
|
||||
}
|
||||
if _, err := Load("roster.json", "application/json", []byte("name: value")); err == nil {
|
||||
t.Fatal("JSON media accepted YAML syntax")
|
||||
}
|
||||
_, err = Load("roster.json", "application/json", []byte(`{"name":"one","name":"two"}`))
|
||||
if !errors.As(err, &native) {
|
||||
t.Fatalf("expected native error for duplicate JSON keys, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadNativeStructuralControls(t *testing.T) {
|
||||
got, err := Load("reference.yml", "application/yaml", []byte(`entries:
|
||||
parent: {name: inherited}
|
||||
child: {extends: parent, instruction: ordinary}
|
||||
`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
child := got["entries"].(map[string]any)["child"].(map[string]any)
|
||||
if child["name"] != "inherited" || child["instruction"] != "ordinary" {
|
||||
t.Fatalf("unexpected resolved child: %#v", child)
|
||||
}
|
||||
if _, ok := child["extends"]; ok {
|
||||
t.Fatalf("inheritance control was not consumed: %#v", child)
|
||||
}
|
||||
_, err = Load("reference.yml", "application/yaml", []byte("config: {imports: [{path: missing.yml}]}\n"))
|
||||
var native *configkit.LoadError
|
||||
if !errors.As(err, &native) {
|
||||
t.Fatalf("expected native disabled-import error, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user