micro/options/options.go
Vasiliy Tolstov bc55c2fa6f
Some checks failed
lint / lint (pull_request) Failing after 1m29s
pr / test (pull_request) Failing after 2m39s
fix some options
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-15 07:55:26 +03:00

190 lines
4.1 KiB
Go

package options
import (
"context"
"crypto/tls"
"reflect"
"time"
"go.unistack.org/micro/v4/metadata"
rutil "go.unistack.org/micro/v4/util/reflect"
)
// Option func signature
type Option func(interface{}) error
// Set assign value to struct by its path
func Set(src interface{}, dst interface{}, path string) error {
return rutil.SetFieldByPath(src, dst, path)
}
// Get returns value from struct by its path
func Get(src interface{}, path string) (interface{}, error) {
return rutil.StructFieldByPath(src, path)
}
// Name set Name value
func Name(v ...string) Option {
return func(src interface{}) error {
return Set(src, v, ".Name")
}
}
// Address set Address value to single string or slice of strings
func Address(v ...string) Option {
return func(src interface{}) error {
return Set(src, v, ".Address")
}
}
// Broker set Broker value
func Broker(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Broker")
}
}
// Logger set Logger value
func Logger(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Logger")
}
}
// Meter set Meter value
func Meter(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Meter")
}
}
// Tracer set Tracer value
func Tracer(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Tracer")
}
}
// Store set Store value
func Store(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Store")
}
}
// Register set Register value
func Register(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Register")
}
}
// Router set Router value
func Router(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Router")
}
}
// Codec set Codec value
func Codec(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Codec")
}
}
// Client set Client value
func Client(v interface{}) Option {
return func(src interface{}) error {
return Set(src, v, ".Client")
}
}
// Codecs to be used to encode/decode requests for a given content type
func Codecs(ct string, v interface{}) Option {
return func(src interface{}) error {
cm, err := Get(src, ".Codecs")
if err != nil {
return err
} else if rutil.IsZero(cm) {
cm = reflect.MakeMap(reflect.TypeOf(cm)).Interface()
}
cv := reflect.ValueOf(cm)
cv.SetMapIndex(reflect.ValueOf(ct), reflect.ValueOf(v))
return Set(src, cv.Interface(), ".Codecs")
}
}
// Context set Context value
func Context(v context.Context) Option {
return func(src interface{}) error {
return Set(src, v, ".Context")
}
}
// TLSConfig set TLSConfig value
func TLSConfig(v *tls.Config) Option {
return func(src interface{}) error {
return Set(src, v, ".TLSConfig")
}
}
func ContextOption(k, v interface{}) Option {
return func(src interface{}) error {
ctx, err := Get(src, ".Context")
if err != nil {
return err
}
if ctx == nil {
ctx = context.Background()
}
err = Set(src, context.WithValue(ctx.(context.Context), k, v), ".Context")
return err
}
}
// ContentType pass ContentType for message data
func ContentType(ct string) Option {
return func(src interface{}) error {
return Set(src, ct, ".ContentType")
}
}
// Metadata pass additional metadata
func Metadata(md metadata.Metadata) Option {
return func(src interface{}) error {
return Set(src, metadata.Copy(md), ".Metadata")
}
}
// Namespace to use
func Namespace(ns string) Option {
return func(src interface{}) error {
return Set(src, ns, ".Namespace")
}
}
// Labels sets the labels
func Labels(ls ...interface{}) Option {
return func(src interface{}) error {
v, err := Get(src, ".Labels")
if err != nil {
return err
} else if rutil.IsZero(v) {
v = reflect.MakeSlice(reflect.TypeOf(v), 0, len(ls)).Interface()
}
cv := reflect.ValueOf(v)
for _, l := range ls {
cv = reflect.Append(cv, reflect.ValueOf(l))
}
return Set(src, cv.Interface(), ".Labels")
}
}
// Timeout pass timeout time.Duration
func Timeout(td time.Duration) Option {
return func(src interface{}) error {
return Set(src, td, ".Timeout")
}
}