github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/utils/maps/maps.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package maps
    16  
    17  // ConvertToSlice Use the equal sign to link key and value looks like key1=value1,key2=value2.
    18  func ConvertToSlice(m map[string]string) []string {
    19  	result := []string{}
    20  	for k, v := range m {
    21  		result = append(result, k+"="+v)
    22  	}
    23  	return result
    24  }
    25  
    26  // Merge :get all elements, only insert key which is not in dst form src.
    27  func Merge(dst, src map[string]string) map[string]string {
    28  	if len(dst) == 0 {
    29  		return Copy(src)
    30  	}
    31  	for srcEnvKey, srcEnvValue := range src {
    32  		if _, ok := dst[srcEnvKey]; ok {
    33  			continue
    34  		}
    35  		dst[srcEnvKey] = srcEnvValue
    36  	}
    37  	return dst
    38  }
    39  
    40  func Copy(origin map[string]string) map[string]string {
    41  	if origin == nil {
    42  		return nil
    43  	}
    44  	ret := make(map[string]string, len(origin))
    45  	for k, v := range origin {
    46  		ret[k] = v
    47  	}
    48  
    49  	return ret
    50  }