jx/internal/mapper/mapper.go

51 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-08-18 01:14:54 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package mapper
type Store[Key comparable, Value any] map[Key]Value
func (m Store[Key, Value]) Get(key Key) (Value, bool) {
v, ok := m[key]
return v, ok
}
func (m Store[Key, Value]) Has(key Key) (ok bool) {
_, ok = m[key]
return
}
func (m Store[Key, Value]) Set(key Key, value Value) {
m[key] = value
}
2024-10-09 22:28:00 +00:00
func (m Store[Key, Value]) Delete(key Key) {
delete(m, key)
}
2024-08-18 01:14:54 +00:00
type Mapper interface {
Get(key string) (any, bool)
Has(key string) (bool)
Set(key string, value any)
2024-10-09 22:28:00 +00:00
Delete(key string)
2024-08-18 01:14:54 +00:00
}
type Getter[Key comparable, Value comparable] interface {
Get(key Key) (Value, bool)
Has(key Key) (bool)
}
type Map[Key comparable, Value comparable] interface {
Get(key Key) (Value, bool)
Has(key Key) (bool)
Set(key Key, value Value)
2024-10-09 22:28:00 +00:00
Delete(key Key)
2024-08-18 01:14:54 +00:00
}
func New[Key comparable, Value comparable]() Store[Key, Value] {
return make(Store[Key, Value])
}
func NewString[Value comparable]() Store[string, Value] {
return make(Store[string, Value])
}