@@ -46,11 +46,14 @@ func (de DirEntry) Link() ipld.Link {
46
46
// node that it cannot fully load. If expectFull is false, it will ignore
47
47
// errors and return nil for any node it cannot load.
48
48
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 )
51
50
}
52
51
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 {
54
57
var proto datamodel.NodePrototype = dagpb .Type .PBNode
55
58
isDagPb := rootCid .Prefix ().Codec == cid .DagProtobuf
56
59
if ! isDagPb {
@@ -61,16 +64,16 @@ func toDirEntryRecursive(t *testing.T, linkSys linking.LinkSystem, rootCid cid.C
61
64
require .NoError (t , err )
62
65
} else if err != nil {
63
66
if e , ok := err .(interface { NotFound () bool }); ok && e .NotFound () {
64
- return nil
67
+ return DirEntry {}
65
68
}
66
69
require .NoError (t , err )
67
70
}
68
71
69
72
if node .Kind () == ipld .Kind_Bytes { // is a file
70
73
byts , err := node .AsBytes ()
71
74
require .NoError (t , err )
72
- return & DirEntry {
73
- Path : name ,
75
+ return DirEntry {
76
+ Path : rootPath ,
74
77
Content : byts ,
75
78
Root : rootCid ,
76
79
}
@@ -86,8 +89,8 @@ func toDirEntryRecursive(t *testing.T, linkSys linking.LinkSystem, rootCid cid.C
86
89
require .NoError (t , err )
87
90
childLink , err := v .AsLink ()
88
91
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 )
91
94
}
92
95
} else {
93
96
// 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
97
100
if err != nil {
98
101
return err
99
102
}
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 )
102
105
}
103
106
return nil
104
107
})
105
108
require .NoError (t , err )
106
109
}
107
110
108
- return & DirEntry {
109
- Path : name ,
111
+ return DirEntry {
112
+ Path : rootPath ,
110
113
Root : rootCid ,
111
114
Children : children ,
112
115
}
0 commit comments