patch.go (1172B)
1 package main 2 3 import ( 4 "errors" 5 "jacobedwards.org/spaceplanner.app/internal/backend" 6 ) 7 8 func applyPatchset(data map[string]interface{}, patches []backend.Patch) error { 9 for _, patch := range patches { 10 if err := applyPatch(data, patch); err != nil { 11 return err 12 } 13 } 14 return nil 15 } 16 17 func applyPatch(data map[string]interface{}, p backend.Patch) error { 18 // I'm aware path's are suppost to be able to be of any depth. Later. 19 v, exists := data[p.Path] 20 switch p.Op { 21 case "test": 22 if v != p.Value { 23 return errors.New("test failed") 24 } 25 case "remove": 26 if !exists { 27 return errors.New("Cannot remove non-existent") 28 } 29 delete(data, p.Path) 30 case "add": 31 data[p.Path] = p.Value 32 case "replace": 33 if !exists { 34 return errors.New("Cannot replace non-existent") 35 } 36 data[p.Path] = p.Value 37 case "move": 38 _, fexists := data[p.From] 39 if !fexists { 40 return errors.New("From does not exist") 41 } 42 data[p.Path] = data[p.From] 43 delete(data, p.From) 44 case "copy": 45 fv, fexists := data[p.From] 46 if !fexists { 47 return errors.New("From does not exist") 48 } 49 data[p.Path] = fv 50 default: 51 return errors.New(p.Op + ": Invalid operation") 52 } 53 return nil 54 }