7 pkgerrors "github.com/pkg/errors"
10 // DBconn interface used to talk to a concrete Database connection
13 // Key is an interface that will be implemented by anypackage
14 // that wants to use the Store interface. This allows various
15 // db backends and key types.
20 // Store is an interface for accessing a database
21 type Store interface {
22 // Returns nil if db health is good
25 // Unmarshal implements any unmarshaling needed for the database
26 Unmarshal(inp []byte, out interface{}) error
28 // Creates a new master table with key and links data with tag and
29 // creates a pointer to the newly added data in the master table
30 Create(table string, key Key, tag string, data interface{}) error
32 // Reads data for a particular key with specific tag.
33 Read(table string, key Key, tag string) ([]byte, error)
35 // Update data for particular key with specific tag
36 Update(table string, key Key, tag string, data interface{}) error
38 // Deletes a specific tag data for key.
39 // TODO: If tag is empty, it will delete all tags under key.
40 Delete(table string, key Key, tag string) error
42 // Reads all master tables and data from the specified tag in table
43 ReadAll(table string, tag string) (map[string][]byte, error)
46 // CreateDBClient creates the DB client
47 func CreateDBClient(dbType string) error {
51 // create a mongodb database with ICN as the name
52 DBconn, err = NewMongoStore("icn", nil)
54 return pkgerrors.New(dbType + "DB not supported")
59 // Serialize converts given data into a JSON string
60 func Serialize(v interface{}) (string, error) {
61 out, err := json.Marshal(v)
63 return "", pkgerrors.Wrap(err, "Error serializing "+reflect.TypeOf(v).String())
65 return string(out), nil
68 // DeSerialize converts string to a json object specified by type
69 func DeSerialize(str string, v interface{}) error {
70 err := json.Unmarshal([]byte(str), &v)
72 return pkgerrors.Wrap(err, "Error deSerializing "+str)