summaryrefslogtreecommitdiff
path: root/system/addon/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/addon/addon.go
parentaec6cd4045a49c3aa5b0f8194658726c61b75094 (diff)
adding []byte driven api vs. url.Values where ever possible
Diffstat (limited to 'system/addon/addon.go')
-rw-r--r--system/addon/addon.go47
1 files changed, 42 insertions, 5 deletions
diff --git a/system/addon/addon.go b/system/addon/addon.go
index a423d07..833ce23 100644
--- a/system/addon/addon.go
+++ b/system/addon/addon.go
@@ -8,6 +8,7 @@ import (
"github.com/ponzu-cms/ponzu/system/db"
"github.com/ponzu-cms/ponzu/system/item"
+ "github.com/tidwall/sjson"
)
var (
@@ -65,8 +66,7 @@ func Register(m Meta, fn func() interface{}) Addon {
// register sets up the system to use the Addon by:
// 1. Validating the Addon struct
-// 2. Checking that the Addon parent type was added to Types (via its New())
-// 3. Saving it to the __addons bucket in DB with id/key = addon_reverse_dns
+// 2. Saving it to the __addons bucket in DB with id/key = addon_reverse_dns
func register(a Addon) error {
if a.PonzuAddonName == "" {
return fmt.Errorf(`Addon must have valid Meta struct embedded: missing %s field.`, "PonzuAddonName")
@@ -118,7 +118,12 @@ func register(a Addon) error {
// db.SetAddon is like SetContent, but rather than the key being an int64 ID,
// we need it to be a string based on the addon_reverse_dns
- err = db.SetAddon(vals)
+ kind, ok := Types[a.PonzuAddonReverseDNS]
+ if !ok {
+ return fmt.Errorf("Error: no addon to set with id: %s", a.PonzuAddonReverseDNS)
+ }
+
+ err = db.SetAddon(vals, kind())
if err != nil {
return err
}
@@ -163,9 +168,41 @@ func setStatus(key, status string) error {
return err
}
- a.Set("addon_status", status)
+ a, err = sjson.SetBytes(a, "addon_status", status)
+ if err != nil {
+ return err
+ }
+
+ kind, ok := Types[key]
+ if !ok {
+ return fmt.Errorf("Error: no addon to set with id: %s", key)
+ }
+
+ // convert json => map[string]interface{} => url.Values
+ var kv map[string]interface{}
+ err = json.Unmarshal(a, kv)
+ if err != nil {
+ return err
+ }
+
+ var vals url.Values
+ for k, v := range kv {
+ switch v.(type) {
+ case []string:
+ s := v.([]string)
+ for i := range s {
+ if i == 0 {
+ vals.Set(k, s[i])
+ }
+
+ vals.Add(k, s[i])
+ }
+ default:
+ vals.Set(k, fmt.Sprintf("%v", v))
+ }
+ }
- err = db.SetAddon(a)
+ err = db.SetAddon(vals, kind())
if err != nil {
return err
}