Skip to content

Commit 0317bf1

Browse files
committed
feat: optionloader for stream call client
1 parent c8d6d60 commit 0317bf1

File tree

10 files changed

+163
-8
lines changed

10 files changed

+163
-8
lines changed

config/client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ type IdleConfig struct {
3131
MaxIdleTimeout TimeInterval `yaml:"max_idle_timeout"`
3232
}
3333

34-
type Tag struct {
35-
Key string `yaml:"key"`
36-
Value string `yaml:"value"`
37-
}
38-
3934
type GRPCClientKeepaliveParams struct {
4035
Time TimeInterval `yaml:"time"`
4136
Timeout TimeInterval `yaml:"timeout"`

config/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ func (t *TimeInterval) Transform() time.Duration {
3434
}
3535
return time.Duration(t.Value) * unit
3636
}
37+
38+
type Tag struct {
39+
Key string `yaml:"key"`
40+
Value string `yaml:"value"`
41+
}

config/streamcall.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
package config
2+
3+
type StreamCallConfig struct {
4+
// from callopt
5+
HostPort string `yaml:"host_port"`
6+
URL string `yaml:"url"`
7+
ConnectTimeout *TimeInterval `yaml:"connect_timeout"`
8+
Tag *Tag `yaml:"tag"`
9+
GRPCCompressor string `yaml:"grpc_compressor"`
10+
}

configloader/yaml/callopt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"os"
88
)
99

10-
func LoadCallopConfig(filePath string) (*config.CalloptConfig, error) {
10+
func LoadCalloptConfig(filePath string) (*config.CalloptConfig, error) {
1111

1212
if _, err := os.Stat(filePath); os.IsNotExist(err) {
1313
return nil, fmt.Errorf("file does not exist: %s", filePath)

configloader/yaml/streamcall.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
11
package yaml
2+
3+
import (
4+
"fmt"
5+
"github.com/kitex-contrib/optionloader/config"
6+
"gopkg.in/yaml.v3"
7+
"os"
8+
)
9+
10+
func LoadStreamCallConfig(filePath string) (*config.StreamCallConfig, error) {
11+
12+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
13+
return nil, fmt.Errorf("file does not exist: %s", filePath)
14+
}
15+
data, err := os.ReadFile(filePath)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
var cfg config.StreamCallConfig
21+
22+
err = yaml.Unmarshal(data, &cfg)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
return &cfg, nil
28+
}

examples/yaml/client/callopt/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func main() {
1010
filePath := "./examples/yaml/client/callopt/config.yaml"
11-
cfg, err := yaml.LoadCallopConfig(filePath)
11+
cfg, err := yaml.LoadCalloptConfig(filePath)
1212
if err != nil {
1313
panic(err)
1414
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
host_port: "localhost:8080"
2+
url: "/your-service-name/your-method"
3+
connect_timeout:
4+
unit: "s"
5+
value: 3
6+
tag:
7+
key: "your-tag-key"
8+
value: "your-tag-value"
9+
grpc_compressor: "gzip"
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
package client
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/kitex-contrib/optionloader/configloader/yaml"
6+
"github.com/kitex-contrib/optionloader/optionloader/client/callopt/streamcall"
7+
)
8+
9+
func main() {
10+
filePath := "./examples/yaml/client/callopt/streamcall/config.yaml"
11+
cfg, err := yaml.LoadStreamCallConfig(filePath)
12+
if err != nil {
13+
panic(err)
14+
}
15+
loader := streamcall.NewOptionLoader()
16+
options, err := loader.Load(cfg)
17+
if err != nil {
18+
panic(err)
19+
}
20+
fmt.Println(len(options))
21+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
11
package streamcall
2+
3+
import (
4+
"fmt"
5+
"github.com/cloudwego/kitex/client/callopt/streamcall"
6+
"github.com/kitex-contrib/optionloader/config"
7+
translator "github.com/kitex-contrib/optionloader/translator/client/callopt/streamcall"
8+
)
9+
10+
type Translator func(config *config.StreamCallConfig) streamcall.Option
11+
12+
type OptionLoader interface {
13+
// RegisterTranslator registers a translator function.
14+
RegisterTranslator(translator Translator)
15+
// Load loads the server options from config and custom registered option translators.
16+
Load(config *config.StreamCallConfig) ([]streamcall.Option, error)
17+
}
18+
19+
type DefaultOptionLoader struct {
20+
translators []Translator
21+
}
22+
23+
func NewOptionLoader() OptionLoader {
24+
return &DefaultOptionLoader{
25+
translators: make([]Translator, 0),
26+
}
27+
}
28+
29+
// RegisterTranslator registers a translator function.
30+
// If the translator function has been registered, both will be registered,
31+
// and the translator functions will be called in the order of registration.
32+
func (loader *DefaultOptionLoader) RegisterTranslator(translator Translator) {
33+
loader.translators = append(loader.translators, translator)
34+
}
35+
36+
func (loader *DefaultOptionLoader) Load(config *config.StreamCallConfig) ([]streamcall.Option, error) {
37+
if config == nil {
38+
return nil, fmt.Errorf("client config not set")
39+
}
40+
var translatorsList []Translator
41+
42+
if config.HostPort != "" {
43+
translatorsList = append(translatorsList, translator.HostPortTranslator)
44+
}
45+
if config.URL != "" {
46+
translatorsList = append(translatorsList, translator.URLTranslator)
47+
}
48+
if config.ConnectTimeout != nil {
49+
translatorsList = append(translatorsList, translator.ConnectTimeoutTranslator)
50+
}
51+
if config.Tag != nil {
52+
translatorsList = append(translatorsList, translator.TagTranslator)
53+
}
54+
if config.GRPCCompressor != "" {
55+
translatorsList = append(translatorsList, translator.GRPCCompressorTranslator)
56+
}
57+
58+
// Add the custom registered option translators behind the default translators.
59+
loader.translators = append(translatorsList, loader.translators...)
60+
61+
var options []streamcall.Option
62+
for _, trans := range loader.translators {
63+
options = append(options, trans(config))
64+
}
65+
return options, nil
66+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
package streamcall
2+
3+
import (
4+
"github.com/cloudwego/kitex/client/callopt/streamcall"
5+
"github.com/kitex-contrib/optionloader/config"
6+
)
7+
8+
func HostPortTranslator(config *config.StreamCallConfig) streamcall.Option {
9+
return streamcall.WithHostPort(config.HostPort)
10+
}
11+
12+
func URLTranslator(config *config.StreamCallConfig) streamcall.Option {
13+
return streamcall.WithURL(config.URL)
14+
}
15+
16+
func ConnectTimeoutTranslator(config *config.StreamCallConfig) streamcall.Option {
17+
return streamcall.WithConnectTimeout(config.ConnectTimeout.Transform())
18+
}
19+
20+
func TagTranslator(config *config.StreamCallConfig) streamcall.Option {
21+
return streamcall.WithTag(config.Tag.Key, config.Tag.Value)
22+
}
23+
24+
func GRPCCompressorTranslator(config *config.StreamCallConfig) streamcall.Option {
25+
return streamcall.WithGRPCCompressor(config.GRPCCompressor)
26+
}

0 commit comments

Comments
 (0)