-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaccount_details.go
95 lines (75 loc) · 2.45 KB
/
account_details.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
package alks
import (
"errors"
"regexp"
"strings"
)
// This regex will attempt to parse ALKS account strings (and must be valid for the package to compile)
// *** WARNING ***: The group names in the regex are referenced below, changing them means updating the associated methods as well
var accountRegex = regexp.MustCompile(`(?P<AccountNumber>\d+)(/(?P<RoleName>(ALKS)?\w+)(\s-\s(?P<AccountDesc>\w+))?)?`)
// AccountDetails represents the callers Account and Role information for ALKS requests
type AccountDetails struct {
Account string `json:"account,omitempty"`
Role string `json:"role,omitempty"`
}
// GetAccountNumber parses the Account provided in AccountDetails and returns the account number if present
func (a AccountDetails) GetAccountNumber() (string, error) {
if a.Account == "" {
return "", errors.New("Account is empty")
}
if accountRegex.MatchString(a.Account) {
matches := accountRegex.FindStringSubmatch(a.Account)
for i, v := range accountRegex.SubexpNames() {
if v == "AccountNumber" {
return matches[i], nil
}
}
}
return "", errors.New("Invalid Account format")
}
// GetRoleName returns the AccountDetails Role or parses the role value from the Account
func (a AccountDetails) GetRoleName(stripPrefix bool) (string, error) {
if a.Role != "" {
if stripPrefix {
return strings.TrimPrefix(a.Role, "ALKS"), nil
}
return a.Role, nil
}
if a.Account == "" {
return "", errors.New("Account is empty")
}
if accountRegex.MatchString(a.Account) {
matches := accountRegex.FindStringSubmatch(a.Account)
for i, v := range accountRegex.SubexpNames() {
if v == "RoleName" {
roleName := matches[i]
if roleName == "" {
return "", errors.New("No Role found")
}
if stripPrefix {
return strings.TrimPrefix(roleName, "ALKS"), nil
}
return roleName, nil
}
}
}
return "", errors.New("Invalid Account format")
}
// GetAccountDesc parses the Account provided in AccountDetails and returns the account description if present
func (a AccountDetails) GetAccountDesc() (string, error) {
if a.Account == "" {
return "", errors.New("Account is empty")
}
if accountRegex.MatchString(a.Account) {
matches := accountRegex.FindStringSubmatch(a.Account)
for i, v := range accountRegex.SubexpNames() {
if v == "AccountDesc" {
if matches[i] == "" {
return "", errors.New("No AccountDesc found")
}
return matches[i], nil
}
}
}
return "", errors.New("Invalid Account format")
}