Firebase Realtime DB Rules - firebase

Does anyone know if it possible to parse a value trying to be written to the DB?
EX.
".write" : "(root.child('Game/' + newData.val().replace(':%d', '') + '/isPlayed').val() == false)"
the input would: 2022090800:29
So I would like to remove everything at and past the :29
OR can I only do the evaluation on the first 10 chars of the value with the following:
".write" : "(root.child('Game/' + newData.val().length(10) + '/isPlayed').val() == false)"

Related

mongolite: how to perform a LIKE query?

I want to perform a partial match query on a MongoDB in R. I've tried to specify a query that matches the MongoDB query format like so:
library(mongolite)
foo <- mongo(url = "myConnectionString")
bar <- foo$find(
query = '{"_id": /idContainsThis/}',
fields = '{}'
)
But when I try this, I get the following error:
Error: Invalid JSON object: {"_id": /idContainsThis/}
I can't use this solution because if I put quotes round the term, the / is taken as a string literal, not the wildcard I need.
Does anyone know how to make this work with mongolite?
You'll have to use the regex function like this
query = '{"_id": { "$regex" : "idContainsThis", "$options" : "i" }}'
The "$options" : "i" is in case you want it to be case insensitive.
However I am not sure if this will work on an _id

Using the replace function in firestore security rules

