-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.go
203 lines (174 loc) · 5.84 KB
/
util.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package easypost
import (
"fmt"
"strconv"
"strings"
)
type MinifiedRate struct {
ID string `json:"id,omitempty" url:"id,omitempty"`
Service string `json:"service,omitempty" url:"service,omitempty"`
Carrier string `json:"carrier,omitempty" url:"carrier,omitempty"`
Rate string `json:"rate,omitempty" url:"rate,omitempty"`
}
// StringPtr returns a pointer to a string with the given value.
func StringPtr(s string) *string {
return &s
}
// BoolPtr returns a pointer to a bool with the given value.
func BoolPtr(b bool) *bool {
return &b
}
// listContainsString returns true if the given list contains the given string.
func listContainsString(strings []string, targetString string) bool {
for _, aString := range strings {
if aString == targetString {
return true
}
}
return false
}
// lowestSmartRate returns the lowest smartrate from the given list of smartrates.
func (c *Client) lowestSmartRate(rates []*SmartRate, deliveryDays int, deliveryAccuracy string) (out SmartRate, err error) {
validDeliveryAccuracies := []string{"percentile_50", "percentile_75", "percentile_85", "percentile_90", "percentile_95",
"percentile_97", "percentile_99"}
// if the delivery accuracy is not valid, return an error
if !listContainsString(validDeliveryAccuracies, strings.ToLower(deliveryAccuracy)) {
return out, fmt.Errorf("invalid delivery accuracy: %s", deliveryAccuracy)
}
for _, rate := range rates {
smartrateDeliveryDay := -1
switch strings.ToLower(deliveryAccuracy) {
case "percentile_50":
smartrateDeliveryDay = rate.TimeInTransit.Percentile50
case "percentile_75":
smartrateDeliveryDay = rate.TimeInTransit.Percentile75
case "percentile_85":
smartrateDeliveryDay = rate.TimeInTransit.Percentile85
case "percentile_90":
smartrateDeliveryDay = rate.TimeInTransit.Percentile90
case "percentile_95":
smartrateDeliveryDay = rate.TimeInTransit.Percentile95
case "percentile_97":
smartrateDeliveryDay = rate.TimeInTransit.Percentile97
case "percentile_99":
smartrateDeliveryDay = rate.TimeInTransit.Percentile99
default:
break
}
// if this rate's delivery days is greater than the requested delivery days, skip it
if smartrateDeliveryDay > deliveryDays {
continue
}
// if lowest rate is null, set it to this rate
if (out == SmartRate{}) {
out = *rate
continue
}
// if this rate is lower than the lowest rate, set it to this rate
if 0 < rate.Rate && rate.Rate < out.Rate {
out = *rate
}
}
// if not rate was ever set (nothing matched the criteria), return an error
if (out == SmartRate{}) {
return out, newFilteringError(NoRatesFoundMatchingFilters)
}
return
}
// lowestRate returns the lowest rate from the given list of rates with carrier and service filters.
func (c *Client) lowestRate(rates []*MinifiedRate, carriers []string, services []string) (out MinifiedRate, err error) {
carriersMap, servicesMap := make(map[string]bool), make(map[string]bool)
for _, carrier := range carriers {
carriersMap[strings.ToLower(carrier)] = true
}
for _, service := range services {
servicesMap[strings.ToLower(service)] = true
}
for _, rate := range rates {
// if this rate's carrier is not in the carrier list or this rate's service is not in the service list, skip it
if len(carriersMap) > 0 && !carriersMap[strings.ToLower(rate.Carrier)] ||
len(servicesMap) > 0 && !servicesMap[strings.ToLower(rate.Service)] {
continue
}
currentRate, _ := strconv.ParseFloat(out.Rate, 32)
newRate, _ := strconv.ParseFloat(rate.Rate, 32)
// if lowest rate is null, set it to this rate
if (out == MinifiedRate{}) {
out = *rate
continue
}
// if this rate is lower than the lowest rate, set it to this rate
if 0 < newRate && newRate < currentRate {
out = *rate
}
}
// if not rate was ever set (nothing matched the criteria), return an error
if (out == MinifiedRate{}) {
return out, newFilteringError(NoRatesFoundMatchingFilters)
}
return
}
// lowestObjectRate returns the lowest rate from the given list of rates.
func (c *Client) lowestObjectRate(rates []*Rate, carriers []string, services []string) (out Rate, err error) {
filterRates := make([]*MinifiedRate, 0)
for _, rate := range rates {
filterRates = append(filterRates, &MinifiedRate{
ID: rate.ID,
Service: rate.Service,
Carrier: rate.Carrier,
Rate: rate.Rate,
})
}
lowestRate, err := c.lowestRate(filterRates, carriers, services)
if err == nil {
for _, rate := range rates {
if rate.ID == lowestRate.ID {
return *rate, nil
}
}
}
return
}
// lowestStatelessRate returns the lowest stateless rate from the given list of stateless rates.
func (c *Client) lowestStatelessRate(rates []*StatelessRate, carriers []string, services []string) (out StatelessRate, err error) {
filterRates := make([]*MinifiedRate, 0)
for _, rate := range rates {
filterRates = append(filterRates, &MinifiedRate{
ID: "",
Service: rate.Service,
Carrier: rate.Carrier,
Rate: rate.Rate,
})
}
lowestRate, err := c.lowestRate(filterRates, carriers, services)
if err == nil {
for _, rate := range rates {
// no ID to compare, so compare carrier and service
if rate.Carrier == lowestRate.Carrier && rate.Service == lowestRate.Service {
return *rate, nil
}
}
}
return
}
// lowestPickupRate returns the lowest pickup rate from the given list of pickup rates.
func (c *Client) lowestPickupRate(rates []*PickupRate, carriers []string, services []string) (out PickupRate, err error) {
filterRates := make([]*MinifiedRate, 0)
for _, rate := range rates {
filterRates = append(filterRates, &MinifiedRate{
ID: rate.ID,
Service: rate.Service,
Carrier: rate.Carrier,
Rate: rate.Rate,
})
}
lowestRate, err := c.lowestRate(filterRates, carriers, services)
if err == nil {
for _, rate := range rates {
if rate.ID == lowestRate.ID {
return *rate, nil
}
}
}
return
}