87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package config
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/url"
|
|
"fmt"
|
|
"decl/internal/data"
|
|
"decl/internal/folio"
|
|
"decl/internal/codec"
|
|
"io"
|
|
)
|
|
|
|
func init() {
|
|
folio.DocumentRegistry.ConfigurationTypes.Register([]string{"generic"}, func(u *url.URL) data.Configuration {
|
|
g := NewGeneric[any]()
|
|
return g
|
|
})
|
|
}
|
|
|
|
type Generic[Value any] map[string]Value
|
|
|
|
func NewGeneric[Value any]() *Generic[Value] {
|
|
g := make(Generic[Value])
|
|
return &g
|
|
}
|
|
|
|
func (g *Generic[Value]) URI() string {
|
|
return fmt.Sprintf("%s://%s", g.Type(), "")
|
|
}
|
|
|
|
func (g *Generic[Value]) SetURI(uri string) error {
|
|
return nil
|
|
}
|
|
|
|
func (g *Generic[Value]) SetParsedURI(uri data.URIParser) 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]()
|
|
if unmarshalErr := json.Unmarshal(jsonGeneric, clone); unmarshalErr != nil {
|
|
panic(unmarshalErr)
|
|
}
|
|
return clone
|
|
}
|
|
|
|
func (g *Generic[Value]) Type() string {
|
|
return "generic"
|
|
}
|
|
|
|
func (g *Generic[Value]) Read(context.Context) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (g *Generic[Value]) GetValue(name string) (result any, err error) {
|
|
var ok bool
|
|
if result, ok = (*g)[name]; !ok {
|
|
err = data.ErrUnknownConfigurationKey
|
|
}
|
|
return
|
|
}
|
|
|
|
func (g *Generic[Value]) Has(key string) (ok bool) {
|
|
_, ok = (*g)[key]
|
|
return
|
|
}
|