add readme for each package
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
# Go Registry
|
||||||
|
|
||||||
|
This package provides a simple registry implementation which allows storing arbitrary values in a memory map.
|
||||||
|
|
||||||
|
You can also lock specific keys to prevent them being written, even if no value has yet been set. Locked keys cannot be unlocked.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/annybs/go/registry"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := registry.New()
|
||||||
|
|
||||||
|
r.Set("some key", "some text")
|
||||||
|
fmt.Println(r.Get("some key"))
|
||||||
|
|
||||||
|
r.Lock("some key")
|
||||||
|
if err := r.Set("some key", "different text"); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
See [LICENSE.md](../LICENSE.md)
|
||||||
+9
-9
@@ -12,41 +12,41 @@ var (
|
|||||||
// Registry is a simple interface allowing untyped common values to be shared across packages.
|
// Registry is a simple interface allowing untyped common values to be shared across packages.
|
||||||
// This is particularly useful for resolving import cycles.
|
// This is particularly useful for resolving import cycles.
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
Data map[string]any
|
data map[string]any
|
||||||
Locks map[string]bool
|
locks map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the value of a key.
|
// Get the value of a key.
|
||||||
// This function returns nil if no value exists.
|
// This function returns nil if no value exists.
|
||||||
func (reg *Registry) Get(key string) any {
|
func (reg *Registry) Get(key string) any {
|
||||||
return reg.Data[key]
|
return reg.data[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Has checks whether a key is non-nil.
|
// Has checks whether a key is non-nil.
|
||||||
func (reg *Registry) Has(key string) bool {
|
func (reg *Registry) Has(key string) bool {
|
||||||
return reg.Data[key] == nil
|
return reg.data[key] == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock a key.
|
// Lock a key.
|
||||||
// This prevents the value being overwritten by Set.
|
// This prevents the value being overwritten by Set.
|
||||||
func (reg *Registry) Lock(key string) {
|
func (reg *Registry) Lock(key string) {
|
||||||
reg.Locks[key] = true
|
reg.locks[key] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the value for a key.
|
// Set the value for a key.
|
||||||
// This function returns an error if the key is locked.
|
// This function returns an error if the key is locked.
|
||||||
func (reg *Registry) Set(key string, value any) error {
|
func (reg *Registry) Set(key string, value any) error {
|
||||||
if reg.Locks[key] {
|
if reg.locks[key] {
|
||||||
return ErrKeyLocked
|
return ErrKeyLocked
|
||||||
}
|
}
|
||||||
reg.Data[key] = value
|
reg.data[key] = value
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new registry.
|
// New creates a new registry.
|
||||||
func New() *Registry {
|
func New() *Registry {
|
||||||
return &Registry{
|
return &Registry{
|
||||||
Data: map[string]any{},
|
data: map[string]any{},
|
||||||
Locks: map[string]bool{},
|
locks: map[string]bool{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user