Skip to content

Commit

Permalink
When making a map iterator over a directory, calls to .Next() curre…
Browse files Browse the repository at this point in the history
…ntly return a value of the `FieldHash()` of the dagpb link.

This change has them instead return a `IterLink`.

In both cases, the "exposed" interface of the returned type, `AsLink()` exposes the underlying hash cidLink to the entry.

By using a wrapping `IterLink` type, we provide an opportunity to type-cast the response, and instead of treating the response under it's returned `ipld.Node` interface,
the client has the ability to do something of the form:
```
k, next, err := iterator.Next()
nextSize := next.(*iter.IterLink).Substrate.FieldSize()
```
  • Loading branch information
willscott committed Aug 9, 2023
1 parent a1cbf13 commit a049aee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions iter/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func (itr *UnixFSDir__MapItr) Next() (k ipld.Node, v ipld.Node, err error) {
if itr.transformName != nil {
name = itr.transformName(name)
}
return name, next.FieldHash(), nil
return name, &IterLink{next}, nil
}
nb := dagpb.Type.String.NewBuilder()
err = nb.AssignString("")
if err != nil {
return nil, nil, err
}
s := nb.Build()
return s, next.FieldHash(), nil
return s, &IterLink{next}, nil

Check warning on line 48 in iter/iter.go

View check run for this annotation

Codecov / codecov/patch

iter/iter.go#L48

Added line #L48 was not covered by tests
}

func (itr *UnixFSDir__MapItr) Done() bool {
Expand Down
67 changes: 67 additions & 0 deletions iter/iterlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package iter

import (
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime/datamodel"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)

type IterLink struct {
Substrate dagpb.PBLink
}

func (il *IterLink) AsBool() (bool, error) {
return false, datamodel.ErrWrongKind{}

Check warning on line 14 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L13-L14

Added lines #L13 - L14 were not covered by tests
}
func (il *IterLink) AsBytes() ([]byte, error) {
return nil, datamodel.ErrWrongKind{}

Check warning on line 17 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L16-L17

Added lines #L16 - L17 were not covered by tests
}
func (il *IterLink) AsFloat() (float64, error) {
return 0.0, datamodel.ErrWrongKind{}

Check warning on line 20 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L19-L20

Added lines #L19 - L20 were not covered by tests
}
func (il *IterLink) AsInt() (int64, error) {
return 0, datamodel.ErrWrongKind{}

Check warning on line 23 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L22-L23

Added lines #L22 - L23 were not covered by tests
}
func (il *IterLink) AsLink() (datamodel.Link, error) {
return il.Substrate.FieldHash().AsLink()
}
func (il *IterLink) AsString() (string, error) {
return "", datamodel.ErrWrongKind{}

Check warning on line 29 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L28-L29

Added lines #L28 - L29 were not covered by tests
}

func (il *IterLink) IsAbsent() bool {
return il.Substrate.IsAbsent()

Check warning on line 33 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L32-L33

Added lines #L32 - L33 were not covered by tests
}
func (il *IterLink) IsNull() bool {
return il.Substrate.IsNull()

Check warning on line 36 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L35-L36

Added lines #L35 - L36 were not covered by tests
}
func (il *IterLink) Kind() datamodel.Kind {
return datamodel.Kind_Link
}
func (il *IterLink) Length() int64 {
return 0

Check warning on line 42 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}
func (il *IterLink) ListIterator() datamodel.ListIterator {
return nil

Check warning on line 45 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}
func (il *IterLink) LookupByIndex(idx int64) (datamodel.Node, error) {
return nil, datamodel.ErrWrongKind{}

Check warning on line 48 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}
func (il *IterLink) LookupByNode(key datamodel.Node) (datamodel.Node, error) {
return nil, datamodel.ErrWrongKind{}

Check warning on line 51 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}
func (il *IterLink) LookupBySegment(seg datamodel.PathSegment) (datamodel.Node, error) {
return nil, datamodel.ErrWrongKind{}

Check warning on line 54 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}
func (il *IterLink) LookupByString(key string) (datamodel.Node, error) {
return nil, datamodel.ErrWrongKind{}

Check warning on line 57 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}
func (il *IterLink) MapIterator() datamodel.MapIterator {
return nil

Check warning on line 60 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}
func (il *IterLink) Prototype() datamodel.NodePrototype {
return basicnode.Prototype__Link{}

Check warning on line 63 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}
func (il *IterLink) Representation() datamodel.Node {
return il

Check warning on line 66 in iter/iterlink.go

View check run for this annotation

Codecov / codecov/patch

iter/iterlink.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

0 comments on commit a049aee

Please sign in to comment.