diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-03-20 13:25:23 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-03-20 13:25:23 -0700 |
commit | ac3a65292037c0583bec67738c6c2baff7829ec4 (patch) | |
tree | 8bb3d82f073022c48ad53a7f8528a0f60c391220 /system/db | |
parent | f6f6d6e05d9f5f209b6a3273eb6a57af1eb5f6a2 (diff) |
separate UpdateContent and SetContent to differentiate when data should be merged or replaced
Diffstat (limited to 'system/db')
-rw-r--r-- | system/db/content.go | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/system/db/content.go b/system/db/content.go index 8a91828..685ac13 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -3,7 +3,6 @@ package db import ( "bytes" "encoding/json" - "errors" "fmt" "log" "net/url" @@ -18,16 +17,18 @@ import ( uuid "github.com/satori/go.uuid" ) +// IsValidID checks that an ID from a DB target is valid. +// ID should be an integer greater than 0. +// ID of -1 is special for new posts, not updates. +// IDs start at 1 for auto-incrementing func IsValidID(id string) bool { - // ID should be a non-negative integer. - // ID of -1 is special for new posts, not updates. - if i, err := strconv.Atoi(id); err != nil || i < 0 { + if i, err := strconv.Atoi(id); err != nil || i < 1 { return false } return true } -// SetContent inserts or updates values in the database. +// SetContent inserts/replaces values in the database. // The `target` argument is a string made up of namespace:id (string:int) func SetContent(target string, data url.Values) (int, error) { t := strings.Split(target, ":") @@ -43,6 +44,19 @@ func SetContent(target string, data url.Values) (int, error) { return insert(ns, data) } + return update(ns, id, data, nil) +} + +// UpdateContent updates/merges values in the database. +// The `target` argument is a string made up of namespace:id (string:int) +func UpdateContent(target string, data url.Values) (int, error) { + t := strings.Split(target, ":") + ns, id := t[0], t[1] + + if !IsValidID(id) { + return 0, fmt.Errorf("Invalid ID in target for UpdateContent: %s", target) + } + // retrieve existing content from the database existingContent, err := Content(target) if err != nil { @@ -51,7 +65,9 @@ func SetContent(target string, data url.Values) (int, error) { return update(ns, id, data, &existingContent) } -// update can support merge or replace behavior +// update can support merge or replace behavior depending on existingContent. +// if existingContent is non-nil, we merge field values. empty/missing fields are ignored. +// if existingContent is nil, we replace field values. empty/missing fields are reset. func update(ns, id string, data url.Values, existingContent *[]byte) (int, error) { var specifier string // i.e. __pending, __sorted, etc. if strings.Contains(ns, "__") { @@ -112,8 +128,7 @@ func mergeData(ns string, data url.Values, existingContent []byte) ([]byte, erro var j []byte t, ok := item.Types[ns] if !ok { - log.Println("Type not found from namespace:", ns) - return j, errors.New("Invalid type.") + return nil, fmt.Errorf("namespace type not found:", ns) } // Unmarsal the existing values |