forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
83 lines (71 loc) · 1.96 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
package goshopify
import (
"fmt"
"net/url"
"strings"
"time"
)
// Return the full shop name, including .myshopify.com
func ShopFullName(name string) string {
name = strings.TrimSpace(name)
name = strings.Trim(name, ".")
if strings.Contains(name, "myshopify.com") {
return name
}
return name + ".myshopify.com"
}
// Return the short shop name, excluding .myshopify.com
func ShopShortName(name string) string {
// Convert to fullname and remove the myshopify part. Perhaps not the most
// performant solution, but then we don't have to repeat all the trims here
// :-)
return strings.Replace(ShopFullName(name), ".myshopify.com", "", -1)
}
// Return the Shop's base url.
func ShopBaseUrl(name string) string {
name = ShopFullName(name)
return fmt.Sprintf("https://%s", name)
}
// Return the prefix for a metafield path
func MetafieldPathPrefix(resource string, resourceID int64) string {
prefix := "metafields"
if resource != "" {
prefix = fmt.Sprintf("%s/%d/metafields", resource, resourceID)
}
return prefix
}
// Return the prefix for a fulfillment path
func FulfillmentPathPrefix(resource string, resourceID int64) string {
prefix := "fulfillments"
if resource != "" {
prefix = fmt.Sprintf("%s/%d/fulfillments", resource, resourceID)
}
return prefix
}
type OnlyDate struct {
time.Time
}
func (c *OnlyDate) UnmarshalJSON(b []byte) error {
value := strings.Trim(string(b), `"`)
if value == "" || value == "null" {
*c = OnlyDate{time.Time{}}
return nil
}
t, err := time.Parse("2006-01-02", value)
if err != nil {
return err
}
*c = OnlyDate{t}
return nil
}
func (c *OnlyDate) MarshalJSON() ([]byte, error) {
return []byte(c.String()), nil
}
// It seems shopify accepts both the date with double-quotes and without them, so we just stick to the double-quotes for now.
func (c *OnlyDate) EncodeValues(key string, v *url.Values) error {
v.Add(key, c.String())
return nil
}
func (c *OnlyDate) String() string {
return `"` + c.Format("2006-01-02") + `"`
}