forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
103 lines (91 loc) · 3.05 KB
/
strings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2021 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"regexp"
"strings"
"github.com/golang/glog"
)
// Map from initialism -> TitleCase variant
// We can assume camelCase is the same as TitleCase except that we downcase the
// first segment
var initialisms = map[string]string{
"ip": "IP",
"ipv4": "IPv4",
"ipv6": "IPv6",
"oauth": "OAuth",
"oauth2": "OAuth2",
"tpu": "TPU",
"vpc": "VPC",
}
func assertSnakeArray(ss []string) []miscellaneousNameSnakeCase {
var sc []miscellaneousNameSnakeCase
for _, s := range ss {
if strings.ToLower(s) != s {
glog.Exitf("assertion failed: snake case value not in snake case. %q != %q (same string passed to ToLower).", s, strings.ToLower(s))
}
sc = append(sc, miscellaneousNameSnakeCase(s))
}
return sc
}
func concatenateSnakeCase(s ...snakeCaseName) miscellaneousNameSnakeCase {
var names []string
for _, n := range s {
names = append(names, n.snakecase())
}
return miscellaneousNameSnakeCase(strings.Join(names, "_"))
}
// snakeToParts returns the parts of a snake_case string absent of '_'
// if titleCase is true these segents will have their first letter capitalized
func snakeToParts(s snakeCaseName, titleCase bool) []string {
parts := []string{}
segments := strings.Split(s.snakecase(), "_")
for _, seg := range segments {
if v, ok := initialisms[seg]; ok {
parts = append(parts, v)
} else {
var newPart string = seg
if titleCase && len(newPart) > 0 {
newPart = strings.ToUpper(newPart[0:1]) + newPart[1:]
}
parts = append(parts, newPart)
}
}
return parts
}
// jsonToSnakeCase converts a jsonCase string to snake_case.
func jsonToSnakeCase(s string) miscellaneousNameSnakeCase {
for _, v := range initialisms {
s = strings.ReplaceAll(s, v, v[0:1]+strings.ToLower(v[1:]))
}
result := regexp.MustCompile("(.)([A-Z][^A-Z]+)").ReplaceAllString(s, "${1}_${2}")
return miscellaneousNameSnakeCase(strings.ToLower(regexp.MustCompile("([a-z0-9])([A-Z])").ReplaceAllString(result, "${1}_${2}")))
}
func sampleNameToTitleCase(s string) miscellaneousNameTitleCase {
return miscellaneousNameTitleCase(strings.Join(sampleNameToTitleParts(s), ""))
}
func sampleNameToTitleParts(s string) []string {
parts := []string{}
s = strings.ReplaceAll(s, "/", " ")
s = strings.ReplaceAll(s, "_", " ")
segments := strings.Split(s, " ")
for _, seg := range segments {
if v, ok := initialisms[seg]; ok {
parts = append(parts, v)
} else {
parts = append(parts, strings.ToUpper(seg[0:1])+seg[1:])
}
}
return parts
}