Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions client/egrpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
)

// metricUnaryClientInterceptor returns grpc unary request metrics collector interceptor
func (c *Container) metricUnaryClientInterceptor() func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
func (c *Container) metricUnaryClientInterceptor() func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
beg := time.Now()
emetric.ClientStartedCounter.Inc(emetric.TypeGRPCUnary, c.name, method, cc.Target())
err := invoker(ctx, method, req, reply, cc, opts...)
Expand All @@ -53,7 +53,7 @@

// debugUnaryClientInterceptor returns grpc unary request request and response details interceptor
func (c *Container) debugUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
var p peer.Peer
beg := time.Now()
err := invoker(ctx, method, req, reply, cc, append(opts, grpc.Peer(&p))...)
Expand All @@ -74,7 +74,7 @@
egrpcinteceptor.RPCSystemGRPC,
egrpcinteceptor.GRPCKindUnary,
}
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
md = metadata.New(nil)
Expand Down Expand Up @@ -104,7 +104,7 @@

// defaultUnaryClientInterceptor returns interceptor inject app name
func (c *Container) defaultUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
// https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md
ctx = metadata.AppendToOutgoingContext(ctx, "app", eapp.Name())
// if c.config.EnableCPUUsage {
Expand Down Expand Up @@ -142,7 +142,7 @@
sentMessageID int
}

