|
| 1 | +package unixfsnode |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/ipfs/go-unixfsnode/iter" |
| 5 | + "github.com/ipfs/go-unixfsnode/utils" |
| 6 | + dagpb "github.com/ipld/go-codec-dagpb" |
| 7 | + "github.com/ipld/go-ipld-prime" |
| 8 | + "github.com/ipld/go-ipld-prime/schema" |
| 9 | +) |
| 10 | + |
| 11 | +var _ ipld.Node = PathedPBNode(nil) |
| 12 | +var _ schema.TypedNode = PathedPBNode(nil) |
| 13 | + |
| 14 | +type PathedPBNode = *_PathedPBNode |
| 15 | + |
| 16 | +type _PathedPBNode struct { |
| 17 | + _substrate dagpb.PBNode |
| 18 | +} |
| 19 | + |
| 20 | +func (n PathedPBNode) Kind() ipld.Kind { |
| 21 | + return n._substrate.Kind() |
| 22 | +} |
| 23 | + |
| 24 | +// LookupByString looks for the key in the list of links with a matching name |
| 25 | +func (n PathedPBNode) LookupByString(key string) (ipld.Node, error) { |
| 26 | + links := n._substrate.FieldLinks() |
| 27 | + link := utils.Lookup(links, key) |
| 28 | + if link == nil { |
| 29 | + return nil, schema.ErrNoSuchField{Type: nil /*TODO*/, Field: ipld.PathSegmentOfString(key)} |
| 30 | + } |
| 31 | + return link, nil |
| 32 | +} |
| 33 | + |
| 34 | +func (n PathedPBNode) LookupByNode(key ipld.Node) (ipld.Node, error) { |
| 35 | + ks, err := key.AsString() |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + return n.LookupByString(ks) |
| 40 | +} |
| 41 | + |
| 42 | +func (n PathedPBNode) LookupByIndex(idx int64) (ipld.Node, error) { |
| 43 | + return n._substrate.LookupByIndex(idx) |
| 44 | +} |
| 45 | + |
| 46 | +func (n PathedPBNode) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) { |
| 47 | + return n.LookupByString(seg.String()) |
| 48 | +} |
| 49 | + |
| 50 | +func (n PathedPBNode) MapIterator() ipld.MapIterator { |
| 51 | + return iter.NewUnixFSDirMapIterator(n._substrate.Links.Iterator(), nil) |
| 52 | +} |
| 53 | + |
| 54 | +// ListIterator returns an iterator which yields key-value pairs |
| 55 | +// traversing the node. |
| 56 | +// If the node kind is anything other than a list, nil will be returned. |
| 57 | +// |
| 58 | +// The iterator will yield every entry in the list; that is, it |
| 59 | +// can be expected that itr.Next will be called node.Length times |
| 60 | +// before itr.Done becomes true. |
| 61 | +func (n PathedPBNode) ListIterator() ipld.ListIterator { |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// Length returns the length of a list, or the number of entries in a map, |
| 66 | +// or -1 if the node is not of list nor map kind. |
| 67 | +func (n PathedPBNode) Length() int64 { |
| 68 | + return n._substrate.FieldLinks().Length() |
| 69 | +} |
| 70 | + |
| 71 | +func (n PathedPBNode) IsAbsent() bool { |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +func (n PathedPBNode) IsNull() bool { |
| 76 | + return false |
| 77 | +} |
| 78 | + |
| 79 | +func (n PathedPBNode) AsBool() (bool, error) { |
| 80 | + return n._substrate.AsBool() |
| 81 | +} |
| 82 | + |
| 83 | +func (n PathedPBNode) AsInt() (int64, error) { |
| 84 | + return n._substrate.AsInt() |
| 85 | +} |
| 86 | + |
| 87 | +func (n PathedPBNode) AsFloat() (float64, error) { |
| 88 | + return n._substrate.AsFloat() |
| 89 | +} |
| 90 | + |
| 91 | +func (n PathedPBNode) AsString() (string, error) { |
| 92 | + return n._substrate.AsString() |
| 93 | +} |
| 94 | + |
| 95 | +func (n PathedPBNode) AsBytes() ([]byte, error) { |
| 96 | + return n._substrate.AsBytes() |
| 97 | +} |
| 98 | + |
| 99 | +func (n PathedPBNode) AsLink() (ipld.Link, error) { |
| 100 | + return n._substrate.AsLink() |
| 101 | +} |
| 102 | + |
| 103 | +func (n PathedPBNode) Prototype() ipld.NodePrototype { |
| 104 | + // TODO: should this return something? |
| 105 | + // probobly not until we write the write interfaces |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// satisfy schema.TypedNode |
| 110 | +func (PathedPBNode) Type() schema.Type { |
| 111 | + return nil /*TODO:typelit*/ |
| 112 | +} |
| 113 | + |
| 114 | +func (n PathedPBNode) Representation() ipld.Node { |
| 115 | + return n._substrate.Representation() |
| 116 | +} |
| 117 | + |
| 118 | +// Native map accessors |
| 119 | + |
| 120 | +func (n PathedPBNode) Iterator() *iter.UnixFSDir__Itr { |
| 121 | + |
| 122 | + return iter.NewUnixFSDirIterator(n._substrate.Links.Iterator(), nil) |
| 123 | +} |
| 124 | + |
| 125 | +func (n PathedPBNode) Lookup(key dagpb.String) dagpb.Link { |
| 126 | + return utils.Lookup(n._substrate.FieldLinks(), key.String()) |
| 127 | +} |
| 128 | + |
| 129 | +// direct access to the links and data |
| 130 | + |
| 131 | +func (n PathedPBNode) FieldLinks() dagpb.PBLinks { |
| 132 | + return n._substrate.FieldLinks() |
| 133 | +} |
| 134 | + |
| 135 | +func (n PathedPBNode) FieldData() dagpb.MaybeBytes { |
| 136 | + return n._substrate.FieldData() |
| 137 | +} |
| 138 | + |
| 139 | +// Substrate returns the underlying PBNode -- note: only the substrate will encode successfully to protobuf if writing |
| 140 | +func (n PathedPBNode) Substrate() ipld.Node { |
| 141 | + return n._substrate |
| 142 | +} |
0 commit comments