ducky/devices

Paddy 2015-12-19

16:a700ede02f91 Go to Latest

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

Validate device creation. Update our uuid package to the latest, which is now based on the GitHub fork instead of the Google Code. Also, update our api package to its latest version, which now needs the pqarrays package as a dependency. We fleshed out the validateDeviceCreation. We now pass in the scopes we have (for broad access control) and the user ID (for fine-grained access control). This helper returns the first error it encounters, though it should probably return a slice so we can return multiple errors all at once. Before we even decode the request to create a Device, let's check if the user is even logged in. If we can't ascertain that or they're not, there's no point in even consuming the memory necessary to read the request, because we know we're not going to use it anyways. Finally actually validate the devices we're creating, and return an appropriate error for each error we can get. Also, the api.CheckScopes helper function now takes the scopes passed in as a string slice, and we have an api.GetScopes helper function to retrieve the scopes associated with the request. Let's not keep parsing that. We need two new scopes to control access for device creation; ScopeImport lets users import devices in and is pretty much admin access. ScopeCreateOtherUserDevices allows a user to create Devices that are owned by another user.

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