Skip to content

Commit

Permalink
fix: modify Options function
Browse files Browse the repository at this point in the history
  • Loading branch information
notacommonperson committed Jan 15, 2024
1 parent d1952cc commit ff3ae91
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/monitor"
"github.com/kitex-contrib/config-file/parser"

Check failure on line 22 in client/suite.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
)

type FileConfigClientSuite struct {
Expand All @@ -42,6 +43,7 @@ func NewSuite(service, key string, watcher filewatcher.FileWatcher) *FileConfigC
// Options return a list client.Option
func (s *FileConfigClientSuite) Options() []kitexclient.Option {
s.watcher.SetManager(&parser.ClientFileManager{})
s.watcher.SetParser(&parser.Parser{})

opts := make([]kitexclient.Option, 0, 7)
opts = append(opts, WithRetryPolicy(s.watcher)...)
Expand Down
7 changes: 7 additions & 0 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cloudwego/kitex/pkg/klog"
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"

Check failure on line 25 in monitor/monitor.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
)

type ConfigMonitor interface {
Expand All @@ -33,6 +34,7 @@ type ConfigMonitor interface {
Stop()
SetManager(manager parser.ConfigManager)
SetParser(parser parser.ConfigParser)
ConfigParse(kind parser.ConfigType, data []byte, config interface{}) error
RegisterCallback(callback func()) int64
DeregisterCallback(uniqueID int64)
}
Expand Down Expand Up @@ -112,6 +114,11 @@ func (c *configMonitor) SetParser(parser parser.ConfigParser) {
c.parser = parser
}

// ConfigParse call configMonitor.parser.Decode()
func (c *configMonitor) ConfigParse(kind parser.ConfigType, data []byte, config interface{}) error {
return c.parser.Decode(kind, data, config)
}

// RegisterCallback add callback function, it will be called when file changed, return key for deregister
func (c *configMonitor) RegisterCallback(callback func()) int64 {
c.lock.Lock()
Expand Down
9 changes: 9 additions & 0 deletions monitor/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func TestSetParser(t *testing.T) {
t.Errorf("NewConfigMonitor() error = %v", err)
}
cm.SetParser(&parser.Parser{})

// use json format test ConfigParse
kind := parser.JSON
jsonData := []byte(`{"key": "value"}`)
var config struct{}
err = cm.ConfigParse(kind, jsonData, &config)
if err != nil {
t.Errorf("ConfigParse() error = %v", err)
}
}

func TestRegisterCallback(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions server/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/monitor"
"github.com/kitex-contrib/config-file/parser"

Check failure on line 22 in server/suite.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
)

type FileConfigServerSuite struct {
Expand All @@ -40,6 +41,7 @@ func NewSuite(key string, watcher filewatcher.FileWatcher) *FileConfigServerSuit
// Options return a list client.Option
func (s *FileConfigServerSuite) Options() []kitexserver.Option {
s.watcher.SetManager(&parser.ServerFileManager{})
s.watcher.SetParser(&parser.Parser{})

opts := make([]kitexserver.Option, 0, 1)
opts = append(opts, WithLimiter(s.watcher))
Expand Down

0 comments on commit ff3ae91

Please sign in to comment.