ducky/devices

Paddy 2016-01-02 Parent:a700ede02f91

20:ed1b5ba69551 Go to Latest

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

Add updating devices to apiv1. We needed a way to be able to update devices after they were created. This is supported in the devices package, we just needed to expose it using apiv1 endpoints. In doing so, it became apparent that allowing users to change the Owner of their Devices wasn't properly thought through, and pending a reason to use it, I'm just removing it. The biggest issue came when trying to return usable error messages; we couldn't distinguish between "you don't own the device you're trying to update" and "you're not allowed to change the owner of the device". I also couldn't figure out _who should be able to_ change the owner of the device, which is generally an indication that I'm building a feature before I have a use case for it. To support this change, the apiv1.DeviceChange type needed its Owner property removed. I also needed to add deviceFromAPI and devicesFromAPI helpers to return devices.Device types from apiv1.Device types. There's now a new validateDeviceUpdate helper that checks to ensure that a device update request is valid and the user has the appropriate permissions. The createRequest type now accepts a slice of Devices, not a slice of DeviceChanges, because we want to pass the Owner in. A new updateRequest type is created, which accepts a DeviceChange to apply. A new handleUpdateDevice handler is created, which is assigned to the endpoint for PATCH requests against a device ID. It checks that the user is logged in, the Device they're trying to update exists, and that it's a valid update. If all of that is true, the device is updated and the updated device is returned. Finally, we had to add two new scopes to support new functionality: ScopeUpdateOtherUserDevices allows a user to update other user's devices, and ScopeUpdateLastSeen allows a user to update the LastSeen property of a device. Pending some better error messages, this should be a full implementation of updating a device, which leaves only the deletion endpoint to deal with.

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 }