blob: 5759ff34f6e5673c4a6de8af3b4e1a34a2e7af4c [file] [log] [blame]
/*
* 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"
"regexp"
"strings"
)
import (
"github.com/apache/dubbo-go/common/constant"
)
func mergeValue(str1, str2, def string) string {
if str1 == "" && str2 == "" {
return def
}
s1 := strings.Split(str1, ",")
s2 := strings.Split(str2, ",")
str := "," + strings.Join(append(s1, s2...), ",")
defKey := strings.Contains(str, ","+constant.DEFAULT_KEY)
if !defKey {
str = "," + constant.DEFAULT_KEY + str
}
str = strings.TrimPrefix(strings.Replace(str, ","+constant.DEFAULT_KEY, ","+def, -1), ",")
return removeMinus(strings.Split(str, ","))
}
func removeMinus(strArr []string) string {
if len(strArr) == 0 {
return ""
}
var normalStr string
var minusStrArr []string
for _, v := range strArr {
if strings.HasPrefix(v, "-") {
minusStrArr = append(minusStrArr, v[1:])
} else {
normalStr += fmt.Sprintf(",%s", v)
}
}
normalStr = strings.Trim(normalStr, ",")
for _, v := range minusStrArr {
normalStr = strings.Replace(normalStr, v, "", 1)
}
reg := regexp.MustCompile("[,]+")
normalStr = reg.ReplaceAllString(strings.Trim(normalStr, ","), ",")
return normalStr
}