jx/internal/config/generic.go

49 lines
963 B
Go
Raw Normal View History

// 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 {
2024-07-17 08:34:57 +00:00
g := NewGeneric[any]()
return g
})
}
2024-07-17 08:34:57 +00:00
type Generic[Value any] map[string]Value
2024-07-17 08:34:57 +00:00
func NewGeneric[Value any]() *Generic[Value] {
g := make(Generic[Value])
return &g
}
2024-07-17 08:34:57 +00:00
func (g *Generic[Value]) Clone() Configuration {
jsonGeneric, _ := json.Marshal(g)
2024-07-17 08:34:57 +00:00
clone := NewGeneric[Value]()
if unmarshalErr := json.Unmarshal(jsonGeneric, clone); unmarshalErr != nil {
panic(unmarshalErr)
}
2024-07-17 08:34:57 +00:00
return clone
}
2024-07-17 08:34:57 +00:00
func (g *Generic[Value]) Type() string {
return "generic"
}
2024-07-17 08:34:57 +00:00
func (g *Generic[Value]) Read(context.Context) ([]byte, error) {
return nil, nil
}
2024-07-17 08:34:57 +00:00
func (g *Generic[Value]) GetValue(name string) (result any, err error) {
var ok bool
if result, ok = (*g)[name]; !ok {
err = ErrUnknownConfigurationKey
}
return
}