forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetafield.go
167 lines (137 loc) · 6.29 KB
/
metafield.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
package goshopify
import (
"fmt"
"time"
)
// MetafieldService is an interface for interfacing with the metafield endpoints
// of the Shopify API.
// https://help.shopify.com/api/reference/metafield
type MetafieldService interface {
List(interface{}) ([]Metafield, error)
Count(interface{}) (int, error)
Get(int64, interface{}) (*Metafield, error)
Create(Metafield) (*Metafield, error)
Update(Metafield) (*Metafield, error)
Delete(int64) error
}
// MetafieldsService is an interface for other Shopify resources
// to interface with the metafield endpoints of the Shopify API.
// https://help.shopify.com/api/reference/metafield
type MetafieldsService interface {
ListMetafields(int64, interface{}) ([]Metafield, error)
CountMetafields(int64, interface{}) (int, error)
GetMetafield(int64, int64, interface{}) (*Metafield, error)
CreateMetafield(int64, Metafield) (*Metafield, error)
UpdateMetafield(int64, Metafield) (*Metafield, error)
DeleteMetafield(int64, int64) error
}
// MetafieldServiceOp handles communication with the metafield
// related methods of the Shopify API.
type MetafieldServiceOp struct {
client *Client
resource string
resourceID int64
}
type metafieldType string
// https://shopify.dev/docs/api/admin-rest/2023-07/resources/metafield#resource-object
const (
//True or false.
MetafieldTypeBoolean metafieldType = "boolean"
//A hexidecimal color code, #fff123.
MetafieldTypeColor metafieldType = "color" //#fff123
//ISO601, YYYY-MM-DD.
MetafieldTypeDate metafieldType = "date"
//ISO8601, YYYY-MM-DDTHH:MM:SS.
MetafieldTypeDatetime metafieldType = "date_time"
//JSON, {"value:" 25.0, "unit": "cm"}.
MetafieldTypeDimension metafieldType = "dimension"
//{"ingredient": "flour", "amount": 0.3}.
MetafieldTypeJSON metafieldType = "json"
//JSON, {"amount": 5.99, "currency_code": "CAD"}.
MetafieldTypeMoney metafieldType = "money"
//lines of text separated with newline characters.
MetafieldTypeMultiLineTextField metafieldType = "multi_line_text_field"
//10.4.
MetafieldTypeNumberDecimal metafieldType = "number_decimal"
//10.
MetafieldTypeNumberInteger metafieldType = "number_integer"
//JSON, {"value": "3.5", "scale_min": "1.0", "scale_max": "5.0"}.
MetafieldTypeRating metafieldType = "rating"
//JSON, {"type": "root","children": [{"type": "paragraph","children": [{"type": "text","value": "Bold text.","bold": true}]}]}.
MetafieldTypeRichTextField metafieldType = "rich_text_field"
//A single line of text. Do not use for numbers or dates, use correct type instead!
MetafieldTypeSingleLineTextField metafieldType = "single_line_text_field"
//https, http, mailto, sms, or tel.
MetafieldTypeURL metafieldType = "url"
//JSON, {"value:" 20.0, "unit": "ml"}.
MetafieldTypeVolume metafieldType = "volume"
//JSON, {"value:" 2.5, "unit": "kg"}.
MetafieldTypeWeight metafieldType = "weight"
)
// Metafield represents a Shopify metafield.
type Metafield struct {
CreatedAt *time.Time `json:"created_at,omitempty"`
Description string `json:"description,omitempty"` //Description of the metafield.
ID int64 `json:"id,omitempty"` //Assigned by Shopify, used for updating a metafield.
Key string `json:"key,omitempty"` //The unique identifier for a metafield within its namespace, 3-64 characters long.
Namespace string `json:"namespace,omitempty"` //The container for a group of metafields, 3-255 characters long.
OwnerId int64 `json:"owner_id,omitempty"` //The unique ID of the resource the metafield is for, i.e.: an Order ID.
OwnerResource string `json:"owner_resource,omitempty"` //The type of reserouce the metafield is for, i.e.: and Order.
UpdatedAt *time.Time `json:"updated_at,omitempty"` //
Value interface{} `json:"value,omitempty"` //The data stored in the metafield. Always stored as a string, use Type field for actual data type.
Type metafieldType `json:"type,omitempty"` //One of Shopify's defined types, see metafieldType.
AdminGraphqlAPIID string `json:"admin_graphql_api_id,omitempty"`
}
// MetafieldResource represents the result from the metafields/X.json endpoint
type MetafieldResource struct {
Metafield *Metafield `json:"metafield"`
}
// MetafieldsResource represents the result from the metafields.json endpoint
type MetafieldsResource struct {
Metafields []Metafield `json:"metafields"`
}
// List metafields
func (s *MetafieldServiceOp) List(options interface{}) ([]Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s.json", prefix)
resource := new(MetafieldsResource)
err := s.client.Get(path, resource, options)
return resource.Metafields, err
}
// Count metafields
func (s *MetafieldServiceOp) Count(options interface{}) (int, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/count.json", prefix)
return s.client.Count(path, options)
}
// Get individual metafield
func (s *MetafieldServiceOp) Get(metafieldID int64, options interface{}) (*Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d.json", prefix, metafieldID)
resource := new(MetafieldResource)
err := s.client.Get(path, resource, options)
return resource.Metafield, err
}
// Create a new metafield
func (s *MetafieldServiceOp) Create(metafield Metafield) (*Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s.json", prefix)
wrappedData := MetafieldResource{Metafield: &metafield}
resource := new(MetafieldResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Metafield, err
}
// Update an existing metafield
func (s *MetafieldServiceOp) Update(metafield Metafield) (*Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d.json", prefix, metafield.ID)
wrappedData := MetafieldResource{Metafield: &metafield}
resource := new(MetafieldResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Metafield, err
}
// Delete an existing metafield
func (s *MetafieldServiceOp) Delete(metafieldID int64) error {
prefix := MetafieldPathPrefix(s.resource, s.resourceID)
return s.client.Delete(fmt.Sprintf("%s/%d.json", prefix, metafieldID))
}