forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnames.go
151 lines (116 loc) · 4.31 KB
/
names.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import "strings"
// We struggle with many types of names, and many conversions between types of names. In order to sanitize all of this,
// we have four interfaces, and many string-aliased types, which conform to at most one of the interfaces.
// This way, we can't wind up with complex, lossy, and hard-to-trace name conversions, which used to be a big problem in tpgtools.
// The functions are not really meant to be called, so much, but they are useful for converting back to regular strings where needed.
type snakeCaseName interface {
snakecase() string
}
type titleCaseName interface {
titlecase() string
}
type jsonCaseName interface {
jsoncase() string
}
type lowercaseName interface {
lowercase() string
}
// e.g. `google_compute_instance` or `google_orgpolicy_policy`.
type SnakeCaseTerraformResourceName string
func (s SnakeCaseTerraformResourceName) snakecase() string {
return string(s)
}
// e.g. `ComputeInstanceGroupManager`.
type TitleCaseFullName string
func (s TitleCaseFullName) titlecase() string {
return string(s)
}
// e.g. "compute_firewall_rule"
type SnakeCaseFullName string
func (s SnakeCaseFullName) snakecase() string {
return string(s)
}
// e.g. "os_policy"
type SnakeCaseProductName string
func (s SnakeCaseProductName) snakecase() string {
return string(s)
}
func (s SnakeCaseProductName) ToTitle() RenderedString {
return RenderedString(snakeToTitleCase(s).titlecase())
}
// e.g. "ForwardingRule"
type TitleCaseResourceName string
func (t TitleCaseResourceName) titlecase() string {
return string(t)
}
// e.g. "computeinstancegroupmanager".
type ConjoinedString string
// snakeToLowercase converts a snake_case string to a conjoined string
func snakeToLowercase(s snakeCaseName) ConjoinedString {
return ConjoinedString(strings.Join(snakeToParts(s, false), ""))
}
// snakeToTitleCase converts a snake_case string to TitleCase / Go struct case.
func snakeToTitleCase(s snakeCaseName) miscellaneousNameTitleCase {
return miscellaneousNameTitleCase(strings.Join(snakeToParts(s, true), ""))
}
// A type for a string that is not meant for further conversion. Some functions return a
// RenderedString to indicate that they have been lossily converted to another format.
type RenderedString string
func (r RenderedString) String() string {
return string(r)
}
func renderSnakeAsTitle(s snakeCaseName) RenderedString {
return RenderedString(strings.Join(snakeToParts(s, true), ""))
}
// e.g. "ospolicy"
type DCLPackageName string
func (d DCLPackageName) lowercase() string {
return string(d)
}
type BasePathOverrideNameSnakeCase string
func (b BasePathOverrideNameSnakeCase) snakecase() string {
return string(b)
}
func (b BasePathOverrideNameSnakeCase) ToUpper() RenderedString {
return RenderedString(strings.ToUpper(string(b)))
}
func (b BasePathOverrideNameSnakeCase) ToTitle() RenderedString {
title := snakeToTitleCase(b).titlecase()
// Got to special case the capitalization of "OS" in "OSConfig", for base paths specifically,
// because of interop with MMv1.
if strings.HasPrefix(string(b), "os") {
return RenderedString("OS" + title[2:])
}
if strings.HasPrefix(string(b), "gkehub") {
return RenderedString("GKEHub" + title[6:])
}
if strings.HasPrefix(string(b), "vertex_ai") {
return RenderedString("VertexAI" + title[8:])
}
return RenderedString(title)
}
// A path on the filesystem, usually relative to the root of the tpgtools/ directory.
type Filepath string
// A package path, potentially including a version suffix.
// e.g. "ospolicy/beta" or "ospolicy"
type DCLPackageNameWithVersion string
// A type for some string, not one of the things that have a specific type above, which is in
// a particular case. This is useful because we want to be able to write strings functions that take in
// a snake case string or return a snake case string, which works even if the string isn't a
// specific type.
//
// Also, having all these misc strings prevents us from winding up with a bunch of `string` types
// for things that should be explicit.
type miscellaneousNameSnakeCase string
func (m miscellaneousNameSnakeCase) snakecase() string {
return string(m)
}
type miscellaneousNameTitleCase string
func (m miscellaneousNameTitleCase) titlecase() string {
return string(m)
}
type miscellaneousNameLowercase string
func (m miscellaneousNameLowercase) lowercase() string {
return string(m)
}