oracle/neofs: allow to use filters on NeoFS responses

This commit is contained in:
Evgeniy Stratonikov 2021-03-12 14:15:40 +03:00
parent 508a1ca9ee
commit 540c6c13a9
2 changed files with 15 additions and 12 deletions

View file

@ -6,6 +6,8 @@ import (
"unicode/utf8"
"github.com/PaesslerAG/jsonpath"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
)
func filter(value []byte, path string) ([]byte, error) {
@ -22,3 +24,14 @@ func filter(value []byte, path string) ([]byte, error) {
}
return json.Marshal([]interface{}{result})
}
func filterRequest(result []byte, req *state.OracleRequest) (transaction.OracleResponseCode, []byte) {
if req.Filter != nil {
var err error
result, err = filter(result, *req.Filter)
if err != nil {
return transaction.Error, nil
}
}
return transaction.Success, result
}

View file

@ -117,16 +117,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
}
break
}
if req.Req.Filter != nil {
res, err := filter(result, *req.Req.Filter)
if err != nil {
resp.Code = transaction.Error
break
}
result = res
}
resp.Code = transaction.Success
resp.Result = result
resp.Code, resp.Result = filterRequest(result, req.Req)
case r.StatusCode == http.StatusForbidden:
resp.Code = transaction.Forbidden
case r.StatusCode == http.StatusNotFound:
@ -144,8 +135,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error {
if err != nil {
resp.Code = transaction.Error
} else {
resp.Code = transaction.Success
resp.Result = res
resp.Code, resp.Result = filterRequest(res, req.Req)
}
}