api/router/static: allow to specify body dst (#1486)
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
bad15af45d
commit
5b5c73457b
30
rpc.go
30
rpc.go
@ -300,6 +300,7 @@ func requestPayload(r *http.Request) ([]byte, error) {
|
||||
|
||||
// allocate maximum
|
||||
matches := make(map[string]interface{}, len(md))
|
||||
bodydst := ""
|
||||
|
||||
// get fields from url path
|
||||
for k, v := range md {
|
||||
@ -307,6 +308,9 @@ func requestPayload(r *http.Request) ([]byte, error) {
|
||||
if strings.HasPrefix(k, "x-api-field-") {
|
||||
matches[strings.TrimPrefix(k, "x-api-field-")] = v
|
||||
delete(md, k)
|
||||
} else if k == "x-api-body" {
|
||||
bodydst = v
|
||||
delete(md, k)
|
||||
}
|
||||
}
|
||||
|
||||
@ -387,10 +391,34 @@ func requestPayload(r *http.Request) ([]byte, error) {
|
||||
} else {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
if bodydst == "" || bodydst == "*" {
|
||||
if out, err = jsonpatch.MergeMergePatches(out, bodybuf); err == nil {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
dstmap := make(map[string]interface{})
|
||||
ps := strings.Split(bodydst, ".")
|
||||
if len(ps) == 1 {
|
||||
dstmap[ps[0]] = bodybuf
|
||||
} else {
|
||||
em := make(map[string]interface{})
|
||||
em[ps[len(ps)-1]] = bodybuf
|
||||
for i := len(ps) - 2; i > 0; i-- {
|
||||
nm := make(map[string]interface{})
|
||||
nm[ps[i]] = em
|
||||
em = nm
|
||||
}
|
||||
dstmap[ps[0]] = em
|
||||
}
|
||||
|
||||
bodyout, err := json.Marshal(dstmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if out, err = jsonpatch.MergeMergePatches(out, bodyout); err == nil {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
//fallback to previous unknown behaviour
|
||||
return bodybuf, nil
|
||||
|
Loading…
Reference in New Issue
Block a user