-
Notifications
You must be signed in to change notification settings - Fork 17
/
ndef.go
43 lines (34 loc) · 848 Bytes
/
ndef.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
package main
import (
"bytes"
"encoding/binary"
"html/template"
"github.com/hsanjuan/go-ndef"
)
func buildURL(urlTemplate string, vars interface{}) (string, error) {
tpl, err := template.New("").Parse(urlTemplate)
if err != nil {
return "", err
}
urlBuf := bytes.NewBufferString("")
err = tpl.Execute(urlBuf, vars)
if err != nil {
return "", err
}
return urlBuf.String(), nil
}
func buildNdefDataWithURL(urlTemplate string, vars interface{}) (string, []byte, error) {
url, err := buildURL(urlTemplate, vars)
if err != nil {
return "", nil, err
}
msg := ndef.NewMessageFromRecords(ndef.NewURIRecord(url))
msgBuf, err := msg.Marshal()
if err != nil {
return "", nil, err
}
buf := make([]byte, len(msgBuf)+2)
binary.BigEndian.PutUint16(buf[0:], uint16(len(msgBuf)))
copy(buf[2:], msgBuf)
return url, buf, nil
}