jx/internal/config/configuration.go

49 lines
1.1 KiB
Go
Raw Normal View History

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package config
import (
"errors"
"fmt"
_ "net/url"
_ "decl/internal/codec"
_ "io"
"decl/internal/types"
"decl/internal/data"
"strings"
)
var (
ErrUnknownConfigurationType = errors.New("Unknown configuration type")
ErrUnknownConfigurationKey = errors.New("Unknown configuration key")
ConfigTypes *types.Types[Configuration] = types.New[Configuration]()
ConfigSourceTypes *types.Types[ConfigSource] = types.New[ConfigSource]()
ConfigTargetTypes *types.Types[ConfigTarget] = types.New[ConfigTarget]()
)
type TypeName string //`json:"type"`
type Configuration interface {
Type() string
data.Reader
GetValue(name string) (any, error)
Clone() Configuration
}
func NewConfiguration(uri string) Configuration {
c, e := ConfigTypes.New(uri)
if e == nil {
return c
}
return nil
}
func (n *TypeName) UnmarshalJSON(b []byte) error {
ConfigTypeName := strings.Trim(string(b), "\"")
if ConfigTypes.Has(ConfigTypeName) {
*n = TypeName(ConfigTypeName)
return nil
}
return fmt.Errorf("%w: %s", ErrUnknownConfigurationType, ConfigTypeName)
}