|
| 1 | +package unixfsnode |
| 2 | + |
| 3 | +import ( |
| 4 | + dagpb "github.com/ipld/go-codec-dagpb" |
| 5 | + "github.com/ipld/go-ipld-prime" |
| 6 | + "github.com/ipld/go-ipld-prime/schema" |
| 7 | +) |
| 8 | + |
| 9 | +var _ ipld.Node = UnixFSNode(nil) |
| 10 | +var _ schema.TypedNode = UnixFSNode(nil) |
| 11 | + |
| 12 | +type UnixFSNode = *_UnixFSNode |
| 13 | + |
| 14 | +type _UnixFSNode struct { |
| 15 | + _substrate dagpb.PBNode |
| 16 | +} |
| 17 | + |
| 18 | +func (n UnixFSNode) Kind() ipld.Kind { |
| 19 | + return n._substrate.Kind() |
| 20 | +} |
| 21 | + |
| 22 | +// LookupByString looks for the key in the list of links with a matching name |
| 23 | +func (n UnixFSNode) LookupByString(key string) (ipld.Node, error) { |
| 24 | + links := n._substrate.FieldLinks() |
| 25 | + link := lookup(links, key) |
| 26 | + if link == nil { |
| 27 | + return nil, schema.ErrNoSuchField{Type: nil /*TODO*/, Field: ipld.PathSegmentOfString(key)} |
| 28 | + } |
| 29 | + return link, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (n UnixFSNode) LookupByNode(key ipld.Node) (ipld.Node, error) { |
| 33 | + ks, err := key.AsString() |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + return n.LookupByString(ks) |
| 38 | +} |
| 39 | + |
| 40 | +func (n UnixFSNode) LookupByIndex(idx int64) (ipld.Node, error) { |
| 41 | + return n._substrate.LookupByIndex(idx) |
| 42 | +} |
| 43 | + |
| 44 | +func (n UnixFSNode) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) { |
| 45 | + return n.LookupByString(seg.String()) |
| 46 | +} |
| 47 | + |
| 48 | +func (n UnixFSNode) MapIterator() ipld.MapIterator { |
| 49 | + return &UnixFSNode__MapItr{n._substrate.Links.Iterator()} |
| 50 | +} |
| 51 | + |
| 52 | +// UnixFSNode map iterator iterates throught the links as if they were a map |
| 53 | +// Note: for now it does return links with no name, where the key is just String("") |
| 54 | +type UnixFSNode__MapItr struct { |
| 55 | + _substrate *dagpb.PBLinks__Itr |
| 56 | +} |
| 57 | + |
| 58 | +func (itr *UnixFSNode__MapItr) Next() (k ipld.Node, v ipld.Node, err error) { |
| 59 | + _, next := itr._substrate.Next() |
| 60 | + if next == nil { |
| 61 | + return nil, nil, ipld.ErrIteratorOverread{} |
| 62 | + } |
| 63 | + if next.FieldName().Exists() { |
| 64 | + return next.FieldName().Must(), &_UnixFSLink{next.FieldHash()}, nil |
| 65 | + } |
| 66 | + nb := dagpb.Type.String.NewBuilder() |
| 67 | + err = nb.AssignString("") |
| 68 | + if err != nil { |
| 69 | + return nil, nil, err |
| 70 | + } |
| 71 | + s := nb.Build() |
| 72 | + return s, next.FieldHash(), nil |
| 73 | +} |
| 74 | + |
| 75 | +func (itr *UnixFSNode__MapItr) Done() bool { |
| 76 | + return itr._substrate.Done() |
| 77 | +} |
| 78 | + |
| 79 | +// ListIterator returns an iterator which yields key-value pairs |
| 80 | +// traversing the node. |
| 81 | +// If the node kind is anything other than a list, nil will be returned. |
| 82 | +// |
| 83 | +// The iterator will yield every entry in the list; that is, it |
| 84 | +// can be expected that itr.Next will be called node.Length times |
| 85 | +// before itr.Done becomes true. |
| 86 | +func (n UnixFSNode) ListIterator() ipld.ListIterator { |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// Length returns the length of a list, or the number of entries in a map, |
| 91 | +// or -1 if the node is not of list nor map kind. |
| 92 | +func (n UnixFSNode) Length() int64 { |
| 93 | + return n._substrate.FieldLinks().Length() |
| 94 | +} |
| 95 | + |
| 96 | +func (n UnixFSNode) IsAbsent() bool { |
| 97 | + return false |
| 98 | +} |
| 99 | + |
| 100 | +func (n UnixFSNode) IsNull() bool { |
| 101 | + return false |
| 102 | +} |
| 103 | + |
| 104 | +func (n UnixFSNode) AsBool() (bool, error) { |
| 105 | + return n._substrate.AsBool() |
| 106 | +} |
| 107 | + |
| 108 | +func (n UnixFSNode) AsInt() (int64, error) { |
| 109 | + return n._substrate.AsInt() |
| 110 | +} |
| 111 | + |
| 112 | +func (n UnixFSNode) AsFloat() (float64, error) { |
| 113 | + return n._substrate.AsFloat() |
| 114 | +} |
| 115 | + |
| 116 | +func (n UnixFSNode) AsString() (string, error) { |
| 117 | + return n._substrate.AsString() |
| 118 | +} |
| 119 | + |
| 120 | +func (n UnixFSNode) AsBytes() ([]byte, error) { |
| 121 | + return n._substrate.AsBytes() |
| 122 | +} |
| 123 | + |
| 124 | +func (n UnixFSNode) AsLink() (ipld.Link, error) { |
| 125 | + return n._substrate.AsLink() |
| 126 | +} |
| 127 | + |
| 128 | +func (n UnixFSNode) Prototype() ipld.NodePrototype { |
| 129 | + return _UnixFSNode__Prototype{} |
| 130 | +} |
| 131 | + |
| 132 | +// satisfy schema.TypedNode |
| 133 | +func (UnixFSNode) Type() schema.Type { |
| 134 | + return nil /*TODO:typelit*/ |
| 135 | +} |
| 136 | + |
| 137 | +func (n UnixFSNode) Representation() ipld.Node { |
| 138 | + return n._substrate.Representation() |
| 139 | +} |
| 140 | + |
| 141 | +// Native map accessors |
| 142 | + |
| 143 | +func (n UnixFSNode) Iterator() *UnixFSNode__Itr { |
| 144 | + |
| 145 | + return &UnixFSNode__Itr{n._substrate.Links.Iterator()} |
| 146 | +} |
| 147 | + |
| 148 | +type UnixFSNode__Itr struct { |
| 149 | + _substrate *dagpb.PBLinks__Itr |
| 150 | +} |
| 151 | + |
| 152 | +func (itr *UnixFSNode__Itr) Next() (k dagpb.String, v UnixFSLink) { |
| 153 | + _, next := itr._substrate.Next() |
| 154 | + if next == nil { |
| 155 | + return nil, nil |
| 156 | + } |
| 157 | + if next.FieldName().Exists() { |
| 158 | + return next.FieldName().Must(), &_UnixFSLink{next.FieldHash()} |
| 159 | + } |
| 160 | + nb := dagpb.Type.String.NewBuilder() |
| 161 | + err := nb.AssignString("") |
| 162 | + if err != nil { |
| 163 | + return nil, nil |
| 164 | + } |
| 165 | + s := nb.Build() |
| 166 | + return s.(dagpb.String), &_UnixFSLink{next.FieldHash()} |
| 167 | +} |
| 168 | +func (itr *UnixFSNode__Itr) Done() bool { |
| 169 | + return itr._substrate.Done() |
| 170 | +} |
| 171 | + |
| 172 | +func (n UnixFSNode) Lookup(key dagpb.String) UnixFSLink { |
| 173 | + return lookup(n._substrate.FieldLinks(), key.String()) |
| 174 | +} |
| 175 | + |
| 176 | +// direct access to the links and data |
| 177 | + |
| 178 | +func (n UnixFSNode) FieldLinks() dagpb.PBLinks { |
| 179 | + return n._substrate.FieldLinks() |
| 180 | +} |
| 181 | + |
| 182 | +func (n UnixFSNode) FieldData() dagpb.MaybeBytes { |
| 183 | + return n._substrate.FieldData() |
| 184 | +} |
| 185 | + |
| 186 | +// we need to lookup by key in a list of dag pb links a fair amount, so just have |
| 187 | +// a shortcut method |
| 188 | +func lookup(links dagpb.PBLinks, key string) UnixFSLink { |
| 189 | + li := links.Iterator() |
| 190 | + for !li.Done() { |
| 191 | + _, next := li.Next() |
| 192 | + name := "" |
| 193 | + if next.FieldName().Exists() { |
| 194 | + name = next.FieldName().Must().String() |
| 195 | + } |
| 196 | + if key == name { |
| 197 | + return &_UnixFSLink{next.FieldHash()} |
| 198 | + } |
| 199 | + } |
| 200 | + return nil |
| 201 | +} |
0 commit comments