ducky/devices
ducky/devices/context.go
Move DeviceType to its own file, add helper and constants. Make a device_type.go file, to avoid a mess in the devices.go file. Move the DeviceType definition over to the new file. Also, while we're here, set up a few of the contstants we know we'll need. These are the DeviceTypes we intend to support, such as Android phones, Android tablets, and Chrome extensions. Also, set up a helper method that will determine whether a DeviceType is "valid", i.e. if we have a constant defined for it. DeviceTypes, in general, are mostly intended to be used (at the moment, at least) to customise how we display devices to users. Basically, they allow us to display an at least semi-accurate depiction of the device.
| paddy@0 | 1 package devices |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "errors" |
| paddy@0 | 5 |
| paddy@0 | 6 "golang.org/x/net/context" |
| paddy@0 | 7 ) |
| paddy@0 | 8 |
| paddy@0 | 9 const ( |
| paddy@0 | 10 storerKey = "code.secondbit.org/ducky/devices.hg#Storer" |
| paddy@0 | 11 ) |
| paddy@0 | 12 |
| paddy@0 | 13 var ( |
| paddy@0 | 14 // ErrNoStorerSet is returned when the Context has no Storer set in it. |
| paddy@0 | 15 ErrNoStorerSet = errors.New("storerKey not set in Context") |
| paddy@0 | 16 // ErrStorerKeyNotStorer is returned when there's a value in the Context for storerKey, but it's not a Storer. |
| paddy@0 | 17 ErrStorerKeyNotStorer = errors.New("the value for storerKey does not fulfill the Storer interface") |
| paddy@0 | 18 ) |
| paddy@0 | 19 |
| paddy@0 | 20 func getStorer(c context.Context) (Storer, error) { |
| paddy@0 | 21 val := c.Value(storerKey) |
| paddy@0 | 22 if val == nil { |
| paddy@0 | 23 return nil, ErrNoStorerSet |
| paddy@0 | 24 } |
| paddy@0 | 25 storer, ok := val.(Storer) |
| paddy@0 | 26 if !ok { |
| paddy@0 | 27 return nil, ErrStorerKeyNotStorer |
| paddy@0 | 28 } |
| paddy@0 | 29 return storer, nil |
| paddy@0 | 30 } |