-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase_impl.go
68 lines (56 loc) · 1.25 KB
/
database_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package go_android_utils
import (
"context"
"encoding/json"
"errors"
"io"
"os"
)
// Refactor how we store and retrieve devices, allow for easy backend switching
type AUDOptions map[string]interface{}
type AUDDatabaseIFace interface {
GetDevice(ctx context.Context, parameters ...AUDOptions) (*Device, error)
}
var (
ErrNoDeviceFound = errors.New("no device found")
)
// Example implementation of a JSON backed DB
type JSONAUDDatabase struct {
Devices []*Device
}
func NewJSONAUDDevice(fileLocation string) (AUDDatabaseIFace, error) {
f, err := os.Open(fileLocation)
var (
fBytes []byte
result = &JSONAUDDatabase{Devices: []*Device{}}
)
if err == nil {
fBytes, err = io.ReadAll(f)
if err == nil {
err = json.Unmarshal(fBytes, &result.Devices)
}
}
return result, err
}
func (d *JSONAUDDatabase) GetDevice(ctx context.Context, parameters ...AUDOptions) (*Device, error) {
// var (
// params AUDOptions
//
// realMinSDK int
// )
// if len(parameters) > 0 {
// params = parameters[0]
// }
// minSDK, okSDK := params["minSDK"]
// if okSDK {
// realMinSDK = minSDK.(int)
// }
var (
result *Device
err error
)
for result == nil && err == nil {
result = d.Devices[randomInt(0, len(d.Devices))]
}
return result, err
}