-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.go
212 lines (171 loc) · 5.76 KB
/
file.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
204
205
206
207
208
209
210
211
212
package figma
import (
"context"
"encoding/json"
)
func UnmarshalFile(data []byte) (*File, error) {
var r File
err := json.Unmarshal(data, &r)
return &r, err
}
func (r *File) Marshal() ([]byte, error) {
return json.Marshal(r)
}
func UnmarshalFileNodes(data []byte) (*FileNodes, error) {
var r FileNodes
err := json.Unmarshal(data, &r)
return &r, err
}
func (r *FileNodes) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type File struct {
Document Document `json:"document"`
Components Components `json:"components"`
SchemaVersion int64 `json:"schemaVersion"`
Styles map[string]StyleValue `json:"styles"`
Name string `json:"name"`
LastModified string `json:"lastModified"`
ThumbnailURL string `json:"thumbnailUrl"`
Version string `json:"version"`
}
type FileNode struct {
Document Document `json:"document"`
Components Components `json:"components"`
SchemaVersion int64 `json:"schemaVersion"`
Styles map[string]StyleValue `json:"styles"`
}
type Components struct {
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
type Color struct {
R float64 `json:"r"`
G float64 `json:"g"`
B float64 `json:"b"`
A int64 `json:"a"`
}
type Document struct {
ID string `json:"id"`
Name string `json:"name"`
Type Type `json:"type"`
BlendMode BlendMode `json:"blendMode"`
Opacity *float64 `json:"opacity,omitempty"`
AbsoluteBoundingBox AbsoluteBoundingBox `json:"absoluteBoundingBox"`
Constraints Constraints `json:"constraints"`
Fills []FillElement `json:"fills"`
Strokes []interface{} `json:"strokes"`
StrokeWeight int64 `json:"strokeWeight"`
StrokeAlign StrokeAlign `json:"strokeAlign"`
ExportSettings []interface{} `json:"exportSettings"`
Effects []interface{} `json:"effects"`
Characters *string `json:"characters,omitempty"`
Style *Style `json:"style,omitempty"`
CharacterStyleOverrides []interface{} `json:"characterStyleOverrides"`
StyleOverrideTable *Components `json:"styleOverrideTable,omitempty"`
StrokeJoin *StrokeJoin `json:"strokeJoin,omitempty"`
StrokeMiterAngle interface{} `json:"strokeMiterAngle"`
Styles *Styles `json:"styles,omitempty"`
CornerRadius *int64 `json:"cornerRadius,omitempty"`
Children []Document `json:"children,omitempty"`
BackgroundColor Color `json:"backgroundColor"`
PrototypeStartNodeID interface{} `json:"prototypeStartNodeID"`
}
type AbsoluteBoundingBox struct {
X int64 `json:"x"`
Y int64 `json:"y"`
Width int64 `json:"width"`
Height int64 `json:"height"`
}
type Constraints struct {
Vertical Vertical `json:"vertical"`
Horizontal Horizontal `json:"horizontal"`
}
type FillElement struct {
BlendMode BlendMode `json:"blendMode"`
Type FillType `json:"type"`
Color Color `json:"color"`
}
type Style struct {
FontFamily string `json:"fontFamily"`
FontPostScriptName interface{} `json:"fontPostScriptName"`
FontWeight int64 `json:"fontWeight"`
FontSize int64 `json:"fontSize"`
TextAlignHorizontal Horizontal `json:"textAlignHorizontal"`
TextAlignVertical Vertical `json:"textAlignVertical"`
LetterSpacing int64 `json:"letterSpacing"`
LineHeightPx float64 `json:"lineHeightPx"`
LineHeightPercent int64 `json:"lineHeightPercent"`
LineHeightPercentFontSize float64 `json:"lineHeightPercentFontSize"`
LineHeightUnit string `json:"lineHeightUnit"`
}
type Styles struct {
Fill string `json:"fill"`
}
type StyleValue struct {
Key string `json:"key"`
Name string `json:"name"`
StyleType StyleType `json:"styleType"`
}
type BlendMode string
const (
PassThrough BlendMode = "PASS_THROUGH"
Normal BlendMode = "Normal"
)
type Horizontal string
const (
Left Horizontal = "LEFT"
)
type Vertical string
const (
Top Vertical = "TOP"
)
type FillType string
const (
Solid FillType = "SOLID"
)
type StrokeAlign string
const (
Center StrokeAlign = "CENTER"
Outside StrokeAlign = "OUTSIDE"
)
type StrokeJoin string
const (
Bevel StrokeJoin = "BEVEL"
)
type Type string
const (
Rectangle Type = "RECTANGLE"
Text Type = "TEXT"
)
type StyleType string
const (
Fill StyleType = "FILL"
)
type FileNodes struct {
Name string `json:"name"`
LastModified string `json:"lastModified"`
ThumbnailURL string `json:"thumbnailUrl"`
Version string `json:"version"`
Nodes map[string]FileNode `json:"nodes,omitempty"`
}
func (c *Client) GetFile(ctx context.Context, fileKey string) (*File, error) {
req, _ := c.newRequest(ctx, "GET", "/v1/files/"+fileKey, nil)
res, err := c.send(ctx, req)
if err != nil {
return nil, err
}
return UnmarshalFile(res)
}
func (c *Client) GetFileNode(ctx context.Context, fileKey, ids string) (*FileNodes, error) {
req, _ := c.newRequest(ctx, "GET", "v1/files/"+fileKey+"/nodes", nil)
q := req.URL.Query()
q.Add("ids", ids)
req.URL.RawQuery = q.Encode()
res, err := c.send(ctx, req)
if err != nil {
return nil, err
}
return UnmarshalFileNodes(res)
}