style(contrib/config/apollo): optimized code

pull/2627/head
demoManito 2 years ago
parent fcd3b18e83
commit 2028948127
  1. 38
      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" properties = "properties"
) )
var formats map[string]struct{} var formats = map[string]struct{}{
yaml: {},
yml: {},
json: {},
properties: {},
}
// Option is apollo option // Option is apollo option
type Option func(*options) type Option func(*options)
@ -130,8 +135,10 @@ func NewSource(opts ...Option) config.Source {
} }
func format(ns string) string { func format(ns string) string {
arr := strings.Split(ns, ".") var (
suffix := arr[len(arr)-1] arr = strings.Split(ns, ".")
suffix = arr[len(arr)-1]
)
if len(arr) <= 1 || suffix == properties { if len(arr) <= 1 || suffix == properties {
return json return json
} }
@ -144,8 +151,10 @@ func format(ns string) string {
} }
func (e *apollo) load() []*config.KeyValue { func (e *apollo) load() []*config.KeyValue {
kvs := make([]*config.KeyValue, 0) var (
namespaces := strings.Split(e.opt.namespace, ",") kvs = make([]*config.KeyValue, 0)
namespaces = strings.Split(e.opt.namespace, ",")
)
for _, ns := range namespaces { for _, ns := range namespaces {
if !e.opt.originConfig { 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 // 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" // map level. such as: app.name = "application" => map[app][name] = "application"
func resolve(key string, value interface{}, target map[string]interface{}) { func resolve(key string, value interface{}, target map[string]interface{}) {
// expand key "aaa.bbb" into map[aaa]map[bbb]interface{} var (
keys := strings.Split(key, ".") // expand key "aaa.bbb" into map[aaa]map[bbb]interface{}
last := len(keys) - 1 keys = strings.Split(key, ".")
cursor := target last = len(keys) - 1
cursor = target
)
for i, k := range keys { for i, k := range keys {
if i == last { if i == last {
@ -275,12 +286,3 @@ func genKey(ns, sub string) string {
return ns + "." + sub 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) 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 { func NewAliyunLog(options ...Option) Logger {
opts := defaultOptions() opts := defaultOptions()
for _, o := range options { 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 { func PathPrefix(prefix string) ServerOption {
return func(s *Server) { return func(s *Server) {
s.router = s.router.PathPrefix(prefix).Subrouter() s.router = s.router.PathPrefix(prefix).Subrouter()

Loading…
Cancel
Save