forked from akash-network/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
103 lines (87 loc) · 2.69 KB
/
id.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
package v1beta1
import (
fmt "fmt"
"strconv"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Equals method compares specific deployment with provided deployment
func (id DeploymentID) Equals(other DeploymentID) bool {
return id.Owner == other.Owner && id.DSeq == other.DSeq
}
// Validate method for DeploymentID and returns nil
func (id DeploymentID) Validate() error {
_, err := sdk.AccAddressFromBech32(id.Owner)
switch {
case err != nil:
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "DeploymentID: Invalid Owner Address")
case id.DSeq == 0:
return sdkerrors.Wrap(sdkerrors.ErrInvalidSequence, "DeploymentID: Invalid Deployment Sequence")
}
return nil
}
// String method for deployment IDs
func (id DeploymentID) String() string {
return fmt.Sprintf("%s/%d", id.Owner, id.DSeq)
}
func (id DeploymentID) GetOwnerAddress() (sdk.Address, error) {
return sdk.AccAddressFromBech32(id.Owner)
}
func ParseDeploymentID(val string) (DeploymentID, error) {
parts := strings.Split(val, "/")
return ParseDeploymentPath(parts)
}
// ParseDeploymentPath returns DeploymentID details with provided queries, and return
// error if occurred due to wrong query
func ParseDeploymentPath(parts []string) (DeploymentID, error) {
if len(parts) != 2 {
return DeploymentID{}, ErrInvalidIDPath
}
owner, err := sdk.AccAddressFromBech32(parts[0])
if err != nil {
return DeploymentID{}, err
}
dseq, err := strconv.ParseUint(parts[1], 10, 64)
if err != nil {
return DeploymentID{}, err
}
return DeploymentID{
Owner: owner.String(),
DSeq: dseq,
}, nil
}
// MakeGroupID returns GroupID instance with provided deployment details
// and group sequence number.
func MakeGroupID(id DeploymentID, gseq uint32) GroupID {
return GroupID{
Owner: id.Owner,
DSeq: id.DSeq,
GSeq: gseq,
}
}
// DeploymentID method returns DeploymentID details with specific group details
func (id GroupID) DeploymentID() DeploymentID {
return DeploymentID{
Owner: id.Owner,
DSeq: id.DSeq,
}
}
// Equals method compares specific group with provided group
func (id GroupID) Equals(other GroupID) bool {
return id.DeploymentID().Equals(other.DeploymentID()) && id.GSeq == other.GSeq
}
// Validate method for GroupID and returns nil
func (id GroupID) Validate() error {
if err := id.DeploymentID().Validate(); err != nil {
return sdkerrors.Wrap(err, "GroupID: Invalid DeploymentID")
}
if id.GSeq == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidSequence, "GroupID: Invalid Group Sequence")
}
return nil
}
// String method provides human readable representation of GroupID.
func (id GroupID) String() string {
return fmt.Sprintf("%s/%d", id.DeploymentID(), id.GSeq)
}