You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/08/24 15:11:41 UTC

[GitHub] [dubbo-go] Mulavar commented on a change in pull request #1372: Ftr: Add config loader hooks for 1.5

Mulavar commented on a change in pull request #1372:
URL: https://github.com/apache/dubbo-go/pull/1372#discussion_r694947646



##########
File path: config/config_loader_hook.go
##########
@@ -0,0 +1,367 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package config
+
+import (
+	"fmt"
+	"reflect"
+)
+
+import (
+	"github.com/apache/dubbo-go/common/constant"
+)
+
+const (
+	hookParams = constant.HookParams
+)
+
+type LoaderHook interface {
+	emit() bool
+	// if you want to use emitWithParams, you must add HookParams field and make emit return false
+	emitWithParams(params interface{}) bool
+}
+
+func getLoaderHookType(t LoaderHook) reflect.Type {
+	hookType := reflect.TypeOf(t)
+	if reflect.Ptr == hookType.Kind() {
+		hookType = hookType.Elem()
+	}
+	return hookType
+}
+
+func getLoaderHookTypeString(t LoaderHook) string {
+	return getLoaderHookType(t).String()
+}
+
+func getLoaderHookInfo(v reflect.Value) reflect.Value {
+	if reflect.Ptr == v.Kind() {
+		v = v.Elem()
+	}
+	return v
+}
+
+func getLoaderHookParams(t LoaderHook) interface{} {
+	hookType := getLoaderHookType(t)
+	_, existed := hookType.FieldByName(hookParams)
+	if !existed {
+		panic(fmt.Sprintf("Hook field [%s] not existed for %s", hookParams, hookType.Name()))
+	}
+	hookParams := getLoaderHookInfo(reflect.ValueOf(t)).FieldByName(hookParams)
+	return getLoaderHookInfo(hookParams).Interface()
+}
+
+func AddLoaderHooks(hooks ...LoaderHook) {
+	if len(hooks) > 0 {
+		if loaderHooks == nil {
+			loaderHooks = map[string][]LoaderHook{}
+		}
+		for _, hook := range hooks {
+			hookType := getLoaderHookTypeString(hook)
+			var hooks []LoaderHook
+			if hs, ok := loaderHooks[hookType]; ok {
+				hooks = hs
+			}
+			if hooks == nil {
+				hooks = []LoaderHook{}
+			}
+			loaderHooks[hookType] = append(hooks, hook)
+		}
+	}
+}
+
+func RemoveLoaderHooks(hooks ...LoaderHook) {
+	if loaderHooks != nil && len(loaderHooks) > 0 {
+		for _, rmHook := range hooks {
+			hookType := getLoaderHookTypeString(rmHook)
+			if hooks, ok := loaderHooks[hookType]; ok {
+				for index, targetHook := range hooks {
+					if rmHook == targetHook {
+						newHooks := append(hooks[:index], hooks[index+1:]...)
+						if len(newHooks) == 0 {
+							delete(loaderHooks, hookType)
+						} else {
+							loaderHooks[hookType] = newHooks
+						}
+						break
+					}

Review comment:
       1. 嵌套过深,建议将提前终止的分支代码放前面,比如最外层的if
   if loaderHooks == nil || len(loaderHooks) <= 0 {
       return
   }
   for _,  rmHook := range hooks { 
   // ...
   这样阅读上清晰一些
   2. 好像缩进有点问题,目前dubbogo都是4格。




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org