-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
119 lines (100 loc) · 2.84 KB
/
parser.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
package applehealth
import (
"encoding/xml"
"fmt"
"sync/atomic"
"github.com/AlekSi/applehealth/healthkit"
)
// Parser is responsible for parsing Apple Health XML stream.
//
// Most users should use Unmarshaler instead.
type Parser struct {
DisallowUnhandledElements bool // if true, Next() will return UnhandledElementError for unhandled data
d *xml.Decoder
offset int64
meta *healthkit.Meta
}
// UnhandledElementError is returned by the Parser from the Next() method
// if DisallowUnhandledElements is true and unhandled data is encountered.
type UnhandledElementError struct {
Name string
}
func (u *UnhandledElementError) Error() string {
return fmt.Sprintf("unhandled element %s", u.Name)
}
// NewParser creates a new parser with given XML decoder.
//
// Most users should use NewUnmarshaler instead.
func NewParser(d *xml.Decoder) *Parser {
return &Parser{
d: d,
meta: new(healthkit.Meta),
}
}
// Meta returns parsed metadata after the first call to Next().
func (p *Parser) Meta() *healthkit.Meta {
return p.meta
}
// InputOffset returns XML stream input offset. Unlike calling xml.Decoder.InputOffset directly,
// that method can be called concurrently with Next().
func (p *Parser) InputOffset() int64 {
return atomic.LoadInt64(&p.offset)
}
func (p *Parser) updateOffset() {
atomic.StoreInt64(&p.offset, p.d.InputOffset())
}
var newData = map[string]func() healthkit.Data{
"Record": func() healthkit.Data { return new(healthkit.Record) },
"Correlation": func() healthkit.Data { return new(healthkit.Correlation) },
"Workout": func() healthkit.Data { return new(healthkit.Workout) },
"ActivitySummary": func() healthkit.Data { return new(healthkit.ActivitySummary) },
}
// Next returns the next health data object, or error
// (io.EOF, *UnhandledElementError, or XML parsing error).
func (p *Parser) Next() (healthkit.Data, error) {
defer p.updateOffset()
for {
t, err := p.d.Token()
if err != nil {
return nil, err
}
se, ok := t.(xml.StartElement)
if !ok {
continue
}
name := se.Name.Local
switch name {
case "HealthData":
// not using DecodeElement to avoid reading the whole huge element
for _, attr := range se.Attr {
expected := xml.Name{Local: "locale"}
if attr.Name == expected {
p.meta.Locale = attr.Value
}
}
case "ExportDate":
var ed healthkit.ExportDate
if err = p.d.DecodeElement(&ed, &se); err != nil {
return nil, err
}
p.meta.ExportDate = ed
case "Me":
var m healthkit.Me
if err = p.d.DecodeElement(&m, &se); err != nil {
return nil, err
}
p.meta.Me = m
default:
if ndf := newData[name]; ndf != nil {
d := ndf()
if err = p.d.DecodeElement(d, &se); err != nil {
return nil, err
}
return d, nil
}
if p.DisallowUnhandledElements {
return nil, &UnhandledElementError{Name: name}
}
}
}
}