Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Optimize constant subexpressions in boolean and ternary operators #101

Open
mm-gmbd opened this issue Jan 8, 2016 · 1 comment
Open

Comments

@mm-gmbd
Copy link

mm-gmbd commented Jan 8, 2016

I have a generic function that I use called isAuthType which takes a single input argument and the return value is the result of a ternary operator:

function isAuthType(type) {
  return (type == 'type1' ? true : root.users[auth.uid].type == type);
}

That function is used as a rule, and I would expect the following input/output:

path /path/A {
  read()  = isAuth('type1')
}

path /path/B {
  read()  = isAuthType('admin')
}

//Expected rules
"rules": {
  "path": {
    "A": {
      ".read": true
    },
    "B": {
      ".read": root.child('users').child(auth.uid).child('type').val() == 'admin'
    },
  }
}

However, the ternary operator, which can be evaluated (in some cases, I can see that it wouldn't if it was based on non-explicit variable), is not evaluated and I'm left with the following, clunky rules:

"rules": {
  "path": {
    "A": {
      ".read": 'type1' == 'type1' ? true : root.child('users').child(auth.uid).child('type').val() == 'admin'
    },
    "B": {
      ".read": 'admin' == 'type1' ? true : root.child('users').child(auth.uid).child('type').val() == 'admin'
    },
  }
}

I'll probably separate this into two functions in the meantime to clean things up, but having these evaluate would be handy.

@mm-gmbd mm-gmbd changed the title Optimize bolt to evaluation of ternary operators Optimize bolt evaluate of ternary operators Jan 8, 2016
@mm-gmbd mm-gmbd changed the title Optimize bolt evaluate of ternary operators Optimize bolt evaluate ternary operators Jan 8, 2016
@mckoss
Copy link
Contributor

mckoss commented Jan 8, 2016

Nice idea. I have a pull request that contains a quite extensive expression optimizer with in. Once that lands, we should be able to handle this case using it.

@mckoss mckoss changed the title Optimize bolt evaluate ternary operators Optimize constant subexpressions in boolean and ternary operators Jan 15, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants