Skip to content

Commit 6fb60e8

Browse files
committed
update string method
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
1 parent df9103f commit 6fb60e8

File tree

2 files changed

+89
-45
lines changed

2 files changed

+89
-45
lines changed

clients/operationperm.go

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,49 @@ import "fmt"
88
type Operation int
99

1010
func (op Operation) String() string {
11-
if (int(op) < 0) || (int(op) == len(OperationNames)) {
12-
return fmt.Sprintf("UnknownOperation(%d)", op)
11+
switch op {
12+
case OpViewClient:
13+
return OpViewClientStr
14+
case OpUpdateClient:
15+
return OpUpdateClientStr
16+
case OpUpdateClientTags:
17+
return OpUpdateClientTagsStr
18+
case OpUpdateClientSecret:
19+
return OpUpdateClientSecretStr
20+
case OpEnableClient:
21+
return OpEnableClientStr
22+
case OpDisableClient:
23+
return OpDisableClientStr
24+
case OpDeleteClient:
25+
return OpDeleteClientStr
26+
case OpSetParentGroup:
27+
return OpSetParentGroupStr
28+
case OpRemoveParentGroup:
29+
return OpRemoveParentGroupStr
30+
case OpConnectToChannel:
31+
return OpConnectToChannelStr
32+
case OpDisconnectFromChannel:
33+
return OpDisconnectFromChannelStr
34+
case OpCreateClient:
35+
return OpCreateClientStr
36+
case OpListClients:
37+
return OpListClientsStr
38+
case OpListUserClients:
39+
return OpListUserClientsStr
40+
default:
41+
return fmt.Sprintf("unknown operation: %d", op)
1342
}
14-
return OperationNames[op]
1543
}
1644

1745
type OperationPerm struct {
1846
opPerm map[Operation]Permission
1947
expectedOps []Operation
20-
opNames []string
2148
}
2249

23-
func newOperationPerm(expectedOps []Operation, opNames []string) OperationPerm {
50+
func newOperationPerm(expectedOps []Operation) OperationPerm {
2451
return OperationPerm{
2552
opPerm: make(map[Operation]Permission),
2653
expectedOps: expectedOps,
27-
opNames: opNames,
2854
}
2955
}
3056

@@ -87,24 +113,42 @@ func (p Permission) String() string {
87113

88114
type ExternalOperation int
89115

90-
func (op ExternalOperation) String(operations []string) string {
91-
if (int(op) < 0) || (int(op) == len(operations)) {
92-
return fmt.Sprintf("UnknownOperation(%d)", op)
116+
// func (op ExternalOperation) String(operations []string) string {
117+
// if (int(op) < 0) || (int(op) == len(operations)) {
118+
// return fmt.Sprintf("UnknownOperation(%d)", op)
119+
// }
120+
// return operations[op]
121+
// }
122+
123+
func (op ExternalOperation) String() string {
124+
switch op {
125+
case DomainOpCreateClient:
126+
return DomainOpCreateClientStr
127+
case DomainOpListClients:
128+
return DomainOpListClientsStr
129+
case GroupOpSetChildClient:
130+
return GroupOpSetChildClientStr
131+
case GroupsOpRemoveChildClient:
132+
return GroupsOpRemoveChildClientStr
133+
case ChannelsOpConnectChannel:
134+
return ChannelsOpConnectChannelStr
135+
case ChannelsOpDisconnectChannel:
136+
return ChannelsOpDisconnectChannelStr
137+
default:
138+
return fmt.Sprintf("unknown external operation: %d", op)
93139
}
94-
return operations[op]
95140
}
96141

97142
type ExternalOperationPerm struct {
98143
opPerm map[ExternalOperation]Permission
99144
expectedOps []ExternalOperation
100-
opNames []string
145+
// opNames []string
101146
}
102147

103-
func newExternalOperationPerm(expectedOps []ExternalOperation, opNames []string) ExternalOperationPerm {
148+
func newExternalOperationPerm(expectedOps []ExternalOperation) ExternalOperationPerm {
104149
return ExternalOperationPerm{
105150
opPerm: make(map[ExternalOperation]Permission),
106151
expectedOps: expectedOps,
107-
opNames: opNames,
108152
}
109153
}
110154

@@ -121,7 +165,7 @@ func (eopp ExternalOperationPerm) AddOperationPermissionMap(eopMap map[ExternalO
121165
// First iteration check all the keys are valid, If any one key is invalid then no key should be added.
122166
for eop := range eopMap {
123167
if !eopp.isKeyRequired(eop) {
124-
return fmt.Errorf("%v is not a valid external operation", eop.String(eopp.opNames))
168+
return fmt.Errorf("%v is not a valid external operation", eop.String())
125169
}
126170
}
127171
for eop, perm := range eopMap {
@@ -133,12 +177,12 @@ func (eopp ExternalOperationPerm) AddOperationPermissionMap(eopMap map[ExternalO
133177
func (eopp ExternalOperationPerm) Validate() error {
134178
for eop := range eopp.opPerm {
135179
if !eopp.isKeyRequired(eop) {
136-
return fmt.Errorf("ExternalOperationPerm: \"%s\" is not a valid external operation", eop.String(eopp.opNames))
180+
return fmt.Errorf("ExternalOperationPerm: \"%s\" is not a valid external operation", eop.String())
137181
}
138182
}
139183
for _, eeo := range eopp.expectedOps {
140184
if _, ok := eopp.opPerm[eeo]; !ok {
141-
return fmt.Errorf("ExternalOperationPerm: \"%s\" external operation is missing", eeo.String(eopp.opNames))
185+
return fmt.Errorf("ExternalOperationPerm: \"%s\" external operation is missing", eeo.String())
142186
}
143187
}
144188
return nil
@@ -148,5 +192,5 @@ func (eopp ExternalOperationPerm) GetPermission(eop ExternalOperation) (Permissi
148192
if perm, ok := eopp.opPerm[eop]; ok {
149193
return perm, nil
150194
}
151-
return "", fmt.Errorf("external operation \"%s\" doesn't have any permissions", eop.String(eopp.opNames))
195+
return "", fmt.Errorf("external operation \"%s\" doesn't have any permissions", eop.String())
152196
}

clients/roleoperations.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ const (
2121
OpListUserClients
2222
)
2323

24+
const (
25+
OpViewClientStr = "OpViewClient"
26+
OpUpdateClientStr = "OpUpdateClient"
27+
OpUpdateClientTagsStr = "OpUpdateClientTags"
28+
OpUpdateClientSecretStr = "OpUpdateClientSecret"
29+
OpEnableClientStr = "OpEnableClient"
30+
OpDisableClientStr = "OpDisableClient"
31+
OpDeleteClientStr = "OpDeleteClient"
32+
OpSetParentGroupStr = "OpSetParentGroup"
33+
OpRemoveParentGroupStr = "OpRemoveParentGroup"
34+
OpConnectToChannelStr = "OpConnectToChannel"
35+
OpDisconnectFromChannelStr = "OpDisconnectFromChannel"
36+
OpCreateClientStr = "OpCreateClient"
37+
OpListClientsStr = "OpListClients"
38+
OpListUserClientsStr = "OpListUserClients"
39+
)
40+
2441
var expectedOperations = []Operation{
2542
OpViewClient,
2643
OpUpdateClient,
@@ -35,25 +52,8 @@ var expectedOperations = []Operation{
3552
OpDisconnectFromChannel,
3653
}
3754

38-
var OperationNames = []string{
39-
"OpViewClient",
40-
"OpUpdateClient",
41-
"OpUpdateClientTags",
42-
"OpUpdateClientSecret",
43-
"OpEnableClient",
44-
"OpDisableClient",
45-
"OpDeleteClient",
46-
"OpSetParentGroup",
47-
"OpRemoveParentGroup",
48-
"OpConnectToChannel",
49-
"OpDisconnectFromChannel",
50-
"OpCreateClient",
51-
"OpListClients",
52-
"OpListUserClients",
53-
}
54-
5555
func NewOperationPerm() OperationPerm {
56-
return newOperationPerm(expectedOperations, OperationNames)
56+
return newOperationPerm(expectedOperations)
5757
}
5858

5959
// External Operations.
@@ -66,6 +66,15 @@ const (
6666
ChannelsOpDisconnectChannel
6767
)
6868

69+
const (
70+
DomainOpCreateClientStr = "DomainOpCreateClient"
71+
DomainOpListClientsStr = "DomainOpListClients"
72+
GroupOpSetChildClientStr = "GroupOpSetChildClient"
73+
GroupsOpRemoveChildClientStr = "GroupsOpRemoveChildClient"
74+
ChannelsOpConnectChannelStr = "ChannelsOpConnectChannel"
75+
ChannelsOpDisconnectChannelStr = "ChannelsOpDisconnectChannel"
76+
)
77+
6978
var expectedExternalOperations = []ExternalOperation{
7079
DomainOpCreateClient,
7180
DomainOpListClients,
@@ -75,17 +84,8 @@ var expectedExternalOperations = []ExternalOperation{
7584
ChannelsOpDisconnectChannel,
7685
}
7786

78-
var externalOperationNames = []string{
79-
"DomainOpCreateClient",
80-
"DomainOpListClients",
81-
"GroupOpSetChildClient",
82-
"GroupsOpRemoveChildClient",
83-
"ChannelsOpConnectChannel",
84-
"ChannelsOpDisconnectChannel",
85-
}
86-
8787
func NewExternalOperationPerm() ExternalOperationPerm {
88-
return newExternalOperationPerm(expectedExternalOperations, externalOperationNames)
88+
return newExternalOperationPerm(expectedExternalOperations)
8989
}
9090

9191
// Below codes should moved out of service, may be can be kept in `cmd/<svc>/main.go`

0 commit comments

Comments
 (0)