-
Notifications
You must be signed in to change notification settings - Fork 4
/
barcode.go
54 lines (43 loc) · 1.9 KB
/
barcode.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
package moysklad
import (
"encoding/json"
"reflect"
)
// Barcode Штрихкод.
// Документация МойСклад: https://dev.moysklad.ru/doc/api/remap/1.2/dictionaries/#suschnosti-towar-towary-atributy-wlozhennyh-suschnostej-shtrihkody
type Barcode struct {
Type BarcodeType // Тип штрихкода
Value string // Штрихкод
}
func (b Barcode) String() string {
return Stringify(b)
}
// BarcodeType Тип штрихкода
type BarcodeType string
const (
BarcodeEAN13 BarcodeType = "ean13" // штрихкод в формате EAN13, если требуется создать штрихкод в формате EAN13
BarcodeEAN8 BarcodeType = "ean8" // штрихкод в формате EAN8, если требуется создать штрихкод в формате EAN8
BarcodeCode128 BarcodeType = "code128" // штрихкод в формате Code128, если требуется создать штрихкод в формате Code128
BarcodeGTIN BarcodeType = "gtin" // штрихкод в формате GTIN, если требуется создать штрихкод в формате GTIN. Валидируется на соответствие формату GS1
BarcodeUPC BarcodeType = "upc" // штрихкод в формате UPC, если требуется создать штрихкод в формате UPC
)
func (b Barcode) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{string(b.Type): b.Value})
}
func (b *Barcode) UnmarshalJSON(bytes []byte) (err error) {
tmp := map[string]string{}
if err = json.Unmarshal(bytes, &tmp); err != nil {
return
}
rv := reflect.ValueOf(tmp)
mapKeys := rv.MapKeys()
if len(mapKeys) > 0 {
key := mapKeys[0]
bType := BarcodeType(key.String())
bValue := (rv.MapIndex(key)).String()
b.Type = bType
b.Value = bValue
}
return
}
type Barcodes = Slice[Barcode]