-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
241 lines (209 loc) · 5.42 KB
/
Copy pathmodels.go
File metadata and controls
241 lines (209 loc) · 5.42 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"image/png"
"strings"
"github.com/mect/go-escpos"
)
type PrintRequest struct {
Receipt []ReceiptItem `json:"receipt"`
Quantity int `json:"quantity,omitempty"`
}
// ReceiptItem represents any type of item that can appear on a receipt
type ReceiptItem interface{}
// RawReceiptItem is used for JSON unmarshaling with type discrimination
type RawReceiptItem struct {
Type string `json:"type"`
Data json.RawMessage `json:",inline"`
}
// Custom unmarshaling for PrintRequest
func (pr *PrintRequest) UnmarshalJSON(data []byte) error {
var raw struct {
Receipt []json.RawMessage `json:"receipt"`
Quantity int `json:"quantity,omitempty"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
// Set quantity with default value of 1 if not specified
pr.Quantity = raw.Quantity
if pr.Quantity <= 0 {
pr.Quantity = 1
}
pr.Receipt = make([]ReceiptItem, len(raw.Receipt))
for i, itemData := range raw.Receipt {
// First, extract the type field
var typeExtractor struct {
Type string `json:"type"`
}
if err := json.Unmarshal(itemData, &typeExtractor); err != nil {
return fmt.Errorf("error extracting type from item %d: %v", i, err)
}
// Then unmarshal the full object based on type
switch typeExtractor.Type {
case "line":
var line Line
if err := json.Unmarshal(itemData, &line); err != nil {
return fmt.Errorf("error unmarshaling line: %v", err)
}
pr.Receipt[i] = line
case "barcode":
var barcode Barcode
if err := json.Unmarshal(itemData, &barcode); err != nil {
return fmt.Errorf("error unmarshaling barcode: %v", err)
}
pr.Receipt[i] = barcode
case "qr":
var qr QRCode
if err := json.Unmarshal(itemData, &qr); err != nil {
return fmt.Errorf("error unmarshaling qr: %v", err)
}
pr.Receipt[i] = qr
case "image":
var image Image
if err := json.Unmarshal(itemData, &image); err != nil {
return fmt.Errorf("error unmarshaling image: %v", err)
}
pr.Receipt[i] = image
case "text":
var text Text
if err := json.Unmarshal(itemData, &text); err != nil {
return fmt.Errorf("error unmarshaling text: %v", err)
}
pr.Receipt[i] = text
case "feed":
var feed Feed
if err := json.Unmarshal(itemData, &feed); err != nil {
return fmt.Errorf("error unmarshaling feed: %v", err)
}
pr.Receipt[i] = feed
default:
return fmt.Errorf("unknown receipt item type: %s", typeExtractor.Type)
}
}
return nil
}
type FontType string
const (
FontA FontType = "A"
FontB FontType = "B"
FontC FontType = "C"
)
type AlignmentType string
const (
AlignLeft AlignmentType = "left"
AlignRight AlignmentType = "right"
AlignCenter AlignmentType = "center"
)
func (a *AlignmentType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case "left", "right", "center":
*a = AlignmentType(s)
return nil
default:
return fmt.Errorf("invalid alignment: %s. Must be left, right, or center", s)
}
}
func (a AlignmentType) ToEscposAlignment() escpos.Alignment {
switch a {
case AlignLeft:
return escpos.AlignLeft
case AlignRight:
return escpos.AlignRight
case AlignCenter:
return escpos.AlignCenter
default:
return escpos.AlignLeft // default
}
}
type BarcodeType string
const (
BarcodeUPCA BarcodeType = "UPCA"
BarcodeUPCE BarcodeType = "UPCE"
BarcodeEAN13 BarcodeType = "EAN13"
BarcodeEAN8 BarcodeType = "EAN8"
BarcodeCODE39 BarcodeType = "CODE39"
BarcodeCODE128 BarcodeType = "CODE128"
)
func (b *BarcodeType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case "UPCA", "UPCE", "EAN13", "EAN8", "CODE39", "CODE128":
*b = BarcodeType(s)
return nil
default:
return fmt.Errorf("invalid barcode type: %s", s)
}
}
// this gets called by the handler when converting to ESC/POS so it uses that packages types
func (t BarcodeType) ToEscposBarcodeType() escpos.BarcodeType {
switch t {
case "CODABAR":
return escpos.BarcodeTypeCODABAR
case "CODE128":
return escpos.BarcodeTypeCODE128
case "CODE39":
return escpos.BarcodeTypeCODE39
case "EAN13":
return escpos.BarcodeTypeEAN13
case "EAN8":
return escpos.BarcodeTypeEAN8
case "ITF":
return escpos.BarcodeTypeITF
case "UPCA":
return escpos.BarcodeTypeUPCA
case "UPCE":
return escpos.BarcodeTypeUPCE
}
return escpos.BarcodeTypeCODABAR // shits fucked if we are here
}
func (i *Image) UnmarshalJSON(data []byte) error {
var aux struct {
Data string `json:"data"`
DitherMode string `json:"dither_mode"`
Alignment AlignmentType `json:"alignment"`
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i.Data = aux.Data
i.DitherMode = strings.ToLower(aux.DitherMode)
i.Alignment = aux.Alignment
b64 := aux.Data
if after, ok := strings.CutPrefix(b64, "data:image/png;base64,"); ok {
b64 = after
}
decoded, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return err
}
img, err := png.Decode(bytes.NewReader(decoded))
if err != nil {
return err
}
i.img = img
return nil
}
// Helper functions to convert custom types to escpos types
func (f FontType) ToEscposFont() escpos.Font {
switch f {
case FontA:
return escpos.FontA
case FontB:
return escpos.FontB
case FontC:
return escpos.FontC
default:
return escpos.FontA // default
}
}