github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/newrelic/helpers.go (about)

     1  // Copyright 2019 The Terraformer 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 newrelic
    16  
    17  import (
    18  	"regexp"
    19  	"strings"
    20  )
    21  
    22  func removeDuplicate(s string) string {
    23  	if len(s) < 2 {
    24  		return s
    25  	}
    26  
    27  	src := []byte(s)
    28  	dest := make([]byte, len(src))
    29  	dest[0] = src[0]
    30  
    31  	j := 0
    32  	for i := 1; i < len(s); i++ {
    33  		if dest[j] != src[i] {
    34  			j++
    35  			dest[j] = src[i]
    36  		}
    37  	}
    38  
    39  	return string(dest[:j+1])
    40  }
    41  
    42  // Making resource's name less ugly
    43  func normalizeResourceName(s string) string {
    44  	specialChars := `<>()*#{}[]|@_ .%'",&`
    45  	for _, c := range specialChars {
    46  		s = strings.ReplaceAll(s, string(c), "-")
    47  	}
    48  
    49  	s = regexp.MustCompile(`^[^a-zA-Z_]+`).ReplaceAllLiteralString(s, "")
    50  	s = strings.TrimSuffix(s, "-")
    51  	s = removeDuplicate(s)
    52  
    53  	return strings.ToLower(s)
    54  }