本文整理汇总了Golang中github.com/ipfs/go-ipfs/path.Path.PopLastSegment方法的典型用法代码示例。如果您正苦于以下问题:Golang Path.PopLastSegment方法的具体用法?Golang Path.PopLastSegment怎么用?Golang Path.PopLastSegment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/ipfs/go-ipfs/path.Path
的用法示例。
在下文中一共展示了Path.PopLastSegment方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ResolveToKey
// ResolveToKey resolves a path to a key.
//
// It first checks if the path is already in the form of just a key (<key> or
// /ipfs/<key>) and returns immediately if so. Otherwise, it falls back onto
// Resolve to perform resolution of the dagnode being referenced.
func ResolveToKey(ctx context.Context, n *IpfsNode, p path.Path) (key.Key, error) {
// If the path is simply a key, parse and return it. Parsed paths are already
// normalized (read: prepended with /ipfs/ if needed), so segment[1] should
// always be the key.
if p.IsJustAKey() {
return key.B58KeyDecode(p.Segments()[1]), nil
}
// Fall back onto regular dagnode resolution. Retrieve the second-to-last
// segment of the path and resolve its link to the last segment.
head, tail, err := p.PopLastSegment()
if err != nil {
return key.Key(""), err
}
dagnode, err := Resolve(ctx, n, head)
if err != nil {
return key.Key(""), err
}
// Extract and return the key of the link to the target dag node.
link, err := dagnode.GetNodeLink(tail)
if err != nil {
return key.Key(""), err
}
return key.Key(link.Hash), nil
}
示例2: ResolveToCid
// ResolveToKey resolves a path to a key.
//
// It first checks if the path is already in the form of just a key (<key> or
// /ipfs/<key>) and returns immediately if so. Otherwise, it falls back onto
// Resolve to perform resolution of the dagnode being referenced.
func ResolveToCid(ctx context.Context, n *IpfsNode, p path.Path) (*cid.Cid, error) {
// If the path is simply a key, parse and return it. Parsed paths are already
// normalized (read: prepended with /ipfs/ if needed), so segment[1] should
// always be the key.
if p.IsJustAKey() {
return cid.Decode(p.Segments()[1])
}
// Fall back onto regular dagnode resolution. Retrieve the second-to-last
// segment of the path and resolve its link to the last segment.
head, tail, err := p.PopLastSegment()
if err != nil {
return nil, err
}
dagnode, err := Resolve(ctx, n.Namesys, n.Resolver, head)
if err != nil {
return nil, err
}
// Extract and return the key of the link to the target dag node.
link, _, err := dagnode.ResolveLink([]string{tail})
if err != nil {
return nil, err
}
return link.Cid, nil
}