-
Notifications
You must be signed in to change notification settings - Fork 212
/
path.go
113 lines (90 loc) · 2.94 KB
/
path.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
104
105
106
107
108
109
110
111
112
113
package query
import (
"errors"
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
types "github.com/akash-network/akash-api/go/node/market/v1beta3"
dpath "github.com/akash-network/node/x/deployment/query"
)
const (
ordersPath = "orders"
orderPath = "order"
bidsPath = "bids"
bidPath = "bid"
leasesPath = "leases"
leasePath = "lease"
)
var (
ErrInvalidPath = errors.New("query: invalid path")
ErrOwnerValue = errors.New("query: invalid owner value")
ErrStateValue = errors.New("query: invalid state value")
)
// getOrdersPath returns orders path for queries
func getOrdersPath(ofilters OrderFilters) string {
return fmt.Sprintf("%s/%s/%v", ordersPath, ofilters.Owner, ofilters.StateFlagVal)
}
// OrderPath return order path of given order id for queries
func OrderPath(id types.OrderID) string {
return fmt.Sprintf("%s/%s", orderPath, orderParts(id))
}
// getBidsPath returns bids path for queries
func getBidsPath(bfilters BidFilters) string {
return fmt.Sprintf("%s/%s/%v", bidsPath, bfilters.Owner, bfilters.StateFlagVal)
}
// getBidPath return bid path of given bid id for queries
func getBidPath(id types.BidID) string {
return fmt.Sprintf("%s/%s/%s", bidPath, orderParts(id.OrderID()), id.Provider)
}
// getLeasesPath returns leases path for queries
func getLeasesPath(lfilters LeaseFilters) string {
return fmt.Sprintf("%s/%s/%v", leasesPath, lfilters.Owner, lfilters.StateFlagVal)
}
// LeasePath return lease path of given lease id for queries
func LeasePath(id types.LeaseID) string {
return fmt.Sprintf("%s/%s/%s", leasePath, orderParts(id.OrderID()), id.Provider)
}
func orderParts(id types.OrderID) string {
return fmt.Sprintf("%s/%v/%v/%v", id.Owner, id.DSeq, id.GSeq, id.OSeq)
}
// parseOrderPath returns orderID details with provided queries, and return
// error if occurred due to wrong query
func parseOrderPath(parts []string) (types.OrderID, error) {
if len(parts) < 4 {
return types.OrderID{}, ErrInvalidPath
}
did, err := dpath.ParseGroupPath(parts[0:3])
if err != nil {
return types.OrderID{}, err
}
oseq, err := strconv.ParseUint(parts[3], 10, 32)
if err != nil {
return types.OrderID{}, err
}
return types.MakeOrderID(did, uint32(oseq)), nil
}
// parseBidPath returns bidID details with provided queries, and return
// error if occurred due to wrong query
func parseBidPath(parts []string) (types.BidID, error) {
if len(parts) < 5 {
return types.BidID{}, ErrInvalidPath
}
oid, err := parseOrderPath(parts[0:4])
if err != nil {
return types.BidID{}, err
}
provider, err := sdk.AccAddressFromBech32(parts[4])
if err != nil {
return types.BidID{}, err
}
return types.MakeBidID(oid, provider), nil
}
// parseLeasePath returns leaseID details with provided queries, and return
// error if occurred due to wrong query
func ParseLeasePath(parts []string) (types.LeaseID, error) {
bid, err := parseBidPath(parts)
if err != nil {
return types.LeaseID{}, err
}
return types.MakeLeaseID(bid), nil
}