Skip to content

Commit

Permalink
Merge pull request #1131 from k1LoW/make-kv-internal
Browse files Browse the repository at this point in the history
Move kv to internal/kv
  • Loading branch information
k1LoW authored Dec 25, 2024
2 parents 049172d + a84e261 commit 91a321f
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 116 deletions.
50 changes: 50 additions & 0 deletions internal/kv/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kv

import "sync"

type KV struct {
mu sync.RWMutex
m map[string]any
}

func New() *KV {
return &KV{m: map[string]any{}}
}

func (kv *KV) Set(k string, v any) {
kv.mu.Lock()
defer kv.mu.Unlock()
kv.m[k] = v
}

func (kv *KV) Get(k string) any { //nostyle:getters
kv.mu.RLock()
defer kv.mu.RUnlock()
v, ok := kv.m[k]
if !ok {
return nil
}
return v
}

func (kv *KV) Keys() []string {
kv.mu.RLock()
defer kv.mu.RUnlock()
keys := make([]string, 0, len(kv.m))
for k := range kv.m {
keys = append(keys, k)
}
return keys
}

func (kv *KV) Del(k string) {
kv.mu.RLock()
defer kv.mu.RUnlock()
delete(kv.m, k)
}

func (kv *KV) Clear() {
kv.mu.Lock()
defer kv.mu.Unlock()
kv.m = map[string]any{}
}
54 changes: 54 additions & 0 deletions internal/kv/kv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package kv

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestKV(t *testing.T) {
tests := []struct {
in any
}{
{nil},
{"str"},
{3},
{4.5},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("set/get/del %v", tt.in), func(t *testing.T) {
kv := New()
kv.Set("key", tt.in)
got := kv.Get("key")
if diff := cmp.Diff(got, tt.in); diff != "" {
t.Error(diff)
}

{
kv.Del("key")
got := kv.Get("key")
if got != nil {
t.Errorf("got %v, want %v", got, nil)
}
}
})

t.Run(fmt.Sprintf("set/get/clear %v", tt.in), func(t *testing.T) {
kv := New()
kv.Set("key", tt.in)
got := kv.Get("key")
if diff := cmp.Diff(got, tt.in); diff != "" {
t.Error(diff)
}

{
kv.Clear()
got := kv.Get("key")
if got != nil {
t.Errorf("got %v, want %v", got, nil)
}
}
})
}
}
40 changes: 0 additions & 40 deletions kv.go

This file was deleted.

47 changes: 0 additions & 47 deletions kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,13 @@ package runn

import (
"context"
"fmt"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestKV(t *testing.T) {
tests := []struct {
in any
}{
{nil},
{"str"},
{3},
{4.5},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("set/get/del %v", tt.in), func(t *testing.T) {
kv := newKV()
kv.set("key", tt.in)
got := kv.get("key")
if diff := cmp.Diff(got, tt.in); diff != "" {
t.Error(diff)
}

{
kv.del("key")
got := kv.get("key")
if got != nil {
t.Errorf("got %v, want %v", got, nil)
}
}
})

t.Run(fmt.Sprintf("set/get/clear %v", tt.in), func(t *testing.T) {
kv := newKV()
kv.set("key", tt.in)
got := kv.get("key")
if diff := cmp.Diff(got, tt.in); diff != "" {
t.Error(diff)
}

{
kv.clear()
got := kv.get("key")
if got != nil {
t.Errorf("got %v, want %v", got, nil)
}
}
})
}
}

