-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
83 lines (76 loc) · 1.88 KB
/
parse.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
package main
import (
"encoding/json"
"io"
"log"
"os"
"sync"
)
// readByteSkippingSpace() reads through an io.Reader until a character that is
// not whitespace is encountered
func readByteSkippingSpace(r io.Reader) (b byte, err error) {
buf := make([]byte, 1)
for {
_, err := r.Read(buf)
if err != nil {
return 0, err
}
b := buf[0]
switch b {
// Only handling ASCII white space for now
case ' ', '\t', '\n', '\v', '\f', '\r':
continue
default:
return b, nil
}
}
}
// parseCourses() reads in 'jsonFileName' and parses courses while sending them down
// the 'cChan' channel for processing. 'wg' is marked as done when the end of the
// json list is found.
func parseCourses(jsonFileName string, cChan chan Course, wg *sync.WaitGroup) {
// open file for parsing
file, err := os.OpenFile(jsonFileName, os.O_RDONLY, 0644)
if err != nil {
log.Fatalf("Failed to open file, %s, with error: %s", jsonFileName, err.Error())
}
//defer file.Close() to close after all parsing is finished
r := io.Reader(file)
// Skip whitespace & '['
if b, err := readByteSkippingSpace(r); err != nil {
panic(err)
} else if b != '[' {
panic("Input is not a JSON array")
}
// now we start decoding each of the courses
var c Course
for {
c = Course{} // zero out the course for reuse
dec := json.NewDecoder(r)
if err := dec.Decode(&c); err == io.EOF {
log.Print("finished parsing json file")
return
} else if err != nil {
panic(err)
}
c.fill()
cChan <- c
r = io.MultiReader(dec.Buffered(), r)
if b, err := readByteSkippingSpace(r); err != nil {
log.Printf("broken, hit %s, err => %s", string(b), err.Error())
panic(err)
} else {
switch b {
case ',':
continue
case ']':
log.Print("done reading json list")
close(cChan)
wg.Done()
return
default:
panic("Invalid character in JSON data: " + string([]byte{b}))
}
}
}
}