Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: utils.Options usage #5

Closed
wants to merge 16 commits into from
56 changes: 29 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ import (
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"
fileserver "github.com/kitex-contrib/config-file/server"
"github.com/kitex-contrib/config-file/utils"
)

var _ api.Echo = &EchoImpl{}

const (
filepath = "kitex_server.json"
key = "ServiceName"
serviceName = "ServiceName"
)

var _ api.Echo = &EchoImpl{}
Expand Down Expand Up @@ -83,17 +75,10 @@ func main() {
}
defer fw.StopWatching()

// customed by user
params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
}

svr := echo.NewServer(
new(EchoImpl),
kitexserver.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}),
kitexserver.WithSuite(fileserver.NewSuite(key, fw, opts)), // add watcher
kitexserver.WithSuite(fileserver.NewSuite(key, fw)), // add watcher
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
Expand Down Expand Up @@ -123,7 +108,6 @@ import (
fileclient "github.com/kitex-contrib/config-file/client"
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"
"github.com/kitex-contrib/config-file/utils"
)

const (
Expand Down Expand Up @@ -163,17 +147,10 @@ func main() {
os.Exit(1)
}()

// customed by user
params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
}

client, err := echo.NewClient(
serviceName,
kitexclient.WithHostPorts("0.0.0.0:8888"),
kitexclient.WithSuite(fileclient.NewSuite(serviceName, key, fw, opts)),
kitexclient.WithSuite(fileclient.NewSuite(serviceName, key, fw)),
)
if err != nil {
log.Fatal(err)
Expand All @@ -192,9 +169,34 @@ func main() {
}
```

#### File Configuration Format
#### File Configuration

##### Custom Parser
The configuration format supports `json` and `yaml` by default, which can be implemented by
The `ConfigParser` interface implements a custom parser and passes in the custom function through `utils.Options` during `NewSuite`

The configuration format supports `json` and `yaml` by default. You can implement a custom parser by implementing the `ConfigParser` interface, and pass in the custom parser and custom parameters through `utils.Options` when `NewSuite`
Interface definition:
```
type ConfigParser interface {
Decode(kind ConfigType, data []byte, config interface{}) error
}
```

Sample code:
Custom function
```
func customParser(parser parser.ConfigParser) utils.Options {
return func(o *utils.Option) {
o.Parser = parser
}
}

func customConfigParams(param *parser.ConfigParam) utils.Options {
return func(o *utils.Option) {
o.Params = param
}
}
```

#### Governance Policy
> The service name is `ServiceName` and the client name is `ClientName`.
Expand Down
57 changes: 30 additions & 27 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ import (
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"
fileserver "github.com/kitex-contrib/config-file/server"
"github.com/kitex-contrib/config-file/utils"
)

var _ api.Echo = &EchoImpl{}

const (
filepath = "kitex_server.json"
key = "ServiceName"
serviceName = "ServiceName"
)

var _ api.Echo = &EchoImpl{}
Expand Down Expand Up @@ -83,17 +75,10 @@ func main() {
}
defer fw.StopWatching()

// customed by user
params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
}

svr := echo.NewServer(
new(EchoImpl),
kitexserver.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}),
kitexserver.WithSuite(fileserver.NewSuite(key, fw, opts)), // add watcher
kitexserver.WithSuite(fileserver.NewSuite(key, fw)), // add watcher
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
Expand Down Expand Up @@ -123,7 +108,6 @@ import (
fileclient "github.com/kitex-contrib/config-file/client"
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"
"github.com/kitex-contrib/config-file/utils"
)

const (
Expand Down Expand Up @@ -163,17 +147,10 @@ func main() {
os.Exit(1)
}()

// customed by user
params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
}

client, err := echo.NewClient(
serviceName,
kitexclient.WithHostPorts("0.0.0.0:8888"),
kitexclient.WithSuite(fileclient.NewSuite(serviceName, key, fw, opts)),
kitexclient.WithSuite(fileclient.NewSuite(serviceName, key, fw)),
)
if err != nil {
log.Fatal(err)
Expand All @@ -192,8 +169,34 @@ func main() {
}
```

#### File配置格式
配置格式默认支持`json`和`yaml`,可以通过实现`ConfigParser`接口实现自定义解析器,并在`NewSuite`的时候通过`utils.Options`传入自定义解析器以及自定义参数
#### File配置

##### 自定义解析器
配置格式默认支持`json`和`yaml`,可以通过实现
`ConfigParser`接口实现自定义解析器,并在`NewSuite`的时候通过`utils.Options`传入自定义函数

接口定义:
```
type ConfigParser interface {
Decode(kind ConfigType, data []byte, config interface{}) error
}
```

示例代码:
自定义函数
```
func customParser(parser parser.ConfigParser) utils.Options {
return func(o *utils.Option) {
o.Parser = parser
}
}

func customConfigParams(param *parser.ConfigParam) utils.Options {
return func(o *utils.Option) {
o.Params = param
}
}
```

#### 治理策略
> 服务名称为 ServiceName,客户端名称为 ClientName
Expand Down
22 changes: 12 additions & 10 deletions client/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package client

import (
"fmt"

kitexclient "github.com/cloudwego/kitex/client"
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/monitor"
Expand All @@ -28,19 +30,19 @@ type FileConfigClientSuite struct {
}

// NewSuite service is the destination service.
func NewSuite(service, key string, watcher filewatcher.FileWatcher, opts *utils.Options) *FileConfigClientSuite {
cm, err := monitor.NewConfigMonitor(key, watcher)
if err != nil {
panic(err)
func NewSuite(service, key string, watcher filewatcher.FileWatcher, opts ...utils.Options) *FileConfigClientSuite {
parserOption := &utils.Option{
Parser: parser.DefaultConfigParser(),
Params: parser.DefaultConfigParam(),
}

// use custom parser
if opts.CustomParser != nil {
cm.SetParser(opts.CustomParser)
for _, option := range opts {
option(parserOption)
}

if opts.CustomParams != nil {
cm.SetParams(opts.CustomParams)
fmt.Printf("config parser: %v %T\n", parserOption.Params, parserOption.Parser)
cm, err := monitor.NewConfigMonitor(key, watcher, parserOption)
if err != nil {
panic(err)
}

return &FileConfigClientSuite{
Expand Down
10 changes: 1 addition & 9 deletions example/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
fileclient "github.com/kitex-contrib/config-file/client"
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"
"github.com/kitex-contrib/config-file/utils"
)

const (
Expand Down Expand Up @@ -69,17 +68,10 @@ func main() {
os.Exit(1)
}()

// customed by user
params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
}

client, err := echo.NewClient(
serviceName,
kitexclient.WithHostPorts("0.0.0.0:8888"),
kitexclient.WithSuite(fileclient.NewSuite(serviceName, key, fw, opts)),
kitexclient.WithSuite(fileclient.NewSuite(serviceName, key, fw)),
)
if err != nil {
log.Fatal(err)
Expand Down
10 changes: 1 addition & 9 deletions example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/kitex-contrib/config-file/filewatcher"
"github.com/kitex-contrib/config-file/parser"
fileserver "github.com/kitex-contrib/config-file/server"
"github.com/kitex-contrib/config-file/utils"
)

var _ api.Echo = &EchoImpl{}
Expand Down Expand Up @@ -70,17 +69,10 @@ func main() {
}
defer fw.StopWatching()

// customed by user
params := &parser.ConfigParam{}
opts := &utils.Options{
CustomParser: &MyParser{},
CustomParams: params,
}

svr := echo.NewServer(
new(EchoImpl),
kitexserver.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}),
kitexserver.WithSuite(fileserver.NewSuite(key, fw, opts)), // add watcher
kitexserver.WithSuite(fileserver.NewSuite(key, fw)), // add watcher
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
Expand Down
19 changes: 4 additions & 15 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"
"github.com/kitex-contrib/config-file/utils"
)

type ConfigMonitor interface {
Expand All @@ -32,8 +33,6 @@ type ConfigMonitor interface {
WatcherID() int64
Stop()
SetManager(manager parser.ConfigManager)
SetParser(parser parser.ConfigParser)
SetParams(params *parser.ConfigParam)
ConfigParse(kind parser.ConfigType, data []byte, config interface{}) error
RegisterCallback(callback func()) int64
DeregisterCallback(uniqueID int64)
Expand All @@ -54,7 +53,7 @@ type configMonitor struct {
}

// NewConfigMonitor init a monitor for the config file
func NewConfigMonitor(key string, watcher filewatcher.FileWatcher) (ConfigMonitor, error) {
func NewConfigMonitor(key string, watcher filewatcher.FileWatcher, opt *utils.Option) (ConfigMonitor, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这不是可选参数

if key == "" {
return nil, errors.New("empty config key")
}
Expand All @@ -66,8 +65,8 @@ func NewConfigMonitor(key string, watcher filewatcher.FileWatcher) (ConfigMonito
fileWatcher: watcher,
key: key,
callbacks: make(map[int64]func(), 0),
parser: parser.DefaultConfigParser(),
params: parser.DefaultConfigParam(),
parser: opt.Parser,
params: opt.Params,
}, nil
}

Expand Down Expand Up @@ -111,16 +110,6 @@ func (c *configMonitor) Stop() {
// SetManager set the manager for the config file
func (c *configMonitor) SetManager(manager parser.ConfigManager) { c.manager = manager }

// SetParser set the parser for the config file
func (c *configMonitor) SetParser(parser parser.ConfigParser) {
c.parser = parser
}

// SetParams set the params for the config file, such as file type
func (c *configMonitor) SetParams(params *parser.ConfigParam) {
c.params = params
}

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