From 3eccbd12e414d343659d7ce514c234d7327cba7a Mon Sep 17 00:00:00 2001 From: d_infinite <463394797@qq.com> Date: Fri, 10 Jun 2022 17:43:51 +0800 Subject: [PATCH] chore(veinmind-common): update conf service func field --- plugins/go/veinmind-sensitive/go.mod | 3 +- .../go/service/conf/default_test.go | 17 +++++++++-- veinmind-common/go/service/conf/service.go | 28 +++++++++++-------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/plugins/go/veinmind-sensitive/go.mod b/plugins/go/veinmind-sensitive/go.mod index 2349d3e9..87043d3b 100644 --- a/plugins/go/veinmind-sensitive/go.mod +++ b/plugins/go/veinmind-sensitive/go.mod @@ -2,12 +2,11 @@ module github.com/chaitin/veinmind-tools/plugins/go/veinmind-sensitive go 1.18 -replace github.com/chaitin/veinmind-tools/veinmind-common/go v1.0.0 => ../../../veinmind-common/go require ( github.com/BurntSushi/toml v0.3.1 github.com/chaitin/libveinmind v1.1.0 - github.com/chaitin/veinmind-tools/veinmind-common/go v1.0.0 + github.com/chaitin/veinmind-tools/veinmind-common/go v0.0.0-20220613062319-ac5c6e55bfe4 github.com/stretchr/testify v1.7.0 ) diff --git a/veinmind-common/go/service/conf/default_test.go b/veinmind-common/go/service/conf/default_test.go index d0909a86..1a920a4e 100644 --- a/veinmind-common/go/service/conf/default_test.go +++ b/veinmind-common/go/service/conf/default_test.go @@ -5,10 +5,23 @@ import ( "testing" ) -func TestDefaultClient(t *testing.T) { +func TestDefaultClient(t *testing.T) { c := DefaultConfClient() - _, err := c.Pull(Sensitive) + _, err := c.Pull(Sensitive) assert.Error(t, err) } +func TestNewConfService(t *testing.T) { + s, err := NewConfService() + if err != nil { + t.Error(err) + } + s.Store(Sensitive, []byte{0x01}) + b, err := s.Pull(Sensitive) + if err != nil { + t.Error(err) + } + + assert.Equal(t, b, []byte{0x01}) +} diff --git a/veinmind-common/go/service/conf/service.go b/veinmind-common/go/service/conf/service.go index 15a0a52d..91631c81 100644 --- a/veinmind-common/go/service/conf/service.go +++ b/veinmind-common/go/service/conf/service.go @@ -10,29 +10,35 @@ import ( const Namespace = "github.com/chaitin/veinmind-tools/veinmind-common/go/service/conf" type ConfService struct { - store map[string][]byte + store map[PluginConfNS][]byte } type confClient struct { - ctx context.Context - group *errgroup.Group - Pull func(ns PluginConfNS) ([]byte, error) + ctx context.Context + group *errgroup.Group + Pull func(ns PluginConfNS) ([]byte, error) } -func (c *ConfService) Pull(pluginName string) ([]byte, error){ - if b, ok := c.store[pluginName]; ok { +func NewConfService() (*ConfService, error) { + c := new(ConfService) + c.store = make(map[PluginConfNS][]byte) + return c, nil +} + +func (c *ConfService) Pull(ns PluginConfNS) ([]byte, error) { + if b, ok := c.store[ns]; ok { return b, nil - }else{ + } else { return nil, errors.New("conf: plugin conf doesn't exist") } } -func (c *ConfService) Store(pluginName string, b []byte) error { - c.store[pluginName] = b +func (c *ConfService) Store(ns PluginConfNS, b []byte) error { + c.store[ns] = b return nil } -func (c *ConfService) Add(registry *service.Registry) { - registry.Define(Namespace, struct {}{}) +func (c *ConfService) Add(registry *service.Registry) { + registry.Define(Namespace, struct{}{}) registry.AddService(Namespace, "pull", c.Pull) }