Skip to content

Commit

Permalink
fix: utils.Option usage (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Ricky-chen1 <[email protected]>
  • Loading branch information
Ricky-chen1 and Ricky-chen1 authored Feb 13, 2024
1 parent 7f8e9c5 commit 52b94c4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 102 deletions.
54 changes: 25 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,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{}

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

var _ api.Echo = &EchoImpl{}
Expand Down Expand Up @@ -83,17 +74,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,14 +107,13 @@ 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 (
filepath = "kitex_client.json"
key = "ClientName/ServiceName"
serviceName = "ServiceName"
clientName = "ClientName"
clientName = "echo"
)

// customed by user
Expand Down Expand Up @@ -163,17 +146,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 +168,29 @@ func main() {
}
```

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

##### Custom Parser

The configuration format supports `json` and `yaml` by default. You can implement a custom parser by implementing the `ConfigParser` interface and pass in a custom function in `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:
```go
// ConfigParser the parser for config file.
type ConfigParser interface {
Decode(kind ConfigType, data []byte, config interface{}) error
}
```

Custom function example:
```go
func withParser(o *utils.Options) {
o.Parser = &MyParser{}
o.Params = &parser.ConfigParam{
Type: parser.YAML,
}
}
```

#### Governance Policy
> The service name is `ServiceName` and the client name is `ClientName`.
Expand Down
55 changes: 26 additions & 29 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,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{}

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

var _ api.Echo = &EchoImpl{}
Expand Down Expand Up @@ -83,17 +74,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,14 +107,13 @@ 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 (
filepath = "kitex_client.json"
key = "ClientName/ServiceName"
serviceName = "ServiceName"
clientName = "ClientName"
clientName = "echo"
)

// customed by user
Expand Down Expand Up @@ -163,17 +146,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 +168,29 @@ func main() {
}
```

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

##### 自定义解析器

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

接口定义:
```go
// ConfigParser the parser for config file.
type ConfigParser interface {
Decode(kind ConfigType, data []byte, config interface{}) error
}
```

自定义函数示例:
```go
func withParser(o *utils.Options) {
o.Parser = &MyParser{}
o.Params = &parser.ConfigParam{
Type: parser.YAML,
}
}
```

#### 治理策略
> 服务名称为 ServiceName,客户端名称为 ClientName
Expand Down
13 changes: 2 additions & 11 deletions client/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,12 @@ 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)
func NewSuite(service, key string, watcher filewatcher.FileWatcher, opts ...utils.Option) *FileConfigClientSuite {
cm, err := monitor.NewConfigMonitor(key, watcher, opts...)
if err != nil {
panic(err)
}

// use custom parser
if opts.CustomParser != nil {
cm.SetParser(opts.CustomParser)
}

if opts.CustomParams != nil {
cm.SetParams(opts.CustomParams)
}

return &FileConfigClientSuite{
watcher: cm,
service: service,
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
16 changes: 13 additions & 3 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 Down Expand Up @@ -54,20 +55,29 @@ 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, opts ...utils.Option) (ConfigMonitor, error) {
if key == "" {
return nil, errors.New("empty config key")
}
if watcher == nil {
return nil, errors.New("filewatcher is nil")
}

option := &utils.Options{
Parser: parser.DefaultConfigParser(),
Params: parser.DefaultConfigParam(),
}

for _, opt := range opts {
opt(option)
}

return &configMonitor{
fileWatcher: watcher,
key: key,
callbacks: make(map[int64]func(), 0),
parser: parser.DefaultConfigParser(),
params: parser.DefaultConfigParam(),
parser: option.Parser,
params: option.Params,
}, nil
}

Expand Down
12 changes: 2 additions & 10 deletions server/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,12 @@ type FileConfigServerSuite struct {
}

// NewSuite service is the destination service.
func NewSuite(key string, watcher filewatcher.FileWatcher, opts *utils.Options) *FileConfigServerSuite {
cm, err := monitor.NewConfigMonitor(key, watcher)
func NewSuite(key string, watcher filewatcher.FileWatcher, opts ...utils.Option) *FileConfigServerSuite {
cm, err := monitor.NewConfigMonitor(key, watcher, opts...)
if err != nil {
panic(err)
}

// use custom parser
if opts.CustomParser != nil {
cm.SetParser(opts.CustomParser)
}
if opts.CustomParams != nil {
cm.SetParams(opts.CustomParams)
}

return &FileConfigServerSuite{
watcher: cm,
}
Expand Down
6 changes: 4 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
)

type Options struct {
CustomParser parser.ConfigParser
CustomParams *parser.ConfigParam
Parser parser.ConfigParser
Params *parser.ConfigParam
}

type Option func(o *Options)

// PathExists check whether the file or directory exists
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
Expand Down

0 comments on commit 52b94c4

Please sign in to comment.