1 Star 0 Fork 0

sy_183 / go-common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
map.go 2.76 KB
一键复制 编辑 原始数据 按行查看 历史
sy_183 提交于 2023-07-20 16:10 . 修改了模块名称
package config2
import (
"gitee.com/sy_183/go-common/container"
"gopkg.in/yaml.v3"
"reflect"
)
type EntryType interface {
Zero() any
Check(value any) error
}
type ZeroEntryType[V any] struct {
Checker func(value V) error
}
func (t ZeroEntryType[V]) Zero() any {
var v V
return v
}
func (t ZeroEntryType[V]) Check(value any) error {
if t.Checker != nil {
return t.Checker(value.(V))
}
return nil
}
func NewZeroEntryType[V any](checker func(value V) error) EntryType {
return ZeroEntryType[V]{Checker: checker}
}
type EntryHandler interface {
Handle(zero any) (any, error)
}
type yamlHandler struct {
value *yaml.Node
}
func YAMLHandler(value *yaml.Node) EntryHandler {
return yamlHandler{value: value}
}
func reflectNewPointer(v any) reflect.Value {
vt, vv := reflect.TypeOf(v), reflect.ValueOf(v)
npv := reflect.New(vt)
npv.Elem().Set(vv)
return npv
}
func (h yamlHandler) Handle(zero any) (any, error) {
vp := reflectNewPointer(zero)
if err := h.value.Decode(vp.Interface()); err != nil {
return nil, err
}
return vp.Elem().Interface(), nil
}
type Entry struct {
Type EntryType
Value any
}
func NewEntry(typ EntryType, value any) Entry {
return Entry{Type: typ, Value: value}
}
func NewEntryWithHandler(typ EntryType, handler EntryHandler) (Entry, error) {
e := Entry{Type: typ}
if err := e.Handle(handler); err != nil {
return Entry{}, err
}
return e, nil
}
func (e Entry) IsNil() bool {
return e.Type == nil
}
func (e *Entry) Handle(handler EntryHandler) error {
if e.Type == nil {
return nil
}
if v, err := handler.Handle(e.Type.Zero()); err != nil {
return err
} else if err = e.Type.Check(v); err != nil {
return err
} else {
e.Value = v
}
return nil
}
type Map struct {
m *container.LinkedMap[string, Entry]
}
func NewMap() Map {
return Map{m: container.NewLinkedMap[string, Entry](0)}
}
func (m *Map) Get(key string) any {
if m.m != nil {
if e := m.m.GetEntry(key); e != nil {
return e.Value().Value
}
}
return nil
}
func (m *Map) ensureMap() {
if m.m == nil {
m.m = container.NewLinkedMap[string, Entry](0)
}
}
func (m *Map) SetEntry(ket string, entry Entry) {
if entry.IsNil() {
return
}
m.ensureMap()
m.m.Put(ket, entry)
}
func (m *Map) Set(key string, typ EntryType, value any) {
m.SetEntry(key, NewEntry(typ, value))
}
func (m *Map) HandleAndSet(key string, typ EntryType, handler EntryHandler) error {
entry, err := NewEntryWithHandler(typ, handler)
if err != nil {
return err
}
m.SetEntry(key, entry)
return nil
}
func (m *Map) Remove(key string) {
if m.m != nil {
m.m.Remove(key)
}
}
func (m *Map) Range(f func(key string, typ EntryType, value any) bool) bool {
if m.m != nil {
return m.m.Range(func(key string, entry Entry) bool {
return f(key, entry.Type, entry.Value)
})
}
return true
}
1
https://gitee.com/sy_183/go-common.git
git@gitee.com:sy_183/go-common.git
sy_183
go-common
go-common
v1.0.4

搜索帮助

53164aa7 5694891 3bd8fe86 5694891