diff --git a/internal/config/generic.go b/internal/config/generic.go index f278724..7a27fa1 100644 --- a/internal/config/generic.go +++ b/internal/config/generic.go @@ -9,6 +9,8 @@ import ( "fmt" "decl/internal/data" "decl/internal/folio" + "decl/internal/codec" + "io" ) func init() { @@ -37,6 +39,22 @@ func (g *Generic[Value]) SetParsedURI(uri *url.URL) error { return nil } +func (g *Generic[Value]) Load(r io.Reader) (err error) { + err = codec.NewYAMLDecoder(r).Decode(g) + if err == nil { + _, err = g.Read(context.Background()) + } + return err +} + +func (g *Generic[Value]) LoadYAML(yamlData string) (err error) { + err = codec.NewYAMLStringDecoder(yamlData).Decode(g) + if err == nil { + _, err = g.Read(context.Background()) + } + return err +} + func (g *Generic[Value]) Clone() data.Configuration { jsonGeneric, _ := json.Marshal(g) clone := NewGeneric[Value]() diff --git a/internal/config/schema_test.go b/internal/config/schema_test.go index 44c18c4..08ae697 100644 --- a/internal/config/schema_test.go +++ b/internal/config/schema_test.go @@ -27,10 +27,10 @@ values: bar: quuz ` - testConfig := NewBlock() - e := testConfig.LoadBlock(configBlockYaml) + testConfig := NewGeneric[any]() + + e := testConfig.LoadYAML(configBlockYaml) assert.Nil(t, e) - assert.Equal(t, "foo", testConfig.Name) jsonDoc, jsonErr := json.Marshal(testConfig) assert.Nil(t, jsonErr) @@ -39,11 +39,9 @@ values: assert.Nil(t, schemaErr) } -/* func TestSchemaValidateSchema(t *testing.T) { - s := NewSchema("document") - assert.NotNil(t, s) + s := NewSchema("document") + assert.NotNil(t, s) assert.Nil(t, s.ValidateSchema()) } -*/