From 20289481276476ac4127d005ae0c4533bf6a2d5f Mon Sep 17 00:00:00 2001 From: demoManito <1430482733@qq.com> Date: Tue, 17 Jan 2023 18:11:14 +0800 Subject: [PATCH] style(contrib/config/apollo): optimized code --- contrib/config/apollo/apollo.go | 38 +++++++++++++++++---------------- contrib/log/aliyun/aliyun.go | 2 +- transport/http/server.go | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/contrib/config/apollo/apollo.go b/contrib/config/apollo/apollo.go index 1734fdb02..3d312b1b6 100644 --- a/contrib/config/apollo/apollo.go +++ b/contrib/config/apollo/apollo.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{}) { - // expand key "aaa.bbb" into map[aaa]map[bbb]interface{} - keys := strings.Split(key, ".") - last := len(keys) - 1 - cursor := target + var ( + // expand key "aaa.bbb" into map[aaa]map[bbb]interface{} + 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{}{} -} diff --git a/contrib/log/aliyun/aliyun.go b/contrib/log/aliyun/aliyun.go index 836960e06..8d1ef0672 100644 --- a/contrib/log/aliyun/aliyun.go +++ b/contrib/log/aliyun/aliyun.go @@ -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 { diff --git a/transport/http/server.go b/transport/http/server.go index 8bece9afe..343afaea2 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -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()