forked from awslabs/goformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naming.go
70 lines (49 loc) · 1.61 KB
/
naming.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
package main
import (
"errors"
"strings"
)
// filename takes a resource or property name (e.g. AWS::CloudFront::Distribution.Restrictions)
// and returns an appropriate filename for the generated struct (e.g. aws-cloudfront-distribution_restrictions.go)
func filename(input string) string {
// Convert to lowercase
output := strings.ToLower(input)
// Replace :: with -
output = strings.Replace(output, "::", "-", -1)
// Replace . with _
output = strings.Replace(output, ".", "_", -1)
// Suffix with .go
output += ".go"
return output
}
// structName takes a resource or property name (e.g. AWS::CloudFront::Distribution.Restrictions)
// and returns an appropriate struct name for the generated struct (e.g. AWSCloudfrontDistributionRestrictions)
func structName(input string) (string, error) {
// Remove ::
//output := strings.Replace(input, "::", "", -1)
if input == "Tag" {
return "Tag", nil
}
parts := strings.Split(input, "::")
if len(parts) < 2 {
return "", errors.New("invalid CloudFormation resource type: " + input)
}
// Remove .
output := strings.Replace(parts[2], ".", "_", -1)
return output, nil
}
// packageName generates a go package name based on the AWS CloudFormation resource type
// For example, AWS::S3::Bucket would generate a package named 's3'
func packageName(input string, lowercase bool) (string, error) {
if input == "Tag" {
return "cloudformation", nil
}
parts := strings.Split(input, "::")
if len(parts) < 2 {
return "", errors.New("invalid CloudFormation resource type: " + input)
}
if lowercase {
return strings.ToLower(parts[1]), nil
}
return parts[1], nil
}