49 lines
963 B
Go
49 lines
963 B
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package config
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/url"
|
|
)
|
|
|
|
func init() {
|
|
ConfigTypes.Register([]string{"generic"}, func(u *url.URL) 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]) Clone() 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 = ErrUnknownConfigurationKey
|
|
}
|
|
return
|
|
}
|