Skip to content

Commit

Permalink
Remove leading '_' from generated functions (#4)
Browse files Browse the repository at this point in the history
When stripping the package name off the message and enum names, there
was a '.' character still before the message name.
  • Loading branch information
rockwotj authored Jan 30, 2018
1 parent 12ad860 commit d06e640
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ script:
- bazel build //...
- bazel test //...

after_failure:
- cat bazel-testlogs/**/*.log # Print test log on failure.


notifications:
email: false
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ that can be used to validate your incoming data.

```javascript
// @@START_GENERATED_FUNCTIONS@@
function is_PersonMessage(resource) {
function isPersonMessage(resource) {
return ((resource.keys().hasAll(['name']) && resource.size() == 1)) ||
(resource.keys().hasAll(['name','email']) && resource.size() == 2)) ||
(resource.keys().hasAll(['name','phone']) && resource.size() == 2)) ||
Expand All @@ -60,15 +60,15 @@ function is_PersonMessage(resource) {
((resource.email == null) || (resource.email is string)) &&
((resource.phone == null) || (is_Person_PhoneNumberMessage(resource.phone)));
}
function is_Person_PhoneNumberMessage(resource) {
function isPerson_PhoneNumberMessage(resource) {
return ((resource.keys().hasAll([]) && resource.size() == 0)) ||
(resource.keys().hasAll(['number']) && resource.size() == 1)) ||
(resource.keys().hasAll(['type']) && resource.size() == 1)) ||
(resource.keys().hasAll(['type','number']) && resource.size() == 2))) &&
((resource.number == null) || (resource.number is string)) &&
((resource.type == null) || (is_Person_PhoneTypeEnum(resource.type)));
}
function is_Person_PhoneTypeEnum(resource) {
function isPerson_PhoneTypeEnum(resource) {
return resource == "MOBILE" ||
resource == "HOME" ||
resource == "WORK";
Expand Down
22 changes: 12 additions & 10 deletions firebase_rules_generator/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@ namespace {

struct RulesContext {};

std::string SanitizeName(std::string name) {
for (size_t i = 0; i < name.size(); ++i) {
if (name[i] == '.') {
name[i] = '_';
}
}
return name;
}

std::string StripPrefix(std::string str, const std::string &prefix) {
std::string StripPrefix(const std::string &str, const std::string &prefix) {
if (prefix.size() <= str.size() && str.substr(0, prefix.size()) == prefix) {
return str.substr(prefix.size());
} else {
return str;
}
}

std::string SanitizeName(const std::string &name) {
// Strip leading '.' characters.
std::string sanitized_name = StripPrefix(name, ".");
for (size_t i = 0; i < sanitized_name.size(); ++i) {
if (sanitized_name[i] == '.') {
sanitized_name[i] = '_';
}
}
return sanitized_name;
}

void ReturnIndent(protobuf::io::Printer &printer) {
for (int i = 0; i < 4; ++i) {
printer.Indent();
Expand Down
2 changes: 1 addition & 1 deletion testdata/test0.rules
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @@START_GENERATED_FUNCTIONS@@
function is_ExampleMessage(resource) {
function isExampleMessage(resource) {
return ((resource.keys().hasAll([]) && resource.size() == 0)) ||
(resource.keys().hasAll(['foo']) && resource.size() == 1)) ||
(resource.keys().hasAll(['bar']) && resource.size() == 1)) ||
Expand Down
2 changes: 1 addition & 1 deletion testdata/test1.rules
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @@START_GENERATED_FUNCTIONS@@
function is_ExampleMessage(resource) {
function isExampleMessage(resource) {
return ((resource.keys().hasAll(['foo']) && resource.size() == 1)) ||
(resource.keys().hasAll(['foo','bar']) && resource.size() == 2)) ||
(resource.keys().hasAll(['foo','baz']) && resource.size() == 2)) ||
Expand Down
4 changes: 2 additions & 2 deletions testdata/test3.rules
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @@START_GENERATED_FUNCTIONS@@
function is_ExampleWithStringsEnum(resource) {
function isExampleWithStringsEnum(resource) {
return resource == "FOO" ||
resource == "BAR" ||
resource == "BAZ";
}
function is_ExampleWithNumbersEnum(resource) {
function isExampleWithNumbersEnum(resource) {
return resource == 0 ||
resource == 1 ||
resource == 2;
Expand Down
2 changes: 1 addition & 1 deletion testdata/test4.rules
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @@START_GENERATED_FUNCTIONS@@
function is_ExampleMessage(resource) {
function isExampleMessage(resource) {
return ((resource.keys().hasAll([]) && resource.size() == 0)) ||
(resource.keys().hasAll(['foo']) && resource.size() == 1)) ||
(resource.keys().hasAll(['bar']) && resource.size() == 1)) ||
Expand Down
10 changes: 5 additions & 5 deletions testdata/test5.rules
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// @@START_GENERATED_FUNCTIONS@@
function is_PersonMessage(resource) {
function isPersonMessage(resource) {
return ((resource.keys().hasAll(['name']) && resource.size() == 1)) ||
(resource.keys().hasAll(['name','email']) && resource.size() == 2)) ||
(resource.keys().hasAll(['name','phone']) && resource.size() == 2)) ||
(resource.keys().hasAll(['name','phone','email']) && resource.size() == 3))) &&
((resource.name is string)) &&
((resource.email == null) || (resource.email is string)) &&
((resource.phone == null) || (is_Person_PhoneNumberMessage(resource.phone)));
((resource.phone == null) || (isPerson_PhoneNumberMessage(resource.phone)));
}
function is_Person_PhoneNumberMessage(resource) {
function isPerson_PhoneNumberMessage(resource) {
return ((resource.keys().hasAll([]) && resource.size() == 0)) ||
(resource.keys().hasAll(['number']) && resource.size() == 1)) ||
(resource.keys().hasAll(['type']) && resource.size() == 1)) ||
(resource.keys().hasAll(['type','number']) && resource.size() == 2))) &&
((resource.number == null) || (resource.number is string)) &&
((resource.type == null) || (is_Person_PhoneTypeEnum(resource.type)));
((resource.type == null) || (isPerson_PhoneTypeEnum(resource.type)));
}
function is_Person_PhoneTypeEnum(resource) {
function isPerson_PhoneTypeEnum(resource) {
return resource == "MOBILE" ||
resource == "HOME" ||
resource == "WORK";
Expand Down
2 changes: 1 addition & 1 deletion testdata/test7.rules
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @@START_GENERATED_FUNCTIONS@@
function is_ExampleMessage(resource) {
function isExampleMessage(resource) {
return ((resource.keys().hasAll([]) && resource.size() == 0)) ||
(resource.keys().hasAll(['foo']) && resource.size() == 1)) ||
(resource.keys().hasAll(['bar']) && resource.size() == 1)) ||
Expand Down

0 comments on commit d06e640

Please sign in to comment.