summaryrefslogtreecommitdiff
path: root/system/db/addon.go
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2017-01-11 16:00:03 -0800
committerSteve Manuel <nilslice@gmail.com>2017-01-11 16:00:03 -0800
commit270e04f2333a9c0993fcee864bac9fbc2288cdb7 (patch)
treee3c54763e576f48591a4c9b125c227d06c2122db /system/db/addon.go
parentaec6cd4045a49c3aa5b0f8194658726c61b75094 (diff)
adding []byte driven api vs. url.Values where ever possible
Diffstat (limited to 'system/db/addon.go')
-rw-r--r--system/db/addon.go53
1 files changed, 21 insertions, 32 deletions
diff --git a/system/db/addon.go b/system/db/addon.go
index c77ccf0..4b3e20c 100644
--- a/system/db/addon.go
+++ b/system/db/addon.go
@@ -7,7 +7,10 @@ import (
"log"
"net/url"
+ "encoding/json"
+
"github.com/boltdb/bolt"
+ "github.com/gorilla/schema"
)
var (
@@ -16,8 +19,8 @@ var (
)
// Addon looks for an addon by its addon_reverse_dns as the key and returns
-// the url.Values representation of an addon
-func Addon(key string) (url.Values, error) {
+// the []byte as json representation of an addon
+func Addon(key string) ([]byte, error) {
buf := &bytes.Buffer{}
err := store.View(func(tx *bolt.Tx) error {
@@ -40,25 +43,21 @@ func Addon(key string) (url.Values, error) {
return nil, err
}
- data, err := url.ParseQuery(buf.String())
- if err != nil {
- return nil, err
- }
-
- return data, nil
+ return buf.Bytes(), nil
}
// SetAddon stores the values of an addon into the __addons bucket with a the
-// addon_reverse_dns field used as the key
-func SetAddon(data url.Values) error {
- // we don't know the structure of the addon type from a addon developer, so
- // encoding to json before it's stored in the db is difficult. Instead, we
- // can just encode the url.Values to a query string using the Encode() method.
- // The downside is that we will have to parse the values out of the query
- // string when loading it from the db
- v := data.Encode()
+// `addon_reverse_dns` field used as the key. `kind` is the interface{} type for
+// the provided addon (as in the result of calling addon.Types[id])
+func SetAddon(data url.Values, kind interface{}) error {
+ dec := schema.NewDecoder()
+ dec.IgnoreUnknownKeys(true)
+ dec.SetAliasTag("json")
+ err := dec.Decode(kind, data)
- err := store.Update(func(tx *bolt.Tx) error {
+ v, err := json.Marshal(kind)
+
+ err = store.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("__addons"))
k := data.Get("addon_reverse_dns")
if k == "" {
@@ -66,7 +65,7 @@ func SetAddon(data url.Values) error {
return fmt.Errorf(`Addon "%s" has no identifier to use as key.`, name)
}
- err := b.Put([]byte(k), []byte(v))
+ err := b.Put([]byte(k), v)
if err != nil {
return err
}
@@ -80,25 +79,15 @@ func SetAddon(data url.Values) error {
return nil
}
-// AddonAll returns all registered addons as a []url.Values
-func AddonAll() []url.Values {
- var all []url.Values
- buf := &bytes.Buffer{}
+// AddonAll returns all registered addons as a [][]byte
+func AddonAll() [][]byte {
+ var all [][]byte
err := store.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("__addons"))
err := b.ForEach(func(k, v []byte) error {
- _, err := buf.Write(v)
- if err != nil {
- return err
- }
-
- data, err := url.ParseQuery(buf.String())
- if err != nil {
- return err
- }
+ all = append(all, v)
- all = append(all, data)
return nil
})
if err != nil {