v01.config accepts and applies json patch
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user