mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-27 14:14:12 +08:00
path: simplify ResolveToLastNode
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -70,33 +70,41 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (ipld
|
|||||||
|
|
||||||
for len(p) > 0 {
|
for len(p) > 0 {
|
||||||
lnk, rest, err := r.ResolveOnce(ctx, r.DAG, nd, p)
|
lnk, rest, err := r.ResolveOnce(ctx, r.DAG, nd, p)
|
||||||
if lnk != nil {
|
if lnk == nil {
|
||||||
if err != nil {
|
break
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
next, err := lnk.GetNode(ctx, r.DAG)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
nd = next
|
|
||||||
p = rest
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val, rest, err := nd.Resolve(p)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
switch val.(type) {
|
|
||||||
case *ipld.Link:
|
next, err := lnk.GetNode(ctx, r.DAG)
|
||||||
return nil, nil, errors.New("inconsistent ResolveOnce / nd.Resolve")
|
if err != nil {
|
||||||
default:
|
return nil, nil, err
|
||||||
return nd, p, nil
|
|
||||||
}
|
}
|
||||||
|
nd = next
|
||||||
|
p = rest
|
||||||
}
|
}
|
||||||
|
|
||||||
return nd, nil, nil
|
if len(p) == 0 {
|
||||||
|
return nd, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirm the path exists within the object
|
||||||
|
val, rest, err := nd.Resolve(p)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rest) > 0 {
|
||||||
|
return nil, nil, errors.New("path failed to resolve fully")
|
||||||
|
}
|
||||||
|
switch val.(type) {
|
||||||
|
case *ipld.Link:
|
||||||
|
return nil, nil, errors.New("inconsistent ResolveOnce / nd.Resolve")
|
||||||
|
default:
|
||||||
|
return nd, p, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolvePath fetches the node for given path. It returns the last item
|
// ResolvePath fetches the node for given path. It returns the last item
|
||||||
|
Reference in New Issue
Block a user