Skip to content

Commit

Permalink
Merge pull request #1476 from target/sched-data-preserve-fields
Browse files Browse the repository at this point in the history
temp schedules: preserve unknown fields
  • Loading branch information
mastercactapus authored May 3, 2021
2 parents 20ac8d3 + 7506685 commit 35ad334
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
8 changes: 2 additions & 6 deletions config/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/target/goalert/permission"
"github.com/target/goalert/util"
"github.com/target/goalert/util/errutil"
"github.com/target/goalert/util/jsonutil"
"github.com/target/goalert/util/log"

"github.com/pkg/errors"
Expand Down Expand Up @@ -302,12 +303,7 @@ func (s *Store) updateConfigTx(ctx context.Context, tx *sql.Tx, fn func(Config)
return 0, err
}

data, err := json.Marshal(newCfg)
if err != nil {
return 0, errors.Wrap(err, "marshal config")
}

data, err = mergeJSON(cfg.data, data)
data, err := jsonutil.Apply(cfg.data, newCfg)
if err != nil {
return 0, errors.Wrap(err, "merge config")
}
Expand Down
9 changes: 5 additions & 4 deletions engine/cleanupmanager/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/target/goalert/config"
"github.com/target/goalert/permission"
"github.com/target/goalert/schedule"
"github.com/target/goalert/util/jsonutil"
"github.com/target/goalert/util/log"
)

Expand Down Expand Up @@ -79,16 +80,16 @@ func (db *DB) update(ctx context.Context) error {
type schedData struct {
ID string
Data schedule.Data
Raw json.RawMessage
}
var m []schedData
for rows.Next() {
var data schedData
var rawData json.RawMessage
err = rows.Scan(&data.ID, &rawData)
err = rows.Scan(&data.ID, &data.Raw)
if err != nil {
return err
}
err = json.Unmarshal(rawData, &data.Data)
err = json.Unmarshal(data.Raw, &data.Data)
if err != nil {
return err
}
Expand All @@ -106,7 +107,7 @@ func (db *DB) update(ctx context.Context) error {
schedCuttoff := now.AddDate(-1, 0, 0)
for _, dat := range m {
cleanupScheduleData(&dat.Data, lookup, schedCuttoff)
rawData, err := json.Marshal(dat.Data)
rawData, err := jsonutil.Apply(dat.Raw, dat.Data)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion schedule/storetemporaryschedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/target/goalert/permission"
"github.com/target/goalert/util/jsonutil"
"github.com/target/goalert/util/sqlutil"
"github.com/target/goalert/validation"
"github.com/target/goalert/validation/validate"
Expand Down Expand Up @@ -116,7 +117,8 @@ func (store *Store) updateFixedShifts(ctx context.Context, tx *sql.Tx, scheduleI
return err
}

rawData, err = json.Marshal(data)
// preserve unknown fields
rawData, err = jsonutil.Apply(rawData, data)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions util/jsonutil/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jsonutil

import "encoding/json"

// Apply can be used in place of `json.Marshal` but with the effect of
// merging into the original document.
func Apply(original []byte, src interface{}) ([]byte, error) {
srcDoc, err := json.Marshal(src)
if err != nil {
return nil, err
}
return Merge(original, srcDoc)
}
23 changes: 23 additions & 0 deletions util/jsonutil/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package jsonutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestApply(t *testing.T) {
const rawDoc = `{"Bin": "baz","Foo": "bar"}`
var newDoc struct {
Foo string
}
newDoc.Foo = "FOO"

data, err := Apply([]byte(rawDoc), newDoc)
assert.NoError(t, err)
assert.Equal(t, `{"Bin":"baz","Foo":"FOO"}`, string(data))

data, err = Apply(nil, newDoc)
assert.NoError(t, err)
assert.Equal(t, `{"Foo":"FOO"}`, string(data))
}
5 changes: 3 additions & 2 deletions config/mergejson.go → util/jsonutil/merge.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package jsonutil

import (
"encoding/json"
Expand All @@ -7,7 +7,8 @@ import (
"github.com/pkg/errors"
)

func mergeJSON(dst, src []byte) ([]byte, error) {
// Merge will recursively merge a src JSON document into the dst JSON document.
func Merge(dst, src []byte) ([]byte, error) {
var dstM, srcM map[string]interface{}
if len(dst) == 0 {
dstM = make(map[string]interface{})
Expand Down
6 changes: 3 additions & 3 deletions config/mergejson_test.go → util/jsonutil/merge_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package config
package jsonutil

import "testing"

func TestMergeJSON(t *testing.T) {
func TestMerge(t *testing.T) {
const orig = `{"foo": "bar", "bin": "baz", "d": {"e": "f"}}`
const add = `{"bin":"", "ok": "then", "a":{"b":"c"}, "d":{"e":"g"}}`
const exp = `{"a":{"b":"c"},"bin":"","d":{"e":"g"},"foo":"bar","ok":"then"}`

data, err := mergeJSON([]byte(orig), []byte(add))
data, err := Merge([]byte(orig), []byte(add))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 35ad334

Please sign in to comment.