Skip to content

Commit 9f4c804

Browse files
committed
feat: expose ToDirEntryFrom to allow sub-dag representation
1 parent 5157d10 commit 9f4c804

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

testutil/directory.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ func (de DirEntry) Link() ipld.Link {
4646
// node that it cannot fully load. If expectFull is false, it will ignore
4747
// errors and return nil for any node it cannot load.
4848
func ToDirEntry(t *testing.T, linkSys linking.LinkSystem, rootCid cid.Cid, expectFull bool) DirEntry {
49-
de := toDirEntryRecursive(t, linkSys, rootCid, "", expectFull)
50-
return *de
49+
return ToDirEntryFrom(t, linkSys, rootCid, "", expectFull)
5150
}
5251

53-
func toDirEntryRecursive(t *testing.T, linkSys linking.LinkSystem, rootCid cid.Cid, name string, expectFull bool) *DirEntry {
52+
// ToDirEntryFrom is the same as ToDirEntry but allows specifying a rootPath
53+
// such that the resulting DirEntry tree will all have that path as a prefix.
54+
// This is useful when representing a sub-DAG of a larger DAG where you want
55+
// to make direct comparisons.
56+
func ToDirEntryFrom(t *testing.T, linkSys linking.LinkSystem, rootCid cid.Cid, rootPath string, expectFull bool) DirEntry {
5457
var proto datamodel.NodePrototype = dagpb.Type.PBNode
5558
isDagPb := rootCid.Prefix().Codec == cid.DagProtobuf
5659
if !isDagPb {
@@ -61,16 +64,16 @@ func toDirEntryRecursive(t *testing.T, linkSys linking.LinkSystem, rootCid cid.C
6164
require.NoError(t, err)
6265
} else if err != nil {
6366
if e, ok := err.(interface{ NotFound() bool }); ok && e.NotFound() {
64-
return nil
67+
return DirEntry{}
6568
}
6669
require.NoError(t, err)
6770
}
6871

6972
if node.Kind() == ipld.Kind_Bytes { // is a file
7073
byts, err := node.AsBytes()
7174
require.NoError(t, err)
72-
return &DirEntry{
73-
Path: name,
75+
return DirEntry{
76+
Path: rootPath,
7477
Content: byts,
7578
Root: rootCid,
7679
}
@@ -86,8 +89,8 @@ func toDirEntryRecursive(t *testing.T, linkSys linking.LinkSystem, rootCid cid.C
8689
require.NoError(t, err)
8790
childLink, err := v.AsLink()
8891
require.NoError(t, err)
89-
child := toDirEntryRecursive(t, linkSys, childLink.(cidlink.Link).Cid, name+"/"+childName, expectFull)
90-
children = append(children, *child)
92+
child := ToDirEntryFrom(t, linkSys, childLink.(cidlink.Link).Cid, rootPath+"/"+childName, expectFull)
93+
children = append(children, child)
9194
}
9295
} else {
9396
// not a dag-pb node, let's pretend it is but using IPLD pathing rules
@@ -97,16 +100,16 @@ func toDirEntryRecursive(t *testing.T, linkSys linking.LinkSystem, rootCid cid.C
97100
if err != nil {
98101
return err
99102
}
100-
child := toDirEntryRecursive(t, linkSys, l.(cidlink.Link).Cid, name+"/"+prog.Path.String(), expectFull)
101-
children = append(children, *child)
103+
child := ToDirEntryFrom(t, linkSys, l.(cidlink.Link).Cid, rootPath+"/"+prog.Path.String(), expectFull)
104+
children = append(children, child)
102105
}
103106
return nil
104107
})
105108
require.NoError(t, err)
106109
}
107110

108-
return &DirEntry{
109-
Path: name,
111+
return DirEntry{
112+
Path: rootPath,
110113
Root: rootCid,
111114
Children: children,
112115
}

0 commit comments

Comments
 (0)