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

     1  // Copyright 2018 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 rabbitmq
    16  
    17  import (
    18  	"strings"
    19  	"unicode"
    20  
    21  	"golang.org/x/text/secure/precis"
    22  	"golang.org/x/text/transform"
    23  	"golang.org/x/text/unicode/norm"
    24  )
    25  
    26  func normalizeResourceName(s string) string {
    27  	normalize := precis.NewIdentifier(
    28  		precis.AdditionalMapping(func() transform.Transformer {
    29  			return transform.Chain(norm.NFD, transform.RemoveFunc(func(r rune) bool { //nolint
    30  				return unicode.Is(unicode.Mn, r)
    31  			}))
    32  		}),
    33  		precis.Norm(norm.NFC),
    34  	)
    35  	normalizedLower, _ := normalize.String(strings.ToLower(s))
    36  	r := strings.NewReplacer(" ", "_",
    37  		"!", "_",
    38  		"\"", "_",
    39  		"#", "_",
    40  		"%", "_",
    41  		"&", "_",
    42  		"'", "_",
    43  		"(", "_",
    44  		")", "_",
    45  		"{", "_",
    46  		"}", "_",
    47  		"*", "_",
    48  		"+", "_",
    49  		",", "_",
    50  		"-", "_",
    51  		".", "_",
    52  		"/", "slash",
    53  		"|", "_",
    54  		"\\", "_",
    55  		":", "_",
    56  		";", "_",
    57  		">", "_",
    58  		"=", "_",
    59  		"<", "_",
    60  		"?", "_",
    61  		"[", "_",
    62  		"]", "_",
    63  		"^", "_",
    64  		"`", "_",
    65  		"~", "_",
    66  		"$", "_",
    67  		"@", "_at_")
    68  	return r.Replace(normalizedLower)
    69  }