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/20 03:31:11 UTC

[GitHub] [dubbo-go] xavier-niu commented on a change in pull request #1370: Ftr: Add config loader hooks for 3.0

xavier-niu commented on a change in pull request #1370:
URL: https://github.com/apache/dubbo-go/pull/1370#discussion_r692633485



##########
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 (
+	"dubbo.apache.org/dubbo-go/v3/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)
+		}

Review comment:
       It seems that `hooks` is a useless variable in this case.
   
   ```go
   if _, ok := loaderHooks[hookType]; !ok {
      loaderHooks[hookType] = make([]LoaderHook)
   }
   loaderHooks[hookType] = append(loaderHooks[hookType], hook)
   ```




-- 
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