Skip to content
Open
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
8 changes: 4 additions & 4 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ func resolver(input map[string]any, mapper func(name string) string, toType bool

func mapper(input map[string]any) func(name string) string {
mapper := func(name string) string {
args := strings.SplitN(strings.TrimSpace(name), ":", 2) //nolint:mnd
if v, has := readValue(input, args[0]); has {
key, value, cut := strings.Cut(strings.TrimSpace(name), ":")
if v, has := readValue(input, key); has {
s, _ := v.String()
return s
} else if len(args) > 1 { // default value
return args[1]
} else if cut { // default value
return value
}
return ""
}
Expand Down
23 changes: 13 additions & 10 deletions contrib/config/apollo/apollo.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ func NewSource(opts ...Option) config.Source {
}

func format(ns string) string {
arr := strings.Split(ns, ".")
suffix := arr[len(arr)-1]
if len(arr) <= 1 || suffix == properties {
lastDot := strings.LastIndexByte(ns, '.')
if lastDot == -1 || lastDot == len(ns)-1 {
return json
}
suffix := ns[lastDot+1:]
if suffix == properties {
return json
}
if _, ok := formats[suffix]; !ok {
Expand Down Expand Up @@ -258,19 +261,19 @@ func resolve(key string, value any, target map[string]any) {
// genKey got the key of config.KeyValue pair.
// eg: namespace.ext with subKey got namespace.subKey
func genKey(ns, sub string) string {
arr := strings.Split(ns, ".")
if len(arr) == 1 {
if ns == "" {
return sub
}
if ns == "" {
return sub
}

lastDot := strings.LastIndexByte(ns, '.')
if lastDot == -1 || lastDot == len(ns)-1 {
return ns + "." + sub
}

suffix := arr[len(arr)-1]
suffix := ns[lastDot+1:]
_, ok := formats[suffix]
if ok {
return strings.Join(arr[:len(arr)-1], ".") + "." + sub
return ns[:lastDot] + "." + sub
}

return ns + "." + sub
Expand Down
6 changes: 3 additions & 3 deletions contrib/registry/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func defaultResolver(_ context.Context, entries []*api.ServiceEntry) []*registry
for _, entry := range entries {
var version string
for _, tag := range entry.Service.Tags {
ss := strings.SplitN(tag, "=", 2)
if len(ss) == 2 && ss[0] == "version" {
version = ss[1]
k, v, cut := strings.Cut(tag, "=")
if cut && k == "version" {
version = v
}
}
endpoints := make([]string, 0)
Expand Down
2 changes: 1 addition & 1 deletion contrib/registry/kubernetes/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func getServiceInstanceFromPod(pod *corev1.Pod) (*registry.ServiceInstance, erro
protocol := protocolMap.GetProtocol(port)
if protocol == "" {
if cp.Name != "" {
protocol = strings.Split(cp.Name, "-")[0]
protocol, _, _ = strings.Cut(cp.Name, "-")
} else {
protocol = string(cp.Protocol)
}
Expand Down
5 changes: 2 additions & 3 deletions middleware/auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ func Server(keyFunc jwt.Keyfunc, opts ...Option) middleware.Middleware {
if keyFunc == nil {
return nil, ErrMissingKeyFunc
}
auths := strings.SplitN(header.RequestHeader().Get(authorizationKey), " ", 2)
if len(auths) != 2 || !strings.EqualFold(auths[0], bearerWord) {
bearer, jwtToken, cut := strings.Cut(header.RequestHeader().Get(authorizationKey), " ")
if !cut || !strings.EqualFold(bearer, bearerWord) {
return nil, ErrMissingJwtToken
}
jwtToken := auths[1]
var (
tokenInfo *jwt.Token
err error
Expand Down
8 changes: 4 additions & 4 deletions middleware/tracing/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ func setServerSpan(ctx context.Context, span trace.Span, m any) {
// on a gRPC's FullMethod.
func parseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
name := strings.TrimLeft(fullMethod, "/")
parts := strings.SplitN(name, "/", 2)
if len(parts) != 2 { //nolint:mnd
service, method, cut := strings.Cut(name, "/")
if !cut {
// Invalid format, does not follow `/package.service/method`.
return name, []attribute.KeyValue{attribute.Key("rpc.operation").String(fullMethod)}
}

var attrs []attribute.KeyValue
if service := parts[0]; service != "" {
if service != "" {
attrs = append(attrs, semconv.RPCServiceKey.String(service))
}
if method := parts[1]; method != "" {
if method != "" {
attrs = append(attrs, semconv.RPCMethodKey.String(method))
}
return name, attrs
Expand Down
Loading