mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-01-11 01:20:37 +00:00
oracle/neofs: change result types
1. `GetHash` returnes '0x'-prefixed little-endian hex-string. 2. `GetPayload` and `GetRange` assume result is UTF-8.
This commit is contained in:
parent
540c6c13a9
commit
e8b95ebf3e
2 changed files with 26 additions and 3 deletions
|
@ -9,9 +9,11 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/gogo/protobuf/jsonpb"
|
"github.com/gogo/protobuf/jsonpb"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||||
|
@ -104,7 +106,7 @@ func getPayload(ctx context.Context, c *client.Client, addr *object.Address) ([]
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return obj.Payload(), nil
|
return checkUTF8(obj.Payload())
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRange(ctx context.Context, c *client.Client, addr *object.Address, ps ...string) ([]byte, error) {
|
func getRange(ctx context.Context, c *client.Client, addr *object.Address, ps ...string) ([]byte, error) {
|
||||||
|
@ -115,7 +117,11 @@ func getRange(ctx context.Context, c *client.Client, addr *object.Address, ps ..
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return c.ObjectPayloadRangeData(ctx, new(client.RangeDataParams).WithAddress(addr).WithRange(r))
|
data, err := c.ObjectPayloadRangeData(ctx, new(client.RangeDataParams).WithAddress(addr).WithRange(r))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return checkUTF8(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHeader(ctx context.Context, c *client.Client, addr *object.Address) ([]byte, error) {
|
func getHeader(ctx context.Context, c *client.Client, addr *object.Address) ([]byte, error) {
|
||||||
|
@ -149,7 +155,7 @@ func getHash(ctx context.Context, c *client.Client, addr *object.Address, ps ...
|
||||||
if len(hashes) == 0 {
|
if len(hashes) == 0 {
|
||||||
return nil, fmt.Errorf("%w: empty response", ErrInvalidRange)
|
return nil, fmt.Errorf("%w: empty response", ErrInvalidRange)
|
||||||
}
|
}
|
||||||
return hashes[0][:], nil
|
return util.Uint256(hashes[0]).MarshalJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRange(s string) (*object.Range, error) {
|
func parseRange(s string) (*object.Range, error) {
|
||||||
|
@ -171,3 +177,10 @@ func parseRange(s string) (*object.Range, error) {
|
||||||
r.SetLength(length)
|
r.SetLength(length)
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkUTF8(v []byte) ([]byte, error) {
|
||||||
|
if !utf8.Valid(v) {
|
||||||
|
return nil, errors.New("invalid UTF-8")
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
|
@ -81,3 +81,13 @@ func TestParseNeoFSURL(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_checkUTF8(t *testing.T) {
|
||||||
|
_, err := checkUTF8([]byte{0xFF})
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
a := []byte{1, 2, 3}
|
||||||
|
b, err := checkUTF8(a)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, a, b)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue