1919const _path = require ( 'path' ) ;
2020const { PuterPath } = require ( '../lib/PuterPath' ) ;
2121
22- class NodePathSelector {
22+ class NodeSelector {
23+ path = null ;
24+ uid = null ;
25+ }
26+
27+ class NodePathSelector extends NodeSelector {
2328 constructor ( path ) {
29+ super ( ) ;
2430 this . value = path ;
2531 }
2632
@@ -34,8 +40,9 @@ class NodePathSelector {
3440 }
3541}
3642
37- class NodeUIDSelector {
43+ class NodeUIDSelector extends NodeSelector {
3844 constructor ( uid ) {
45+ super ( ) ;
3946 this . value = uid ;
4047 }
4148
@@ -58,8 +65,9 @@ class NodeUIDSelector {
5865 }
5966}
6067
61- class NodeInternalIDSelector {
68+ class NodeInternalIDSelector extends NodeSelector {
6269 constructor ( service , id , debugInfo ) {
70+ super ( ) ;
6371 this . service = service ;
6472 this . id = id ;
6573 this . debugInfo = debugInfo ;
@@ -81,23 +89,28 @@ class NodeInternalIDSelector {
8189 }
8290}
8391
84- class NodeChildSelector {
92+ class NodeChildSelector extends NodeSelector {
8593 constructor ( parent , name ) {
94+ super ( ) ;
8695 this . parent = parent ;
8796 this . name = name ;
8897 }
8998
9099 setPropertiesKnownBySelector ( node ) {
91100 node . name = this . name ;
92- // no properties known
101+
102+ try_infer_attributes ( this ) ;
103+ if ( this . path ) {
104+ node . path = this . path ;
105+ }
93106 }
94107
95108 describe ( ) {
96109 return this . parent . describe ( ) + '/' + this . name ;
97110 }
98111}
99112
100- class RootNodeSelector {
113+ class RootNodeSelector extends NodeSelector {
101114 static entry = {
102115 is_dir : true ,
103116 is_root : true ,
@@ -110,6 +123,7 @@ class RootNodeSelector {
110123 node . uid = PuterPath . NULL_UUID ;
111124 }
112125 constructor ( ) {
126+ super ( ) ;
113127 this . entry = this . constructor . entry ;
114128 }
115129
@@ -118,8 +132,9 @@ class RootNodeSelector {
118132 }
119133}
120134
121- class NodeRawEntrySelector {
135+ class NodeRawEntrySelector extends NodeSelector {
122136 constructor ( entry ) {
137+ super ( ) ;
123138 // Fix entries from get_descendants
124139 if ( ! entry . uuid && entry . uid ) {
125140 entry . uuid = entry . uid ;
@@ -145,6 +160,30 @@ class NodeRawEntrySelector {
145160 }
146161}
147162
163+ /**
164+ * Try to infer following attributes for a selector:
165+ * - path
166+ * - uid
167+ *
168+ * @param {NodeSelector } selector
169+ */
170+ function try_infer_attributes ( selector ) {
171+ if ( selector instanceof NodePathSelector ) {
172+ selector . path = selector . value ;
173+ } else if ( selector instanceof NodeUIDSelector ) {
174+ selector . uid = selector . value ;
175+ } else if ( selector instanceof NodeChildSelector ) {
176+ try_infer_attributes ( selector . parent ) ;
177+ if ( selector . parent . path ) {
178+ selector . path = _path . join ( selector . parent . path , selector . name ) ;
179+ }
180+ } else if ( selector instanceof RootNodeSelector ) {
181+ selector . path = '/' ;
182+ } else {
183+ // give up
184+ }
185+ }
186+
148187const relativeSelector = ( parent , path ) => {
149188 if ( path === '.' ) return parent ;
150189 if ( path . startsWith ( '..' ) ) {
@@ -162,11 +201,13 @@ const relativeSelector = (parent, path) => {
162201}
163202
164203module . exports = {
204+ NodeSelector,
165205 NodePathSelector,
166206 NodeUIDSelector,
167207 NodeInternalIDSelector,
168208 NodeChildSelector,
169209 RootNodeSelector,
170210 NodeRawEntrySelector,
171211 relativeSelector,
212+ try_infer_attributes,
172213} ;
0 commit comments