forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metafield.go
171 lines (141 loc) · 7.16 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
168
169
170
171
package goshopify
import (
"context"
"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(context.Context, interface{}) ([]Metafield, error)
Count(context.Context, interface{}) (int, error)
Get(context.Context, uint64, interface{}) (*Metafield, error)
Create(context.Context, Metafield) (*Metafield, error)
Update(context.Context, Metafield) (*Metafield, error)
Delete(context.Context, uint64) 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(context.Context, uint64, interface{}) ([]Metafield, error)
CountMetafields(context.Context, uint64, interface{}) (int, error)
GetMetafield(context.Context, uint64, uint64, interface{}) (*Metafield, error)
CreateMetafield(context.Context, uint64, Metafield) (*Metafield, error)
UpdateMetafield(context.Context, uint64, Metafield) (*Metafield, error)
DeleteMetafield(context.Context, uint64, uint64) error
}
// MetafieldServiceOp handles communication with the metafield
// related methods of the Shopify API.
type MetafieldServiceOp struct {
client *Client
resource string
resourceId uint64
}
// MetafieldType
//
// Docs: https://shopify.dev/docs/api/admin-rest/2023-07/resources/metafield#resource-object
type MetafieldType string
// https://shopify.dev/docs/api/admin-rest/2023-07/resources/metafield#resource-object
const (
// MetafieldTypeBoolean True or false.
MetafieldTypeBoolean MetafieldType = "boolean"
// MetafieldTypeColor A hexidecimal color code, #fff123.
MetafieldTypeColor MetafieldType = "color" // #fff123
// MetafieldTypeDate ISO601, YYYY-MM-DD.
MetafieldTypeDate MetafieldType = "date"
// MetafieldTypeDatetime ISO8601, YYYY-MM-DDTHH:MM:SS.
MetafieldTypeDatetime MetafieldType = "date_time"
// MetafieldTypeDimension JSON, {"value:" 25.0, "unit": "cm"}.
MetafieldTypeDimension MetafieldType = "dimension"
// MetafieldTypeJSON {"ingredient": "flour", "amount": 0.3}.
MetafieldTypeJSON MetafieldType = "json"
// MetafieldTypeMoney JSON, {"amount": 5.99, "currency_code": "CAD"}.
MetafieldTypeMoney MetafieldType = "money"
// MetafieldTypeMultiLineTextField lines of text separated with newline characters.
MetafieldTypeMultiLineTextField MetafieldType = "multi_line_text_field"
// MetafieldTypeNumberDecimal 10.4.
MetafieldTypeNumberDecimal MetafieldType = "number_decimal"
// MetafieldTypeNumberInteger 10.
MetafieldTypeNumberInteger MetafieldType = "number_integer"
// MetafieldTypeRating JSON, {"value": "3.5", "scale_min": "1.0", "scale_max": "5.0"}.
MetafieldTypeRating MetafieldType = "rating"
// MetafieldTypeRichTextField JSON, {"type": "root","children": [{"type": "paragraph","children": [{"type": "text","value": "Bold text.","bold": true}]}]}.
MetafieldTypeRichTextField MetafieldType = "rich_text_field"
// MetafieldTypeSingleLineTextField A single line of text. Do not use for numbers or dates, use correct type instead!
MetafieldTypeSingleLineTextField MetafieldType = "single_line_text_field"
// MetafieldTypeURL https, http, mailto, sms, or tel.
MetafieldTypeURL MetafieldType = "url"
// MetafieldTypeVolume JSON, {"value:" 20.0, "unit": "ml"}.
MetafieldTypeVolume MetafieldType = "volume"
// MetafieldTypeWeight 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 uint64 `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 uint64 `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(ctx context.Context, options interface{}) ([]Metafield, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceId)
path := fmt.Sprintf("%s.json", prefix)
resource := new(MetafieldsResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Metafields, err
}
// Count metafields
func (s *MetafieldServiceOp) Count(ctx context.Context, options interface{}) (int, error) {
prefix := MetafieldPathPrefix(s.resource, s.resourceId)
path := fmt.Sprintf("%s/count.json", prefix)
return s.client.Count(ctx, path, options)
}
// Get individual metafield
func (s *MetafieldServiceOp) Get(ctx context.Context, metafieldId uint64, 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(ctx, path, resource, options)
return resource.Metafield, err
}
// Create a new metafield
func (s *MetafieldServiceOp) Create(ctx context.Context, 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(ctx, path, wrappedData, resource)
return resource.Metafield, err
}
// Update an existing metafield
func (s *MetafieldServiceOp) Update(ctx context.Context, 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(ctx, path, wrappedData, resource)
return resource.Metafield, err
}
// Delete an existing metafield
func (s *MetafieldServiceOp) Delete(ctx context.Context, metafieldId uint64) error {
prefix := MetafieldPathPrefix(s.resource, s.resourceId)
return s.client.Delete(ctx, fmt.Sprintf("%s/%d.json", prefix, metafieldId))
}