style(contrib/config/apollo): optimized code

pull/2627/head
demoManito 2 years ago
parent fcd3b18e83
commit 2028948127
  1. 36
      contrib/config/apollo/apollo.go
  2. 2
      contrib/log/aliyun/aliyun.go
  3. 2
      transport/http/server.go

@ -25,7 +25,12 @@ const (
properties = "properties"
)
var formats map[string]struct{}
var formats = map[string]struct{}{
yaml: {},
yml: {},
json: {},
properties: {},
}
// Option is apollo option
type Option func(*options)
@ -130,8 +135,10 @@ func NewSource(opts ...Option) config.Source {
}
func format(ns string) string {
arr := strings.Split(ns, ".")
suffix := arr[len(arr)-1]
var (
arr = strings.Split(ns, ".")
suffix = arr[len(arr)-1]
)
if len(arr) <= 1 || suffix == properties {
return json
}
@ -144,8 +151,10 @@ func format(ns string) string {
}
func (e *apollo) load() []*config.KeyValue {
kvs := make([]*config.KeyValue, 0)
namespaces := strings.Split(e.opt.namespace, ",")
var (
kvs = make([]*config.KeyValue, 0)
namespaces = strings.Split(e.opt.namespace, ",")
)
for _, ns := range namespaces {
if !e.opt.originConfig {
@ -225,10 +234,12 @@ func (e *apollo) Watch() (config.Watcher, error) {
// resolve convert kv pair into one map[string]interface{} by split key into different
// map level. such as: app.name = "application" => map[app][name] = "application"
func resolve(key string, value interface{}, target map[string]interface{}) {
var (
// expand key "aaa.bbb" into map[aaa]map[bbb]interface{}
keys := strings.Split(key, ".")
last := len(keys) - 1
cursor := target
keys = strings.Split(key, ".")
last = len(keys) - 1
cursor = target
)
for i, k := range keys {
if i == last {
@ -275,12 +286,3 @@ func genKey(ns, sub string) string {
return ns + "." + sub
}
func init() {
formats = make(map[string]struct{})
formats[yaml] = struct{}{}
formats[yml] = struct{}{}
formats[json] = struct{}{}
formats[properties] = struct{}{}
}

@ -102,7 +102,7 @@ func (a *aliyunLog) Log(level log.Level, keyvals ...interface{}) error {
return a.producer.SendLog(a.opts.project, a.opts.logstore, "", "", logInst)
}
// NewAliyunLog new a aliyun logger with options.
// NewAliyunLog new an aliyun logger with options.
func NewAliyunLog(options ...Option) Logger {
opts := defaultOptions()
for _, o := range options {

@ -127,7 +127,7 @@ func Listener(lis net.Listener) ServerOption {
}
}
// PathPrefix with mux's PathPrefix, router will replaced by a subrouter that start with prefix.
// PathPrefix with mux's PathPrefix, router will be replaced by a subroutine that start with prefix.
func PathPrefix(prefix string) ServerOption {
return func(s *Server) {
s.router = s.router.PathPrefix(prefix).Subrouter()

Loading…
Cancel
Save