jx/internal/config/generic.go

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