Using jscodeshift, Is there a way to extract a node path from a collection if there is only one node path? - jscodeshift

I'm just getting started with writing CodeMods with jscodeshift. I noticed that the find method seems to always return a Collection. If there is only one node path in the collection is there an easy way to extract that node path?
For example, I can easily get the default export, of which there will only ever be one:
j(file.source).find(j.ExportDefaultDeclaration)
However, this returns a collection, which I then need to call forEach on to process the one and only node.
Is there a better way?

It looks like .get(0) will get the first Node Path in a Collection, so
j(file.source).find(j.ExportDefaultDeclaration).get(0);
would return the Node Path in question.

Related

Is there a way to use PathFinder from GraphAlgoFactory with spring-data

I took a look at movies-java-spring-data-neo4j-4 and I love it, but I want to use PathFinder from GraphAlgoFactory with spring-data 4.1.1.RELEASE.
I want to create some sort of Roadmap in my db and use the RestController to find a path between two given nodes.
(Basically the second (A*) example from here: neo4j-docs-graph-algo)
PathFinder<WeightedPath> astar = GraphAlgoFactory.aStar(
PathExpanders.allTypesAndDirections(),
doubleCostEvaluator, estimateEvaluator);
path = astar.findSinglePath(start, end);
findSinglePath needs an Object of org.neo4j.graphdb.Node is there a way to get this from a NodeEntity?
The only way I could find is using the EmbeddedDriver and looking up the node myself:
EmbeddedDriver embeddedDriver = (EmbeddedDriver) Components.driver();
GraphDatabaseService databaseService = embeddedDriver.getGraphDatabaseService();
Node node = databaseService.findNode(...)
Is there an easier way, preferably via HttpDriver?
The HTTP Driver does not provide access to the underlying graph database. You can write a stored procedure and call it via the session/template/repository query methods.
Here are some examples of stored procedures for graph algorithms- https://github.com/neo4j-contrib/neo4j-apoc-procedures/#graph-algorithms-work-in-progress

xpath where && clause

I have xml data like this
<users>
<user email="email#email.com">
<password>pass</password>
..
</user>
</users>
I want to retrieve the user's record (xml format) filtering by email and password.
I have tried
for $user in doc("users")/user
[#email="'.$email.'"]
[password="'.$password.'"]
return $user
but I get errors.
Please, how can I retrieve that user's data in xml format so that I can use something like domdocument or simplexmlparser to get the respective nodes?
My reference was http://www.w3schools.com/xsl/xpath_syntax.asp
You would probably want to combine the constrains to one single constraint
for $user in doc("users")//user[#email="'.$email.'" and password="'.$password.'"] return $user
This could also be achieved in one go by:
doc("users")//user[#email="'.$email.'" and password="'.$password.'"]
HTH,
Peter
The most likely cause of the errors you report is that your query asked for user elements that appear as the outermost element of the users document and which satisfy some further constraints, thus:
doc("users")/user[...][...]
But the XML you show has users, not user, as the outermost element.
In the short run, using ...//user instead of .../user, as is done in prker's answer, will solve this problem. (His recommendation to use only a single predicate makes no sense and has nothing to do with whatever problems you had.) The problem can also be fixed by getting the path right: doc("users")/users/user...
In the long run, you should learn to understand what is happening in your XPath expressions. Here, doc("users") returns the document node at the root of the XML document you specify, and the next step, /user (short for /child::user) asks for an element node named user which is the child of the document node.
It worked with this
'for $user in doc("users")//user
where
$user/password = "'.$password.'" and
$user[#email="'.$email.'"]
return $user ';
Reference from http://www.w3schools.com/xsl/xquery_intro.asp

Google Drive API v3, is there a way to get a list of folders that are parents of a fileId?

In v2 it was possible to make a call to /files with the query fileId in children to get a list of DriveFile objects that were parents of the supplied file.
Now, it seems to be required to make a call to /files/:fileId?fields=parents, then make a separate call to /files/:parentId for each returned parent, possibly turning one call into a dozen.
Is this correct, and if so why? This is a huge performance hit to our app, so hopefully there's an undocumented method.
The query "'fileId' in children'" doesn't publicly exist (not documented/supported) in v2 either and I don't recall it ever existing. What does exist in V2 is the Parents collection which effectively answers the same question. In v3, to get the parents of a file you just get the child and ask for the parents field.
As for whether or not that is a performance hit, I don't think it is in practice. The Parents resource in v2 was very light to begin with, and other than the ID the only useful field was the 'isRoot' property. That you can calculate yourself by calling files/root up front to get the ID of the root folder for that user (just once and save it, it won't change for that user.)
If you need to get more information about the parents than just the IDs and are worried about the # of calls you have to make, use batching to fetch them. If you just have one parent, no need to batch (it's just overhead.) If you find that a file has multiple parents, create a batch request. That'll be sent as a single HTTP request/response and is handled very efficiently on the back end.
Point is, if you just need IDs, it's no worse than before. It's one call to get the parents of a file.
If you need more than IDs, it's at most 2 HTTP requests (outside really bizarre edge cases like 1000+ parents which would exceed the batch size :)
In V3 it is possible to list all children of a parent as it's explained here: https://developers.google.com/drive/v3/web/search-parameters
Example call:
https://www.googleapis.com/drive/v3/files?q=parents in '0Byho0qAdzabmVl8xcDR1S0pNY3c' of course replace spaces with %20, this will list all the files in the folder which has id='0Byho0qAdzabmVl8xcDR1S0pNY3c'
you just need to mention like below:
var request = service.Files.List();
request.Q = "('root' in parents)";
var FileListOfParentOnly = request.Execute();

Getting a level of an ALV tree node?

I created an ALV TREE report, using cl_gui_alv_tree, that has 3 levels. I'm also implementing an event handler for when he double clicks a node.
My problem is that I want to take some actions only when he double clicks a node that is a root node. The event 'node_double_click' gives a node_key, but that's the index of the displayed table. How could I achieve this?
The node ID is not an index, it's the ID you assigned to the node when adding it to the tree.
If possible, I'd suggest switching to CL_SALV_TREE - not only because it is documented
and supported by SAP, but also because it comes with some query methods that are quite handy. These methods are documented as well. You can use, for example, GET_NODE to retrieve a node by its ID and then use GET_PARENT to check whether the node in question is a top-level node or has a parent node it is attached to.
I created a pattern for myself, which i am using.
lv_parent1 = node_key.
while lv_parent1 ne go_Main_tree->C_VIRTUAL_ROOT_NODE.
CALL METHOD go_main_tree->get_parent
EXPORTING
i_node_key = lv_parent1
IMPORTING
e_parent_node_key = lv_parent1.
lv_hierlevel = lv_hierlevel + 1 .
ENDWHILE.
if lv_hierlevel > 2.
“ do what You want to do
endif.

Umbraco: how can i get a node by property value with uQuery?

Maybe someone can help, is it a way to combine an xPath expression to query node by it's property value?
I have a node with custom property "ItemId". I need something like this:
uQuery.GetNodesByXPath("//* [#itemId = '<someId>']")
Thanx in advance!
"//*" Will give you all nodes, so "//*[#itemId = '<someId>']" is asking to give you all nodes with an attribute equal to exactly "<someId>", which you can't have as a valid attribute.
So, if you have <myNode someId='my Id value'></myNode> , then try //*[#itemId='someId']
But remember, this will give you ANY node with that particular attribute ID.
I'm not sure if this is what you're looking for, but please post your XML or a snippet of what you're grabbing, as that will clear up any grey areas.

Resources