forked from TrueCloudLab/frostfs-sdk-go
[#327] nns: Support Null
returned value
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
d808f72c38
commit
71891029da
2 changed files with 21 additions and 11 deletions
30
ns/nns.go
30
ns/nns.go
|
@ -2,6 +2,7 @@ package ns
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
|
@ -11,6 +12,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
||||
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"github.com/nspcc-dev/neofs-contract/nns"
|
||||
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
||||
)
|
||||
|
@ -82,24 +84,32 @@ func (n *NNS) Dial(address string) error {
|
|||
//
|
||||
// See also https://docs.neo.org/docs/en-us/reference/nns.html.
|
||||
func (n *NNS) ResolveContainerName(name string) (cid.ID, error) {
|
||||
arr, err := unwrap.Array(n.invoker.Call(n.nnsContract, "resolve",
|
||||
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "resolve",
|
||||
name+".container", int64(nns.TXT),
|
||||
))
|
||||
if err != nil {
|
||||
return cid.ID{}, fmt.Errorf("contract invocation: %w", err)
|
||||
}
|
||||
|
||||
var id cid.ID
|
||||
|
||||
for i := range arr {
|
||||
bs, err := arr[i].TryBytes()
|
||||
if err != nil {
|
||||
return cid.ID{}, fmt.Errorf("convert array item to byte slice: %w", err)
|
||||
if _, ok := item.(stackitem.Null); !ok {
|
||||
arr, ok := item.Value().([]stackitem.Item)
|
||||
if !ok {
|
||||
// unexpected for types from stackitem package
|
||||
return cid.ID{}, errors.New("invalid cast to stack item slice")
|
||||
}
|
||||
|
||||
err = id.DecodeString(string(bs))
|
||||
if err == nil {
|
||||
return id, nil
|
||||
var id cid.ID
|
||||
|
||||
for i := range arr {
|
||||
bs, err := arr[i].TryBytes()
|
||||
if err != nil {
|
||||
return cid.ID{}, fmt.Errorf("convert array item to byte slice: %w", err)
|
||||
}
|
||||
|
||||
err = id.DecodeString(string(bs))
|
||||
if err == nil {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -112,7 +112,7 @@ func TestNNS_ResolveContainerName(t *testing.T) {
|
|||
testC.res.Stack[0] = stackitem.Null{}
|
||||
|
||||
_, err := n.ResolveContainerName(testContainerName)
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, errNotFound)
|
||||
})
|
||||
|
||||
t.Run("array stack item with non-slice value", func(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue