github.com/sacloud/iaas-api-go@v1.12.0/internal/dsl/functions.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go 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.ReplaceAll(normalizeResourceName(xstrings.ToSnakeCase(name)), "-", "_")
    48  }
    49  
    50  func toLower(name string) string {
    51  	return strings.ReplaceAll(normalizeResourceName(xstrings.ToSnakeCase(name)), "_", "")
    52  }
    53  
    54  var normalizationWords = map[string]string{
    55  	"Ip":    "IP",
    56  	"i_pv":  "ipv",
    57  	"i_pv_": "ipv",
    58  	"i-pv-": "ipv",
    59  	"Cpu":   "CPU",
    60  	"Ssd":   "SSD",
    61  	"Hdd":   "HDD",
    62  }
    63  
    64  func normalizeResourceName(name string) string {
    65  	n := name
    66  	for k, v := range normalizationWords {
    67  		if strings.Contains(name, k) {
    68  			n = strings.ReplaceAll(name, k, v)
    69  			break
    70  		}
    71  	}
    72  	return n
    73  }
    74  
    75  func firstRuneToLower(name string) string {
    76  	return xstrings.FirstRuneToLower(name)
    77  }