ducky/devices

Paddy 2015-12-19 Parent:a700ede02f91

17:ad63b888f899 Go to Latest

ducky/devices/vendor/code.secondbit.org/pqarrays.hg/parser.go

Fix JSON pointers in errors when creating devices. Our JSON pointers used to point at the root of the document, but the properties were actually in objects in an array keyed off of "devices", so we had to update the field property of our errors to match. While we were in there, we fixed the "insufficient" error for a Device's name to be "missing" when the name is an empty string. So far, the only way for a Device's name for it to be insufficient is _for it to be the empty string_, but if in the future we raise the minimum length of the Device name, there will be a distinction and I'd like the code to recognise it.

History
1 package pqarrays
3 import (
4 "errors"
5 )
7 func parse(l *lexer) ([]*string, error) {
8 var parsed []*string
9 pchan := make(chan *string)
10 errchan := make(chan error)
11 done := make(chan struct{})
12 go runParse(l, pchan, errchan, done)
13 for {
14 select {
15 case err := <-errchan:
16 return parsed, err
17 case item := <-pchan:
18 parsed = append(parsed, item)
19 case <-done:
20 return parsed, nil
21 }
22 }
23 }
25 func runParse(l *lexer, parsed chan *string, err chan error, done chan struct{}) {
26 var state parseFunc = parseStart
27 for {
28 var e error
29 state, e = state(l, parsed)
30 if e != nil {
31 err <- e
32 break
33 }
34 if state == nil {
35 break
36 }
37 }
38 close(done)
39 }
41 type parseFunc func(*lexer, chan *string) (parseFunc, error)
43 func parseEOF(l *lexer, parsed chan *string) (parseFunc, error) {
44 tok := l.nextToken()
45 if tok.typ == tokenWhitespace {
46 tok = l.nextToken()
47 }
48 if tok.typ != tokenEOF {
49 return nil, errors.New("expected EOF, got " + tok.typ.String())
50 }
51 return nil, nil
52 }
54 func parseStringOrNull(l *lexer, parsed chan *string) (parseFunc, error) {
55 tok := l.nextToken()
56 if tok.typ == tokenWhitespace {
57 tok = l.nextToken()
58 } else if tok.typ == tokenString {
59 parsed <- &tok.val
60 return parseSeparatorOrDelim, nil
61 } else if tok.typ == tokenNull {
62 parsed <- nil
63 return parseSeparatorOrDelim, nil
64 }
65 return nil, errors.New("expected string, got " + tok.typ.String())
66 }
68 func parseSeparatorOrDelim(l *lexer, parsed chan *string) (parseFunc, error) {
69 tok := l.nextToken()
70 if tok.typ == tokenWhitespace {
71 return parseSeparatorOrDelim, nil
72 } else if tok.typ == tokenSeparator {
73 return parseStringOrNull, nil
74 } else if tok.typ == tokenArrayEnd {
75 return parseEOF, nil
76 }
77 return nil, errors.New("expected separator or delim, got " + tok.typ.String())
78 }
80 func parseStart(l *lexer, parsed chan *string) (parseFunc, error) {
81 tok := l.nextToken()
82 if tok.typ == tokenWhitespace {
83 return parseStart, nil
84 } else if tok.typ == tokenArrayStart {
85 return parseStringOrNull, nil
86 }
87 return nil, errors.New("expected separator or delim, got " + tok.typ.String())
88 }