Merge branch 'feat/47-G-fix11-b5g3' into feat/47-G-join11
This commit is contained in:
@@ -2,14 +2,18 @@ package meshtest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
)
|
||||
|
||||
// State is a module's state as a fixture, the shape a gate's Store takes: Fail makes every read and write fail, as a bus
|
||||
// refusing them; Vals are its values by key.
|
||||
// refusing them; Vals are its values by key. A key the bus refuses (a name outside letters, digits and -/_=, in
|
||||
// dot-separated names) is refused here too, so no test passes a key the live bus refuses (lead answer A-209).
|
||||
type State struct {
|
||||
mu sync.Mutex
|
||||
Vals map[string]json.RawMessage
|
||||
@@ -18,12 +22,25 @@ type State struct {
|
||||
Fail error
|
||||
}
|
||||
|
||||
// checkKey refuses a key the bus would.
|
||||
func checkKey(k string) error {
|
||||
for _, part := range strings.Split(k, ".") {
|
||||
if keys.CheckName("key", part) != nil {
|
||||
return fmt.Errorf("%q is not a key the bus can hold: letters, digits and -/_=, in dot-separated names", k)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewState is an empty State.
|
||||
func NewState() *State { return &State{Vals: map[string]json.RawMessage{}, revs: map[string]uint64{}} }
|
||||
|
||||
func (m *State) Get(k string) (*stdio.StateEntry, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if err := checkKey(k); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m.Fail != nil {
|
||||
return nil, m.Fail
|
||||
}
|
||||
@@ -36,6 +53,9 @@ func (m *State) Get(k string) (*stdio.StateEntry, error) {
|
||||
func (m *State) Create(k string, v any) (uint64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if err := checkKey(k); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if m.Fail != nil {
|
||||
return 0, m.Fail
|
||||
}
|
||||
@@ -47,6 +67,9 @@ func (m *State) Create(k string, v any) (uint64, error) {
|
||||
func (m *State) Update(k string, v any, rev uint64) (uint64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if err := checkKey(k); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if m.Fail != nil {
|
||||
return 0, m.Fail
|
||||
}
|
||||
@@ -58,6 +81,9 @@ func (m *State) Update(k string, v any, rev uint64) (uint64, error) {
|
||||
func (m *State) Put(k string, v any) (uint64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if err := checkKey(k); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if m.Fail != nil {
|
||||
return 0, m.Fail
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user