Skip to content

Commit

Permalink
feat: optionloader for stream call client
Browse files Browse the repository at this point in the history
  • Loading branch information
BaiZe1998 committed Aug 3, 2024
1 parent c8d6d60 commit c8d5def
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
9 changes: 9 additions & 0 deletions config/streamcall.go
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
package config

type StreamCallConfig struct {
// from callopt
HostPort string `yaml:"host_port"`
URL string `yaml:"url"`
ConnectTimeout *TimeInterval `yaml:"connect_timeout"`
Tag *Tag `yaml:"tag"`
GRPCCompressor string `yaml:"grpc_compressor"`
}
27 changes: 27 additions & 0 deletions configloader/yaml/streamcall.go
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
package yaml

import (
"fmt"
"github.com/kitex-contrib/optionloader/config"
"gopkg.in/yaml.v3"
"os"
)

func LoadStreamCallConfig(filePath string) (*config.StreamCallConfig, error) {

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

var cfg config.StreamCallConfig

err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}
9 changes: 9 additions & 0 deletions examples/yaml/client/callopt/streamcall/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
host_port: "localhost:8080"
url: "/your-service-name/your-method"
connect_timeout:
unit: "s"
value: 3
tag:
key: "your-tag-key"
value: "your-tag-value"
grpc_compressor: "gzip"
22 changes: 21 additions & 1 deletion examples/yaml/client/callopt/streamcall/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
package client
package main

import (
"fmt"
"github.com/kitex-contrib/optionloader/configloader/yaml"
"github.com/kitex-contrib/optionloader/optionloader/client/callopt/streamcall"
)

func main() {
filePath := "./examples/yaml/client/callopt/streamcall/config.yaml"
cfg, err := yaml.LoadStreamCallConfig(filePath)
if err != nil {
panic(err)
}
loader := streamcall.NewOptionLoader()
options, err := loader.Load(cfg)
if err != nil {
panic(err)
}
fmt.Println(len(options))
}
65 changes: 65 additions & 0 deletions optionloader/client/callopt/streamcall/optionloader.go
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
package streamcall

import (
"fmt"
"github.com/cloudwego/kitex/client/callopt/streamcall"
"github.com/kitex-contrib/optionloader/config"
translator "github.com/kitex-contrib/optionloader/translator/client/callopt/streamcall"
)

type Translator func(config *config.StreamCallConfig) streamcall.Option

type OptionLoader interface {
// RegisterTranslator registers a translator function.
RegisterTranslator(translator Translator)
// Load loads the server options from config and custom registered option translators.
Load(config *config.StreamCallConfig) ([]streamcall.Option, error)
}

type DefaultOptionLoader struct {
translators []Translator
}

func NewOptionLoader() OptionLoader {
return &DefaultOptionLoader{
translators: make([]Translator, 0),
}
}

// RegisterTranslator registers a translator function.
// If the translator function has been registered, both will be registered,
// and the translator functions will be called in the order of registration.
func (loader *DefaultOptionLoader) RegisterTranslator(translator Translator) {
loader.translators = append(loader.translators, translator)
}

func (loader *DefaultOptionLoader) Load(config *config.StreamCallConfig) ([]streamcall.Option, error) {
if config == nil {
return nil, fmt.Errorf("client config not set")
}
var translatorsList []Translator

if config.HostPort != "" {
translatorsList = append(translatorsList, translator.HostPortTranslator)
}
if config.URL != "" {
translatorsList = append(translatorsList, translator.URLTranslator)
}
if config.ConnectTimeout != nil {
translatorsList = append(translatorsList, translator.ConnectTimeoutTranslator)
}
if config.Tag != nil {
translatorsList = append(translatorsList, translator.TagTranslator)
}
if config.GRPCCompressor != "" {
translatorsList = append(translatorsList, translator.GRPCCompressorTranslator)
}

// Add the custom registered option translators behind the default translators.
loader.translators = append(translatorsList, loader.translators...)

var options []streamcall.Option
for _, trans := range loader.translators {
options = append(options, trans(config))
}
return options, nil
}
25 changes: 25 additions & 0 deletions translator/client/callopt/streamcall/translator.go
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
package streamcall

import (
"github.com/cloudwego/kitex/client/callopt/streamcall"
"github.com/kitex-contrib/optionloader/config"
)

func HostPortTranslator(config *config.StreamCallConfig) streamcall.Option {
return streamcall.WithHostPort(config.HostPort)
}

func URLTranslator(config *config.StreamCallConfig) streamcall.Option {
return streamcall.WithURL(config.URL)
}

func ConnectTimeoutTranslator(config *config.StreamCallConfig) streamcall.Option {
return streamcall.WithConnectTimeout(config.ConnectTimeout.Transform())
}

func TagTranslator(config *config.StreamCallConfig) streamcall.Option {
return streamcall.WithTag(config.Tag.Key, config.Tag.Value)
}

func GRPCCompressorTranslator(config *config.StreamCallConfig) streamcall.Option {
return streamcall.WithGRPCCompressor(config.GRPCCompressor)
}

0 comments on commit c8d5def

Please sign in to comment.