ducky/devices

Paddy 2016-01-02 Parent:c24a6c5fcd8c

20:ed1b5ba69551 Go to Latest

ducky/devices/vendor/code.secondbit.org/trout.hg/trie.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 trout
3 import (
4 "net/http"
5 "sort"
6 "sync"
7 )
9 type trie struct {
10 branch *branch
11 sync.RWMutex
12 }
14 func (t *trie) match(input []string) ([]int, bool) {
15 if t.branch == nil {
16 t.branch = &branch{}
17 }
18 b := t.branch
19 path := []int{}
20 offset := 0
21 num := len(input)
22 for i := 0; i < num; i++ {
23 // if we're on a nil branch, we're at the end of our line
24 if b == nil {
25 return path, false
26 }
27 offset = pickNextBranch(b, offset, input[i])
28 if offset == -1 {
29 if len(path) == 0 {
30 // can't find it, bail
31 return path, false
32 }
33 // no match, backup
34 path, offset = backup(path)
35 offset = offset + 1 // we want the next index from the previously matched one
36 b = b.parent // we need to be picking a branch from our parent, again
37 i = i - 2 // back up to the choice before the one that got us here
38 } else {
39 path = append(path, offset)
40 b = b.children[offset]
41 offset = 0
42 }
43 }
44 return path, true
45 }
47 type branch struct {
48 parent *branch
49 children []*branch
50 key string
51 isParam bool
52 methods map[string]http.Handler
53 }
55 func (b *branch) Less(i, j int) bool {
56 if b.children[i].key == b.children[j].key {
57 return b.children[j].isParam && !b.children[i].isParam
58 }
59 return b.children[i].key < b.children[j].key
60 }
62 func (b *branch) Len() int {
63 return len(b.children)
64 }
66 func (b *branch) Swap(i, j int) {
67 b.children[i], b.children[j] = b.children[j], b.children[i]
68 }
70 func (b *branch) addChild(key string, param bool) *branch {
71 child := &branch{key: key, parent: b, isParam: param}
72 if b.children == nil {
73 b.children = []*branch{child}
74 return child
75 }
76 b.children = append(b.children, child)
77 sort.Sort(b)
78 return child
79 }
81 func (b *branch) check(input string) bool {
82 if b.isParam && b.key != "" {
83 return true
84 }
85 if b.key == input {
86 return true
87 }
88 return false
89 }
91 func (b *branch) setHandler(method string, handler http.Handler) {
92 if b.methods == nil {
93 b.methods = map[string]http.Handler{}
94 }
95 b.methods[method] = handler
96 }
98 func pickNextBranch(b *branch, offset int, input string) int {
99 count := len(b.children)
100 for i := offset; i < count; i++ {
101 if b.children[i].check(input) {
102 return i
103 }
104 }
105 return -1
106 }
108 func backup(path []int) ([]int, int) {
109 last := len(path) - 1
110 pos := path[last]
111 path = path[:last]
112 return path, pos
113 }