...
1 package awsrulesfn
2
3 import (
4 "strings"
5 )
6
7
8 type ARN struct {
9 Partition string
10 Service string
11 Region string
12 AccountId string
13 ResourceId OptionalStringSlice
14 }
15
16 const (
17 arnDelimiters = ":"
18 resourceDelimiters = "/:"
19 arnSections = 6
20 arnPrefix = "arn:"
21
22
23 sectionPartition = 1
24 sectionService = 2
25 sectionRegion = 3
26 sectionAccountID = 4
27 sectionResource = 5
28 )
29
30
31
32
33 func ParseARN(input string) *ARN {
34 if !strings.HasPrefix(input, arnPrefix) {
35 return nil
36 }
37
38 sections := strings.SplitN(input, arnDelimiters, arnSections)
39 if numSections := len(sections); numSections != arnSections {
40 return nil
41 }
42
43 if sections[sectionPartition] == "" {
44 return nil
45 }
46 if sections[sectionService] == "" {
47 return nil
48 }
49 if sections[sectionResource] == "" {
50 return nil
51 }
52
53 return &ARN{
54 Partition: sections[sectionPartition],
55 Service: sections[sectionService],
56 Region: sections[sectionRegion],
57 AccountId: sections[sectionAccountID],
58 ResourceId: splitResource(sections[sectionResource]),
59 }
60 }
61
62
63 func splitResource(v string) []string {
64 var parts []string
65 var offset int
66
67 for offset <= len(v) {
68 idx := strings.IndexAny(v[offset:], "/:")
69 if idx < 0 {
70 parts = append(parts, v[offset:])
71 break
72 }
73 parts = append(parts, v[offset:idx+offset])
74 offset += idx + 1
75 }
76
77 return parts
78 }
79
80
81
82
83 type OptionalStringSlice []string
84
85
86
87 func (s OptionalStringSlice) Get(i int) *string {
88 if i < 0 || i >= len(s) {
89 return nil
90 }
91
92 v := s[i]
93 return &v
94 }
95
View as plain text