github.com/sacloud/libsacloud/v2@v2.32.3/internal/dsl/functions.go (about) 1 // Copyright 2016-2022 The Libsacloud Authors 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 dsl 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/huandu/xstrings" 22 ) 23 24 func uniqStrings(ss []string) []string { 25 seen := make(map[string]struct{}, len(ss)) 26 i := 0 27 for _, v := range ss { 28 if _, ok := seen[v]; ok { 29 continue 30 } 31 seen[v] = struct{}{} 32 ss[i] = v 33 i++ 34 } 35 return ss[:i] 36 } 37 38 func wrapByDoubleQuote(targets ...string) []string { 39 var ss []string 40 for _, s := range targets { 41 ss = append(ss, fmt.Sprintf(`"%s"`, s)) 42 } 43 return ss 44 } 45 46 func toSnakeCaseName(name string) string { 47 return strings.Replace(normalizeResourceName(xstrings.ToSnakeCase(name)), "-", "_", -1) 48 } 49 50 func toLower(name string) string { 51 return strings.Replace(normalizeResourceName(xstrings.ToSnakeCase(name)), "_", "", -1) 52 } 53 54 var normalizationWords = map[string]string{ 55 "Ip": "IP", 56 "i_pv_": "ipv", 57 "i-pv-": "ipv", 58 "Cpu": "CPU", 59 "Ssd": "SSD", 60 "Hdd": "HDD", 61 } 62 63 func normalizeResourceName(name string) string { 64 n := name 65 for k, v := range normalizationWords { 66 if strings.Contains(name, k) { 67 n = strings.Replace(name, k, v, -1) 68 break 69 } 70 } 71 return n 72 } 73 74 func firstRuneToLower(name string) string { 75 return xstrings.FirstRuneToLower(name) 76 }