-
Notifications
You must be signed in to change notification settings - Fork 7
/
bin.go
83 lines (70 loc) · 2.32 KB
/
bin.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package protoboards
import (
"context"
"encoding/json"
"fmt"
cmp "github.com/snetsystems/cmp/backend"
)
//go:generate go-bindata -o bin_gen.go -ignore README|apps|.sh|go -pkg protoboards .
// BinProtoboardsStore represents a protoboards store using data generated by go-bindata
type BinProtoboardsStore struct {
Logger cmp.Logger
}
// All returns the set of all protoboards
func (s *BinProtoboardsStore) All(ctx context.Context) ([]cmp.Protoboard, error) {
names := AssetNames()
protoboards := make([]cmp.Protoboard, len(names))
for i, name := range names {
octets, err := Asset(name)
if err != nil {
s.Logger.
WithField("component", "protoboards").
WithField("name", name).
Error("Invalid protoboard: ", err)
return nil, cmp.ErrProtoboardInvalid
}
var protoboard cmp.Protoboard
if err = json.Unmarshal(octets, &protoboard); err != nil {
s.Logger.
WithField("component", "protoboards").
WithField("name", name).
Error("Unable to read protoboard:", err)
return nil, cmp.ErrProtoboardInvalid
}
protoboards[i] = protoboard
}
return protoboards, nil
}
// Add is not support by BinProtoboardsStore
func (s *BinProtoboardsStore) Add(ctx context.Context, protoboard cmp.Protoboard) (cmp.Protoboard, error) {
return cmp.Protoboard{}, fmt.Errorf("Add to BinProtoboardsStore not supported")
}
// Delete is not support by BinProtoboardsStore
func (s *BinProtoboardsStore) Delete(ctx context.Context, protoboard cmp.Protoboard) error {
return fmt.Errorf("Delete to BinProtoboardsStore not supported")
}
// Get retrieves protoboard if `ID` exists.
func (s *BinProtoboardsStore) Get(ctx context.Context, ID string) (cmp.Protoboard, error) {
protoboards, err := s.All(ctx)
if err != nil {
s.Logger.
WithField("component", "protoboards").
WithField("name", ID).
Error("Invalid protoboard: ", err)
return cmp.Protoboard{}, cmp.ErrProtoboardInvalid
}
for _, protoboard := range protoboards {
if protoboard.ID == ID {
return protoboard, nil
}
}
s.Logger.
WithField("component", "protoboards").
WithField("name", ID).
Error("protoboard not found")
return cmp.Protoboard{}, cmp.ErrProtoboardNotFound
}
// Update not supported
func (s *BinProtoboardsStore) Update(ctx context.Context, protoboard cmp.Protoboard) error {
return fmt.Errorf("Update to BinProtoboardsStore not supported")
}