v01.config accepts and applies json patch

This commit is contained in:
bel
2023-03-26 10:20:19 -06:00
parent f9dc4cff9f
commit af42db6803
4 changed files with 110 additions and 0 deletions

View File

@@ -1,5 +1,11 @@
package v01
import (
"encoding/json"
patch "github.com/evanphx/json-patch/v5"
)
type config struct {
Feedback struct {
Addr string
@@ -14,3 +20,24 @@ type config struct {
}
Quiet bool
}
func (cfg config) WithJSONPatch(v interface{}) config {
originalData, _ := json.Marshal(cfg)
patchData, err := json.Marshal(v)
if err != nil {
return cfg
}
patcher, err := patch.DecodePatch(patchData)
if err != nil {
return cfg
}
patchedData, err := patcher.Apply(originalData)
if err != nil {
return cfg
}
var patched config
if err := json.Unmarshal(patchedData, &patched); err != nil {
return cfg
}
return patched
}