Skip to content

Commit

Permalink
update: test nats
Browse files Browse the repository at this point in the history
  • Loading branch information
kainonly committed Jul 8, 2023
1 parent 31783f9 commit 36bb784
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 50 deletions.
14 changes: 7 additions & 7 deletions values/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ var (
PwdStrategy: 1,
PwdTTL: time.Hour * 24 * 365,
}
SECRET = []string{
"TencentSecretKey",
"LarkAppSecret",
"LarkEncryptKey",
"LarkVerificationToken",
"EmailPassword",
"OpenapiSecret",
SECRET = map[string]bool{
"TencentSecretKey": true,
"LarkAppSecret": true,
"LarkEncryptKey": true,
"LarkVerificationToken": true,
"EmailPassword": true,
"OpenapiSecret": true,
}
)

Expand Down
18 changes: 12 additions & 6 deletions values/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ import (
var (
js nats.JetStreamContext
keyvalue nats.KeyValue
cipherx *cipher.Cipher
service *values.Service
engine *route.Engine
)

type M = map[string]interface{}

func TestMain(m *testing.M) {
if err := UseNats("dev"); err != nil {
var err error
if err = UseNats("dev"); err != nil {
log.Fatalln(err)
}
v := values.DEFAULT
cipherx, err := cipher.New("vGglcAlIavhcvZGra7JuZDzp3DZPQ6iU")
if err != nil {
if cipherx, err = cipher.New("vGglcAlIavhcvZGra7JuZDzp3DZPQ6iU"); err != nil {
log.Fatalln(err)
}
v := values.DEFAULT
service = &values.Service{
KeyValue: keyvalue,
Cipher: cipherx,
Expand Down Expand Up @@ -92,8 +93,13 @@ func UseNats(namespace string) (err error) {
if js, err = nc.JetStream(nats.PublishAsyncMaxPending(256)); err != nil {
return
}
js.DeleteKeyValue(namespace)
if object, err = js.CreateObjectStore(&nats.ObjectStoreConfig{Bucket: namespace}); err != nil {
//js.DeleteKeyValue(namespace)
//if keyvalue, err = js.CreateKeyValue(&nats.KeyValueConfig{Bucket: namespace}); err != nil {
// return
//}
if keyvalue, err = js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: namespace,
}); err != nil {
return
}
return
Expand Down
52 changes: 21 additions & 31 deletions values/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/thoas/go-funk"
"github.com/vmihailenco/msgpack/v5"
"github.com/weplanx/go-wpx/cipher"
"reflect"
)

type Service struct {
Expand All @@ -14,22 +15,24 @@ type Service struct {
Values *Values
}

func (x *Service) Fetch() (data map[string]interface{}, err error) {
func (x *Service) Fetch(values map[string]interface{}) (err error) {
var entry nats.KeyValueEntry
if entry, err = x.KeyValue.Get("values"); err != nil {
if !errors.Is(err, nats.ErrKeyNotFound) {
return
}
if err = x.Update(x.Values); err != nil {
return
if errors.Is(err, nats.ErrKeyNotFound) {
v := reflect.ValueOf(*x.Values)
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
values[typ.Field(i).Name] = v.Field(i).Interface()
}
return x.Update(values)
}
return
}
var b []byte
if b, err = x.Cipher.Decode(string(entry.Value())); err != nil {
return
}
if err = msgpack.Unmarshal(b, &x.Values); err != nil {
if err = msgpack.Unmarshal(b, &values); err != nil {
return
}
return
Expand Down Expand Up @@ -69,35 +72,30 @@ func (x *Service) Fetch() (data map[string]interface{}, err error) {
//}

func (x *Service) Set(data map[string]interface{}) (err error) {
if err = x.Fetch(); err != nil {
var values map[string]interface{}
if err = x.Fetch(values); err != nil {
return
}
for path, value := range data {
if err = funk.Set(&x.Values, value, path); err != nil {
return
}
for key, value := range data {
values[key] = value
}
return x.Update(values)
}

func (x *Service) Get(keys []string) (values map[string]interface{}, err error) {
var entry nats.KeyValueEntry
if entry, err = x.KeyValue.Get("values"); err != nil {
return
}
var b []byte
if b, err = x.Cipher.Decode(string(entry.Value())); err != nil {
if err = x.Fetch(values); err != nil {
return
}
if err = msgpack.Unmarshal(b, &values); err != nil {
return
contains := make(map[string]bool)
for _, v := range keys {
contains[v] = true
}
for k, v := range values {
if len(keys) != 0 && !funk.Contains(keys, k) {
if len(keys) != 0 && !contains[k] {
delete(values, k)
continue
}
if funk.Contains(SECRET, k) {
if SECRET[k] {
if funk.IsEmpty(v) {
values[k] = "-"
} else {
Expand All @@ -109,16 +107,8 @@ func (x *Service) Get(keys []string) (values map[string]interface{}, err error)
}

func (x *Service) Remove(key string) (err error) {
var entry nats.KeyValueEntry
if entry, err = x.KeyValue.Get("values"); err != nil {
return
}
var b []byte
if b, err = x.Cipher.Decode(string(entry.Value())); err != nil {
return
}
var values map[string]interface{}
if err = msgpack.Unmarshal(b, &values); err != nil {
if err = x.Fetch(values); err != nil {
return
}
delete(values, key)
Expand Down
36 changes: 30 additions & 6 deletions values/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@ package values_test
import (
"github.com/stretchr/testify/assert"
"github.com/vmihailenco/msgpack/v5"
"github.com/weplanx/go-wpx/values"
"testing"
)

func TestService_Set(t *testing.T) {
t.Log("ok")
b, _ := msgpack.Marshal(M{
"token": "WasY11AEfAVXZ68c",
})
err := service.Set(b)
func TestService_Fetch(t *testing.T) {
//data := make(map[string]interface{})
//err := service.Fetch(data)
//assert.NoError(t, err)
//t.Log(data)
b, err := msgpack.Marshal(values.DEFAULT)
ciphertext, err := cipherx.Encode(b)
assert.NoError(t, err)
_, err = keyvalue.PutString("values", ciphertext)
assert.NoError(t, err)
}

func TestService_Fetch2(t *testing.T) {
entry, err := keyvalue.Get("values")
assert.NoError(t, err)
b, err := cipherx.Decode(string(entry.Value()))
assert.NoError(t, err)
var data values.Values
err = msgpack.Unmarshal(b, &data)
assert.NoError(t, err)
t.Log(data)
}

//func TestService_Set(t *testing.T) {
// t.Log("ok")
// b, _ := msgpack.Marshal(M{
// "token": "WasY11AEfAVXZ68c",
// })
// err := service.Set(b)
// assert.NoError(t, err)
//}

0 comments on commit 36bb784

Please sign in to comment.