I have a JSON REST API accepting sparse updates, but the pattern I've come up with seems exceptionally verbose. Am I going about this the wrong way?
(Assume this is using a data store with no sparse update support built in.)
func choose(a, b *string) *string {
if a != nil {
return a
}
return b
}
type Model {
Id *string `json:"id"`
Field1 *string `json:"field1"`
Field2 *string `json:"field2"`
Field3 *string `json:"field3"`
...
}
func (m1 Model) Update(m2 Model) (m3 Model) {
m3.Id = choose(m2.Id, m1.Id)
m3.Field1 = choose(m2.Field1, m1.Field1)
m3.Field2 = choose(m2.Field2, m1.Field2)
m3.Field3 = choose(m2.Field3, m1.Field3)
...
return
}
func UpdateController(input Model) error {
previous, _ := store.Get(*input.Id)
updated := previous.Update(input)
return store.Put(updated)
}
Ideally I'd be able to write UpdateController
like this instead:
func UpdateController(input Model) {
previous, _ := store.Get(*input.Id)
updated, _ := structs.Update(previous, input)
return store.Put(updated)
}
(Error-handling elided for clarity.)