feat: added configuration center function (#3035)

Co-authored-by: aiden.ma <Aiden.ma@yijinin.com>
This commit is contained in:
MarkJoyMa
2024-08-28 14:47:52 +08:00
committed by GitHub
parent 47d13e5ef8
commit 44cddec5c3
9 changed files with 628 additions and 25 deletions

View File

@@ -0,0 +1,46 @@
package configurator
import (
"sync"
"github.com/zeromicro/go-zero/core/conf"
)
type (
// unmarshalerRegistry is the registry for unmarshalers.
unmarshalerRegistry struct {
unmarshalers map[string]LoaderFn
mu sync.RWMutex
}
// LoaderFn is the function type for loading configuration.
LoaderFn func([]byte, any) error
)
var defaultRegistry *unmarshalerRegistry
func init() {
defaultRegistry = &unmarshalerRegistry{
unmarshalers: map[string]LoaderFn{
"json": conf.LoadFromJsonBytes,
"toml": conf.LoadFromTomlBytes,
"yaml": conf.LoadFromYamlBytes,
},
}
}
// RegisterUnmarshaler registers an unmarshaler.
func RegisterUnmarshaler(name string, fn LoaderFn) {
defaultRegistry.mu.Lock()
defaultRegistry.unmarshalers[name] = fn
defaultRegistry.mu.Unlock()
}
// Unmarshaler returns the unmarshaler by name.
func Unmarshaler(name string) (LoaderFn, bool) {
defaultRegistry.mu.RLock()
fn, ok := defaultRegistry.unmarshalers[name]
defaultRegistry.mu.RUnlock()
return fn, ok
}