func TestRunNWithKV(t *testing.T) {
ctx := context.Background()
book := "testdata/book/kv.yml"
Expand Down
3 changes: 1 addition & 2 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

const (
loopSectionKey = "loop"
storeRootKeyLoopCountIndex = "i"
loopSectionKey = "loop"
)

var (
Expand Down
13 changes: 7 additions & 6 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/k1LoW/maskedio"
"github.com/k1LoW/runn/exprtrace"
"github.com/k1LoW/runn/internal/deprecation"
"github.com/k1LoW/runn/internal/kv"
"github.com/k1LoW/stopw"
"github.com/k1LoW/waitmap"
"github.com/ryo-yamaoka/otchkiss"
Expand Down Expand Up @@ -1434,7 +1435,7 @@ type operatorN struct {
opts []Option
results []*runNResult
runNIndex atomic.Int64 // runNIndex holds the runN execution index (starting from 0). It is incremented each time runN is executed
kv *kv
kv *kv.KV
dbg *dbg
mu sync.Mutex
}
Expand Down Expand Up @@ -1472,7 +1473,7 @@ func Load(pathp string, opts ...Option) (*operatorN, error) {
concmax: 1,
opts: opts,
runNIndex: atomic.Int64{},
kv: newKV(),
kv: kv.New(),
dbg: newDBG(bk.attach),
}
opn.runNIndex.Store(-1) // Set index to -1 ( no runN )
Expand Down Expand Up @@ -1739,22 +1740,22 @@ func (opn *operatorN) CollectCoverage(ctx context.Context) (*Coverage, error) {

// SetKV sets a key-value pair to runn.kv.
func (opn *operatorN) SetKV(k string, v any) {
opn.kv.set(k, v)
opn.kv.Set(k, v)
}

// GetKV gets a value from runn.kv.
func (opn *operatorN) GetKV(k string) any { //nostyle:getters
return opn.kv.get(k)
return opn.kv.Get(k)
}

// DelKV deletes a key-value pair from runn.kv.
func (opn *operatorN) DelKV(k string) {
opn.kv.del(k)
opn.kv.Del(k)
}

// ClearKV clears all key-value pairs in runn.kv.
func (opn *operatorN) Clear() {
opn.kv.clear()
opn.kv.Clear()
}

func (opn *operatorN) runN(ctx context.Context) (*runNResult, error) {
Expand Down
40 changes: 19 additions & 21 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ import (

"github.com/goccy/go-json"
"github.com/k1LoW/maskedio"
"github.com/k1LoW/runn/internal/kv"
"github.com/mattn/go-isatty"
"github.com/samber/lo"
"github.com/spf13/cast"
)

const (
storeRootKeyVars = "vars"
storeRootKeySteps = "steps"
storeRootKeyParent = "parent"
storeRootKeyIncluded = "included"
storeRootKeyCurrent = "current"
storeRootKeyPrevious = "previous"
storeRootKeyEnv = "env"
storeRootKeyCookie = "cookies"
storeRootKeyNodes = "nodes"
storeRootKeyParams = "params"
storeRootKeyRunn = "runn"
storeRootKeyNeeds = "needs"
storeRootKeyVars = "vars"
storeRootKeySteps = "steps"
storeRootKeyParent = "parent"
storeRootKeyIncluded = "included"
storeRootKeyCurrent = "current"
storeRootKeyPrevious = "previous"
storeRootKeyEnv = "env"
storeRootKeyCookie = "cookies"
storeRootKeyNodes = "nodes"
storeRootKeyParams = "params"
storeRootKeyRunn = "runn"
storeRootKeyNeeds = "needs"
storeRootKeyLoopCountIndex = "i"
)

const (
Expand Down Expand Up @@ -74,7 +76,7 @@ type store struct {
useMap bool // Use map syntax in `steps:`.
loopIndex *int
cookies map[string]map[string]*http.Cookie
kv *kv
kv *kv.KV
runNIndex int

// for secret masking
Expand Down Expand Up @@ -265,13 +267,11 @@ func (s *store) toMap() map[string]any {
runnm := map[string]any{}
// runn.kv
if s.kv != nil {
s.kv.mu.Lock()
kv := map[string]any{}
for k, v := range s.kv.m {
kv[k] = v
for _, k := range s.kv.Keys() {
kv[k] = s.kv.Get(k)
}
runnm[storeRunnKeyKV] = kv
s.kv.mu.Unlock()
}
// runn.i
runnm[storeRunnKeyRunNIndex] = s.runNIndex
Expand Down Expand Up @@ -348,13 +348,11 @@ func (s *store) toMapForDbg() map[string]any {
runnm := map[string]any{}
// runn.kv
if s.kv != nil {
s.kv.mu.Lock()
kv := map[string]any{}
for k, v := range s.kv.m {
kv[k] = v
for _, k := range s.kv.Keys() {
kv[k] = s.kv.Get(k)
}
runnm[storeRunnKeyKV] = kv
s.kv.mu.Unlock()
}
// runn.i
runnm[storeRunnKeyRunNIndex] = s.runNIndex
Expand Down

0 comments on commit 91a321f

Please sign in to comment.