dubbo.apache.org/dubbo-go/v3@v3.1.1/config/config_utils.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package config 19 20 import ( 21 "fmt" 22 "regexp" 23 "strings" 24 ) 25 26 import ( 27 "github.com/go-playground/validator/v10" 28 29 "github.com/pkg/errors" 30 ) 31 32 import ( 33 "dubbo.apache.org/dubbo-go/v3/common/constant" 34 "dubbo.apache.org/dubbo-go/v3/common/extension" 35 ) 36 37 var validate *validator.Validate 38 39 func init() { 40 validate = validator.New() 41 } 42 43 func mergeValue(str1, str2, def string) string { 44 if str1 == "" && str2 == "" { 45 return def 46 } 47 s1 := strings.Split(str1, ",") 48 s2 := strings.Split(str2, ",") 49 str := "," + strings.Join(append(s1, s2...), ",") 50 defKey := strings.Contains(str, ","+constant.DefaultKey) 51 if !defKey { 52 str = "," + constant.DefaultKey + str 53 } 54 str = strings.TrimPrefix(strings.Replace(str, ","+constant.DefaultKey, ","+def, -1), ",") 55 return removeMinus(strings.Split(str, ",")) 56 } 57 58 func removeMinus(strArr []string) string { 59 if len(strArr) == 0 { 60 return "" 61 } 62 var normalStr string 63 var minusStrArr []string 64 for _, v := range strArr { 65 if strings.HasPrefix(v, "-") { 66 minusStrArr = append(minusStrArr, v[1:]) 67 } else { 68 normalStr += fmt.Sprintf(",%s", v) 69 } 70 } 71 normalStr = strings.Trim(normalStr, ",") 72 for _, v := range minusStrArr { 73 normalStr = strings.Replace(normalStr, v, "", 1) 74 } 75 reg := regexp.MustCompile("[,]+") 76 normalStr = reg.ReplaceAllString(strings.Trim(normalStr, ","), ",") 77 return normalStr 78 } 79 80 // removeDuplicateElement remove duplicate element 81 func removeDuplicateElement(items []string) []string { 82 result := make([]string, 0, len(items)) 83 temp := map[string]struct{}{} 84 for _, item := range items { 85 if _, ok := temp[item]; !ok && item != "" { 86 temp[item] = struct{}{} 87 result = append(result, item) 88 } 89 } 90 return result 91 } 92 93 // translateIds string "nacos,zk" => ["nacos","zk"] 94 func translateIds(registryIds []string) []string { 95 ids := make([]string, 0) 96 for _, id := range registryIds { 97 98 ids = append(ids, strings.Split(id, ",")...) 99 } 100 return removeDuplicateElement(ids) 101 } 102 103 func verify(s interface{}) error { 104 if err := validate.Struct(s); err != nil { 105 errs := err.(validator.ValidationErrors) 106 var slice []string 107 for _, msg := range errs { 108 slice = append(slice, msg.Error()) 109 } 110 return errors.New(strings.Join(slice, ",")) 111 } 112 return nil 113 } 114 115 // clientNameID unique identifier id for client 116 func clientNameID(config extension.Config, protocol, address string) string { 117 return strings.Join([]string{config.Prefix(), protocol, address}, "-") 118 } 119 120 func isValid(addr string) bool { 121 return addr != "" && addr != constant.NotAvailable 122 }