func (w *clientStream) RecvMsg(m interface{}) error {
func (w *clientStream) RecvMsg(m any) error {
err := w.ClientStream.RecvMsg(m)

if err == nil && !w.desc.ServerStreams {
Expand All @@ -159,7 +159,7 @@
return err
}

func (w *clientStream) SendMsg(m interface{}) error {
func (w *clientStream) SendMsg(m any) error {
err := w.ClientStream.SendMsg(m)

w.sentMessageID++
Expand Down Expand Up @@ -294,7 +294,7 @@

// timeoutUnaryClientInterceptor settings timeout
func (c *Container) timeoutUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
// 若无自定义超时设置,默认设置超时
_, ok := ctx.Deadline()
if !ok {
Expand All @@ -308,7 +308,7 @@

// loggerUnaryClientInterceptor returns log interceptor for logging
func (c *Container) loggerUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, res interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
return func(ctx context.Context, method string, req, res any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
var beg = time.Now()
var fields []elog.Field
var event = "normal"
Expand Down Expand Up @@ -397,11 +397,11 @@

// customHeader 自定义header头
func customHeader(egoLogExtraKeys []string) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, res interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, res any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
for _, key := range egoLogExtraKeys {
if value := tools.GrpcHeaderValue(ctx, key); value != "" {
if ctx.Value(key) != nil {
ctx = context.WithValue(ctx, key, value)

Check failure on line 404 in client/egrpc/interceptor.go

View workflow job for this annotation

GitHub Actions / lint

SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
}
// ctx = transport.WithValue(ctx, key, value)
}
Expand Down
4 changes: 2 additions & 2 deletions client/egrpc/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_customHeader(t *testing.T) {

cc := new(grpc.ClientConn)
err := interceptor(ctx, "/foo", nil, nil, cc,
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
info := tools.GrpcHeaderValue(ctx, "X-Ego-Uid")
assert.Equal(t, "9527", info)
return nil
Expand All @@ -44,7 +44,7 @@ func TestMetric(t *testing.T) {
interceptor := cmp.metricUnaryClientInterceptor()
cc := new(grpc.ClientConn)
err := interceptor(context.Background(), "/foo", nil, nil, cc,
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
return nil
})
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion client/egrpc/resolver/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {
// stats
func stats() (stats map[string][]resolver.Address) {
stats = make(map[string][]resolver.Address)
instances.Range(func(key, val interface{}) bool {
instances.Range(func(key, val any) bool {
name := key.(string)
addresses := val.([]resolver.Address)
stats[name] = addresses
Expand Down
10 changes: 5 additions & 5 deletions core/econf/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type DataSource interface {
}

// Unmarshaller ...
type Unmarshaller = func([]byte, interface{}) error
type Unmarshaller = func([]byte, any) error

var defaultConfiguration = New()

Expand All @@ -54,7 +54,7 @@ func LoadFromReader(r io.Reader, unmarshaller Unmarshaller) error {
}

// Apply ...
func Apply(conf map[string]interface{}) error {
func Apply(conf map[string]any) error {
return defaultConfiguration.apply(conf)
}

Expand All @@ -64,7 +64,7 @@ func Reset() {
}

// Traverse ...
func Traverse(sep string) map[string]interface{} {
func Traverse(sep string) map[string]any {
return defaultConfiguration.traverse(sep)
}

Expand All @@ -79,11 +79,11 @@ func Debug(sep string) {
}

// Get returns an interface. For a specific value use one of the Get____ methods.
func Get(key string) interface{} {
func Get(key string) any {
return defaultConfiguration.Get(key)
}

// Set sets config value for key
func Set(key string, val interface{}) {
func Set(key string, val any) {
_ = defaultConfiguration.Set(key, val)
}
50 changes: 25 additions & 25 deletions core/econf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const PackageName = "core.econf"
// Configuration ...
type Configuration struct {
mu sync.RWMutex
override map[string]interface{}
override map[string]any
keyDelim string
rawConfig []byte
keyMap *sync.Map
Expand All @@ -38,7 +38,7 @@ const (
// New constructs a new Configuration with provider.
func New() *Configuration {
return &Configuration{
override: make(map[string]interface{}),
override: make(map[string]any),
keyDelim: defaultKeyDelim,
keyMap: &sync.Map{},
onChanges: make([]func(*Configuration), 0),
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *Configuration) LoadFromDataSource(ds DataSource, unmarshaller Unmarshal
// Load ...
func (c *Configuration) Load(content []byte, unmarshal Unmarshaller) error {
c.rawConfig = content
configuration := make(map[string]interface{})
configuration := make(map[string]any)
if err := unmarshal(content, &configuration); err != nil {
return err
}
Expand All @@ -130,11 +130,11 @@ func (c *Configuration) LoadFromReader(reader io.Reader, unmarshaller Unmarshall
return c.Load(content, unmarshaller)
}

func (c *Configuration) apply(conf map[string]interface{}) error {
func (c *Configuration) apply(conf map[string]any) error {
c.mu.Lock()
defer c.mu.Unlock()

var changes = make(map[string]interface{})
var changes = make(map[string]any)

xmap.MergeStringMap(c.override, conf)
for k, v := range c.traverse(c.keyDelim) {
Expand All @@ -152,7 +152,7 @@ func (c *Configuration) apply(conf map[string]interface{}) error {
return nil
}

func (c *Configuration) notifyChanges(changes map[string]interface{}) {
func (c *Configuration) notifyChanges(changes map[string]any) {
var changedWatchPrefixMap = map[string]struct{}{}

for watchPrefix := range c.watchers {
Expand All @@ -173,7 +173,7 @@ func (c *Configuration) notifyChanges(changes map[string]interface{}) {
}

// Set ...
func (c *Configuration) Set(key string, val interface{}) error {
func (c *Configuration) Set(key string, val any) error {
paths := strings.Split(key, c.keyDelim)
lastKey := paths[len(paths)-1]
m := deepSearch(c.override, paths[:len(paths)-1])
Expand All @@ -182,18 +182,18 @@ func (c *Configuration) Set(key string, val interface{}) error {
// c.keyMap.Store(key, val)
}

func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
func deepSearch(m map[string]any, path []string) map[string]any {
for _, k := range path {
m2, ok := m[k]
if !ok {
m3 := make(map[string]interface{})
m3 := make(map[string]any)
m[k] = m3
m = m3
continue
}
m3, ok := m2.(map[string]interface{})
m3, ok := m2.(map[string]any)
if !ok {
m3 = make(map[string]interface{})
m3 = make(map[string]any)
m[k] = m3
}
m = m3
Expand All @@ -202,7 +202,7 @@ func deepSearch(m map[string]interface{}, path []string) map[string]interface{}
}

// Get returns the value associated with the key
func (c *Configuration) Get(key string) interface{} {
func (c *Configuration) Get(key string) any {
return c.find(key)
}

Expand Down Expand Up @@ -287,22 +287,22 @@ func (c *Configuration) GetStringSlice(key string) []string {
}

// GetSlice returns the value associated with the key as a slice of strings with default defaultConfiguration.
func GetSlice(key string) []interface{} {
func GetSlice(key string) []any {
return defaultConfiguration.GetSlice(key)
}

// GetSlice returns the value associated with the key as a slice of strings.
func (c *Configuration) GetSlice(key string) []interface{} {
func (c *Configuration) GetSlice(key string) []any {
return cast.ToSlice(c.Get(key))
}

// GetStringMap returns the value associated with the key as a map of interfaces with default defaultConfiguration.
func GetStringMap(key string) map[string]interface{} {
func GetStringMap(key string) map[string]any {
return defaultConfiguration.GetStringMap(key)
}

// GetStringMap returns the value associated with the key as a map of interfaces.
func (c *Configuration) GetStringMap(key string) map[string]interface{} {
func (c *Configuration) GetStringMap(key string) map[string]any {
return cast.ToStringMap(c.Get(key))
}

Expand All @@ -317,7 +317,7 @@ func (c *Configuration) GetStringMapString(key string) map[string]string {
}

// GetSliceStringMap returns the value associated with the slice of maps.
func (c *Configuration) GetSliceStringMap(key string) []map[string]interface{} {
func (c *Configuration) GetSliceStringMap(key string) []map[string]any {
return tools.ToSliceStringMap(c.Get(key))
}

Expand All @@ -332,12 +332,12 @@ func (c *Configuration) GetStringMapStringSlice(key string) map[string][]string
}

// UnmarshalWithExpect unmarshal key, returns expect if failed
func UnmarshalWithExpect(key string, expect interface{}) interface{} {
func UnmarshalWithExpect(key string, expect any) any {
return defaultConfiguration.UnmarshalWithExpect(key, expect)
}

// UnmarshalWithExpect unmarshal key, returns expect if failed
func (c *Configuration) UnmarshalWithExpect(key string, expect interface{}) interface{} {
func (c *Configuration) UnmarshalWithExpect(key string, expect any) any {
err := c.UnmarshalKey(key, expect)
if err != nil {
return expect
Expand All @@ -346,15 +346,15 @@ func (c *Configuration) UnmarshalWithExpect(key string, expect interface{}) inte
}

// UnmarshalKey takes a single key and unmarshal it into a Struct with default defaultConfiguration.
func UnmarshalKey(key string, rawVal interface{}, opts ...Option) error {
func UnmarshalKey(key string, rawVal any, opts ...Option) error {
return defaultConfiguration.UnmarshalKey(key, rawVal, opts...)
}

// ErrInvalidKey ...
var ErrInvalidKey = errors.New("invalid key, maybe not exist in config")

// UnmarshalKey takes a single key and unmarshal it into a Struct.
func (c *Configuration) UnmarshalKey(key string, rawVal interface{}, opts ...Option) error {
func (c *Configuration) UnmarshalKey(key string, rawVal any, opts ...Option) error {
var options = defaultContainer
for _, opt := range opts {
opt(&options)
Expand Down Expand Up @@ -385,7 +385,7 @@ func (c *Configuration) UnmarshalKey(key string, rawVal interface{}, opts ...Opt
return decoder.Decode(value)
}

func (c *Configuration) find(key string) interface{} {
func (c *Configuration) find(key string) any {
dd, ok := c.keyMap.Load(key)
if ok {
return dd
Expand All @@ -400,7 +400,7 @@ func (c *Configuration) find(key string) interface{} {
return dd
}

func lookup(prefix string, target map[string]interface{}, data map[string]interface{}, sep string) {
func lookup(prefix string, target map[string]any, data map[string]any, sep string) {
for k, v := range target {
pp := fmt.Sprintf("%s%s%s", prefix, sep, k)
if prefix == "" {
Expand All @@ -414,8 +414,8 @@ func lookup(prefix string, target map[string]interface{}, data map[string]interf
}
}

func (c *Configuration) traverse(sep string) map[string]interface{} {
data := make(map[string]interface{})
func (c *Configuration) traverse(sep string) map[string]any {
data := make(map[string]any)
lookup("", c.override, data, sep)
return data
}
Expand Down
4 changes: 2 additions & 2 deletions core/econf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSetKeyDelim(t *testing.T) {
func TestSub(t *testing.T) {
c := &Configuration{
keyDelim: defaultKeyDelim,
override: map[string]interface{}{
override: map[string]any{
"key1": "hello",
"key2": "world",
},
Expand All @@ -30,7 +30,7 @@ func TestSub(t *testing.T) {
out := c.Sub("")
in := &Configuration{
keyDelim: defaultKeyDelim,
override: map[string]interface{}{},
override: map[string]any{},
keyMap: &sync.Map{},
}
assert.Equal(t, in, out)
Expand Down
Loading
Loading