aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/schedule/parse.go
blob: b0ed8df4be3ed95a11844aee8f4e1abfa476cfd1 (plain)
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
package schedule

import (
	"encoding/xml"
	"fmt"
	"time"
)

type schedule struct {
	XMLName    xml.Name   `xml:"schedule"`
	Conference conference `xml:"conference"`
	Tracks     []track    `xml:"tracks>track"`
	Days       []day      `xml:"day"`
}

type conference struct {
	Title            string `xml:"title"`
	Venue            string `xml:"venue"`
	City             string `xml:"city"`
	Start            string `xml:"start"`
	End              string `xml:"end"`
	Days             int    `xml:"days"`
	DayChange        string `xml:"day_change"`
	TimeslotDuration string `xml:"timeslot_duration"`
	BaseURL          string `xml:"base_url"`
	TimeZoneName     string `xml:"time_zone_name"`
}

type track struct {
	Name string `xml:",chardata"`
}

type day struct {
	Date  string `xml:"date,attr"`
	Start string `xml:"start,attr"`
	End   string `xml:"end,attr"`
	Rooms []room `xml:"room"`
}

type room struct {
	Name   string  `xml:"name,attr"`
	Events []event `xml:"event"`
}

type event struct {
	ID          int32        `xml:"id,attr"`
	GUID        string       `xml:"guid,attr"`
	Date        string       `xml:"date"`
	Start       string       `xml:"start"`
	Duration    string       `xml:"duration"`
	Room        string       `xml:"room"`
	URL         string       `xml:"url"`
	Track       string       `xml:"track"`
	Type        string       `xml:"type"`
	Title       string       `xml:"title"`
	Abstract    string       `xml:"abstract"`
	Persons     []person     `xml:"persons>person"`
	Attachments []attachment `xml:"attachments>attachment"`
	Links       []link       `xml:"links>link"`
}

type person struct {
	ID   int    `xml:"id,attr"`
	Name string `xml:",chardata"`
}

type attachment struct {
	Type string `xml:"id,attr"`
	Href string `xml:"href,attr"`
	Name string `xml:",chardata"`
}

type link struct {
	Href string `xml:"href,attr"`
	Name string `xml:",chardata"`
}

func (dst *Schedule) Scan(src schedule) error {
	dst.Conference.Scan(src.Conference)

	dst.Tracks = make([]Track, len(src.Tracks))
	for i := range src.Tracks {
		dst.Tracks[i].Scan(src.Tracks[i])
	}
	dst.Days = make([]Day, len(src.Days))
	for i := range src.Days {
		if err := dst.Days[i].Scan(src.Days[i]); err != nil {
			return fmt.Errorf("failed to scan day: %w", err)
		}
	}
	return nil
}

func (dst *Conference) Scan(src conference) {
	dst.Title = src.Title
	dst.Venue = src.Venue
	dst.City = src.City
	dst.Start = src.Start
	dst.End = src.End
	dst.Days = src.Days
	dst.DayChange = src.DayChange
	dst.TimeslotDuration = src.TimeslotDuration
	dst.BaseURL = src.BaseURL
	dst.TimeZoneName = src.TimeZoneName
}

func (dst *Track) Scan(src track) {
	dst.Name = src.Name
}

func (dst *Day) Scan(src day) error {
	dst.Date = src.Date

	start, err := time.Parse(time.RFC3339, src.Start)
	if err != nil {
		return fmt.Errorf("failed to parse start time: %w", err)
	}
	end, err := time.Parse(time.RFC3339, src.End)
	if err != nil {
		return fmt.Errorf("failed to parse end time: %w", err)
	}

	dst.Start = start
	dst.End = end

	dst.Rooms = make([]Room, len(src.Rooms))
	for i := range src.Rooms {
		dst.Rooms[i].Scan(src.Rooms[i])
	}
	return nil
}

func (dst *Room) Scan(src room) {
	dst.Name = src.Name

	dst.Events = make([]Event, len(src.Events))
	for i := range src.Events {
		dst.Events[i].Scan(src.Events[i])
	}
}

func (dst *Event) Scan(src event) error {
	dst.ID = src.ID
	dst.GUID = src.GUID
	dst.Date = src.Date

	duration, err := parseDuration(src.Duration)
	if err != nil {
		return err
	}
	start, err := time.Parse(time.RFC3339, src.Date)
	if err != nil {
		start = time.Unix(0, 0)
	}
	dst.Start = start
	dst.End = start.Add(time.Minute * time.Duration(duration))

	dst.Room = src.Room
	dst.URL = src.URL
	dst.Track = src.Track
	dst.Type = src.Type
	dst.Title = src.Title
	dst.Abstract = src.Abstract

	dst.Persons = make([]Person, len(src.Persons))
	for i := range src.Persons {
		dst.Persons[i].Scan(src.Persons[i])
	}

	dst.Attachments = make([]Attachment, len(src.Attachments))
	for i := range src.Attachments {
		dst.Attachments[i].Scan(src.Attachments[i])
	}

	dst.Links = make([]Link, len(src.Links))
	for i := range src.Links {
		dst.Links[i].Scan(src.Links[i])
	}

	return nil
}

func (dst *Person) Scan(src person) {
	dst.ID = src.ID
	dst.Name = src.Name
}

func (dst *Attachment) Scan(src attachment) {
	dst.Type = src.Type
	dst.Href = src.Href
	dst.Name = src.Name
}

func (dst *Link) Scan(src link) {
	dst.Href = src.Href
	dst.Name = src.Name
}

func parseDuration(duration string) (int32, error) {
	d, err := time.Parse("15:04", duration)
	if err != nil {
		return 0, err
	}
	return int32(d.Minute() + d.Hour()*60), nil
}