-
Notifications
You must be signed in to change notification settings - Fork 3
/
barcode.go
30 lines (27 loc) · 1.16 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
package passbook
import (
"encoding/json"
"errors"
)
// Barcode Dictionary: Information about a pass’s barcode.
type Barcode struct {
Format BarcodeFormat `json:"format"` // Barcode format.
Message string `json:"message"` // Message or payload to be displayed as a barcode.
MessageEncoding string `json:"messageEncoding"` // Textencodingthatisusedtoconvertthemessage from the string representation to a data representation to render the barcode.
AltText string `json:"altText,omitempty"` // Text displayed near the barcode. For example, a human-readable version of the barcode data in case the barcode doesn’t scan.
}
func (b Barcode) Marshal() ([]byte, error) {
if b.Format != PKBarcodeFormatQR &&
b.Format != PKBarcodeFormatPDF417 &&
b.Format != PKBarcodeFormatAztec {
return nil, errors.New("Barcode format must be one of the following values: " +
"PKBarcodeFormatQR, PKBarcodeFormatPDF417, PKBarcodeFormatAztec")
}
if b.Message == "" {
return nil, errors.New("Message of barcode must be set")
}
if b.MessageEncoding == "" {
b.MessageEncoding = "iso-8859-1"
}
return json.Marshal(b)
}