I'm struggling with Firestore security rules. I want to check on a value that needs the replace function, i.e. an e-mail address. I can find some documentation in the general security docs, but that does not seem to work with Firestore.
For example this works:
allow write: if resource.data.members.data[(request.auth.token.email)] in ["admin"];
but this doesn't (and I changed the key in the members object accordingly):
allow write: if resource.data.members.data[(request.auth.token.email.replace('.' , ',')] in ["admin"];
Another option would be to have a way to use dots in the address of a query, so they don't have to be replaced like this:
var emailSanitized = email.replace('.' , '.');
db.collection('someCollection').where('members.' + emailSanitized, '==', 'admin')
Any ideas here?
A little late, but you can simulate the replace function on a string with this one :
function replace(string, replace, by) {
return string.split(replace).join(by);
}
So you need to define this function in your firestore.rules file and then you can call replace(request.auth.token.email, '.' , ',') to get the same result as request.auth.token.email.replace('.' , ',') in javascript.
There are two reasons why you might have been having issues.
The replace function was added to Security Rules after you asked your question.
The replace function uses regular expressions for the first argument and so matching on '.' will match literally everything.
Consider instead using: request.auth.token.email.replace('\\.' , ',')
var emailSanitized = email.replace('.' , '.');
db.collection('someCollection').where('members.' + emailSanitized, '==', 'admin')

parsing variable composed with lettre and numbers like " JAVAC 1.7.0.XXX"

I'm trying to parse regular expressions using JavaCC but I encountered a problem with variable " Y " composed of lettre and number for exemple : " JAVA 1.7.1.XXX" . knowing that I have already defined the Token
<id > = < lettre > | <number> < #lettre : [ "A"-"Z", "a"-"z"]> | < #number : [ "0"-"9" ] > in execution, the parser processing the first part of the variable " Y " like as <id>. after the parsing is stops. Thanks in advance.
Edit.
Here code parseur.jj:
TOKEN : { <ID2 : (["a"-"z","A"-"Z","0"-"9","_"])+
( (["0"-"9"])+ "." (["0"-"9"])+ "." (["0"-"9"])+)+
(["a"-"z","A"-"Z","_","."])+ >}
TOKEN : { <ID : ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","0"-"9","_"])* >}
Suppose the remaining input steams starts with this : MyFile1_Test 1.2.3.txt
then the token <ID> is attributed ?
and not <ID2>. normaly, why this rules not appilcatble : If more than one regular expression describes a prefix, then a regular expression that describes the longest prefix of the input stream is used. (This
is called the “maximal munch rule”.) thank you very much for your help
Here is the parseur.jj code:
TOKEN : { <ID2 : (["a"-"z","A"-"Z","0"-"9","_"])+ ( (["0"-"9"])+ "." (["0"-"9"])+ "." (["0"-"9"])+)+ (["a"-"z","A"-"Z","_","."])+ >}
TOKEN : { <ID : ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","0"-"9","_"])* >}
Suppose the remaining input steams starts with: MyFile1_Test 1.2.3.txt
then the token <ID> is attributed and not <ID2>. Normaly, why this rules not applicable:
If more than one regular expression describes a prefix, then a regular expression that describes the longest prefix of the input stream is used. (This is called the “maximal munch rule”.)

How to access and mutate node property value by the property name string in Cypher?

My goal is to access and mutate a property of a node in a cypher query where the name of the property to be accessed and mutated is an unknown string value.
For example, consider a command:
Find all nodes containing a two properties such that the name of the first property is lower-case and the name of the latter is the upper-case representation of the former. Then, propagate the value of the property with the lower-case string name to the value of the property with the upper-case name.
The particular case is easy:
MATCH ( node )
WHERE has(node.age) AND has(node.AGE) AND node.age <> node.AGE
SET node.AGE = node.age
RETURN node;
But I can't seem to find a way to implement the general case in a single request.
Specifically, I am unable to:
Access the property of the node with a string and a value
Mutate the property of the node with a string and a value
For the sake of clarity, I'll include my attempt to handle the general case. Where I failed to modify the property of the node I was able to generate the cypher for a command that would accomplish my end goal if it were executed in a subsequent transaction.
MERGE ( justToMakeSureOneExists { age: 14, AGE : 140 } ) WITH justToMakeSureOneExists
MATCH (node)
WHERE ANY ( kx IN keys(node) WHERE kx = LOWER(kx) AND ANY ( ky in keys(node) WHERE ky = UPPER(kx) ) )
REMOVE node.name_conflicts // make sure results are current
FOREACH(kx in keys(node) |
SET node.name_conflicts
= COALESCE(node.name_conflicts,[])
+ CASE kx
WHEN lower(kx)
THEN []
+ CASE WHEN any ( ky in keys(node) WHERE ky = upper(kx) )
THEN ['match (node) where id(node) = ' + id(node)+ ' and node.' + upper(kx) + ' <> node.' + kx + ' set node.' + upper(kx) + ' = node.' + kx + ' return node;']
ELSE [] END
ELSE []
END )
RETURN node,keys(node)
Afterthought: It seems like the ability to mutate a node property by property name would be a pretty common requirement, but the lack of obvious support for the feature leads me to believe that the feature was omitted deliberately? If this feature is indeed unsupported is there any documentation to explain why and if there is some conflict between the approach and the recommended way of doing things in Neo/Cypher?
There is some discussion going on regarding improved support for dynamic property access in Cypher. I'm pretty confident that we will see support for this in the future, but I cannot comment on a target release nor on a date.
As a workaround I'd recommend implementing that into a unmanaged extension.
It appears that the desired language feature was added to Cypher in Neo4j 2.3.0 under the name "dynamic property". The Cypher docs from version 2.3.0-up declare the following syntax group as a valid cypher expression:
A dynamic property: n["prop"], rel[n.city + n.zip], map[coll[0]].
This feature is documented for 2.3.0 but is absent from the previous version (2.2.9).
Thank you Neo4j Team!

Using ternary operator to output a string containing whitespace in Razor

I'm trying to use a ternary operator in Razor, similar to this question, but what I want to output contains whitespace. This code
#(selectedGoal == null ? "" : "value=" + selectedGoal.Name)
should produce
value="Goal 3"
as the value of selectedGoal.Name is "Goal 3". Instead, I get
value="Goal" 3
which is no good. I've tried a bunch of different combinations of escaped quotes, # symbols and no # symbols, and I just can't get this to work, i.e.
#(selectedGoal == null ? "" : "value=" + "selectedGoal.Name")
#(selectedGoal == null ? "" : "value=#selectedGoal.Name")
and then I just get something like
value="selectedGoal.Name"
Anyone know how this should be done?
Your value attribute is missing its own quotes, so they are being automatically added before the space. Try moving value outside of the expression.
value="#(selectedGoal == null ? "" : selectedGoal.Name)"
What about
#(selectedGoal == null ? "" : "value=\"" + selectedGoal.Name + \")
Or you can try rendering them directly as an HTML block, using my method on
Html literal in Razor ternary expression

Resources