List
This commit is contained in:
parent
acf5845e2f
commit
5ebd7f9537
1 changed files with 19 additions and 14 deletions
33
list.go
33
list.go
|
@ -2,7 +2,7 @@ package state
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type uniqueList struct {
|
||||
|
@ -44,19 +44,24 @@ func (l *uniqueList) ItemExists(key string) (bool, error) {
|
|||
return ok, nil
|
||||
}
|
||||
|
||||
func (l *uniqueList) GetList(data []any) error {
|
||||
element := reflect.TypeOf(data).Elem()
|
||||
var err error
|
||||
|
||||
for _, dataByte := range l.data {
|
||||
new := reflect.New(element)
|
||||
err = json.Unmarshal(dataByte, &new)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data = append(data, new)
|
||||
func (l *uniqueList) GetItem(key string, item any) error {
|
||||
data, ok := l.data[key]
|
||||
if !ok {
|
||||
return errors.New("Item not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
err := json.Unmarshal(data, item)
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *uniqueList) GetKeys() []string {
|
||||
keys := make([]string, len(l.data))
|
||||
|
||||
i := 0
|
||||
for k := range l.data {
|
||||
keys[i] = k
|
||||
i++
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
|
Reference in a new issue