add state key as the prefix

This commit is contained in:
c9s 2022-05-05 17:56:41 +08:00
parent 17928986b8
commit f3691489dd
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 5 additions and 7 deletions

View File

@ -134,7 +134,7 @@ func loadPersistenceFields(obj interface{}, id string, persistence service.Persi
return iterateFieldsByTag(obj, "persistence", func(tag string, field reflect.StructField, value reflect.Value) error { return iterateFieldsByTag(obj, "persistence", func(tag string, field reflect.StructField, value reflect.Value) error {
newValueInf := newTypeValueInterface(value.Type()) newValueInf := newTypeValueInterface(value.Type())
// inf := value.Interface() // inf := value.Interface()
store := persistence.NewStore(id, tag) store := persistence.NewStore("state", id, tag)
if err := store.Load(&newValueInf); err != nil { if err := store.Load(&newValueInf); err != nil {
if err == service.ErrPersistenceNotExists { if err == service.ErrPersistenceNotExists {
return nil return nil
@ -159,7 +159,7 @@ func storePersistenceFields(obj interface{}, id string, persistence service.Pers
return iterateFieldsByTag(obj, "persistence", func(tag string, ft reflect.StructField, fv reflect.Value) error { return iterateFieldsByTag(obj, "persistence", func(tag string, ft reflect.StructField, fv reflect.Value) error {
inf := fv.Interface() inf := fv.Interface()
store := persistence.NewStore(id, tag) store := persistence.NewStore("state", id, tag)
return store.Save(inf) return store.Save(inf)
}) })
} }

View File

@ -70,13 +70,13 @@ func Test_storePersistenceFields(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
var i int64 var i int64
store := ps.NewStore("test-struct", "integer") store := ps.NewStore("state", "test-struct", "integer")
err = store.Load(&i) err = store.Load(&i)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, int64(1), i) assert.Equal(t, int64(1), i)
var p *types.Position var p *types.Position
store = ps.NewStore("test-struct", "position") store = ps.NewStore("state", "test-struct", "position")
err = store.Load(&p) err = store.Load(&p)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, fixedpoint.NewFromFloat(10.0), p.Base) assert.Equal(t, fixedpoint.NewFromFloat(10.0), p.Base)

View File

@ -1,7 +1,6 @@
package service package service
import ( import (
"fmt"
"reflect" "reflect"
"strings" "strings"
) )
@ -38,7 +37,6 @@ func (store *MemoryStore) Load(val interface{}) error {
v := reflect.ValueOf(val) v := reflect.ValueOf(val)
if data, ok := store.memory.Slots[store.Key]; ok { if data, ok := store.memory.Slots[store.Key]; ok {
dataRV := reflect.ValueOf(data) dataRV := reflect.ValueOf(data)
fmt.Printf("load %s = %v\n", store.Key, dataRV)
v.Elem().Set(dataRV) v.Elem().Set(dataRV)
} else { } else {
return ErrPersistenceNotExists return ErrPersistenceNotExists

View File

@ -21,7 +21,7 @@ func TestMemoryService(t *testing.T) {
store := service.NewStore("test") store := service.NewStore("test")
i := 3 i := 3
err := store.Save(&i) err := store.Save(i)
assert.NoError(t, err) assert.NoError(t, err)