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
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 }