From d06e640e9f87ba3751e4d1c98bb3fe87ea2fa375 Mon Sep 17 00:00:00 2001 From: Tyler Rockwood Date: Tue, 30 Jan 2018 09:36:57 -0800 Subject: [PATCH] Remove leading '_' from generated functions (#4) When stripping the package name off the message and enum names, there was a '.' character still before the message name. --- .travis.yml | 4 ++++ README.md | 6 +++--- firebase_rules_generator/generator.cc | 22 ++++++++++++---------- testdata/test0.rules | 2 +- testdata/test1.rules | 2 +- testdata/test3.rules | 4 ++-- testdata/test4.rules | 2 +- testdata/test5.rules | 10 +++++----- testdata/test7.rules | 2 +- 9 files changed, 30 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3de490f..0af7506 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,5 +31,9 @@ script: - bazel build //... - bazel test //... +after_failure: + - cat bazel-testlogs/**/*.log # Print test log on failure. + + notifications: email: false diff --git a/README.md b/README.md index 696dfc5..9868b97 100644 --- a/README.md +++ b/README.md @@ -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)) || @@ -60,7 +60,7 @@ 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)) || @@ -68,7 +68,7 @@ function is_Person_PhoneNumberMessage(resource) { ((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"; diff --git a/firebase_rules_generator/generator.cc b/firebase_rules_generator/generator.cc index 76bef71..a5490fd 100644 --- a/firebase_rules_generator/generator.cc +++ b/firebase_rules_generator/generator.cc @@ -33,16 +33,7 @@ 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 { @@ -50,6 +41,17 @@ std::string StripPrefix(std::string str, const std::string &prefix) { } } +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(); diff --git a/testdata/test0.rules b/testdata/test0.rules index e446232..90fe627 100644 --- a/testdata/test0.rules +++ b/testdata/test0.rules @@ -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)) || diff --git a/testdata/test1.rules b/testdata/test1.rules index 51d9243..dcafc73 100644 --- a/testdata/test1.rules +++ b/testdata/test1.rules @@ -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)) || diff --git a/testdata/test3.rules b/testdata/test3.rules index dadde9d..072d7d4 100644 --- a/testdata/test3.rules +++ b/testdata/test3.rules @@ -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; diff --git a/testdata/test4.rules b/testdata/test4.rules index d04f19f..150c657 100644 --- a/testdata/test4.rules +++ b/testdata/test4.rules @@ -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)) || diff --git a/testdata/test5.rules b/testdata/test5.rules index a0a0a1a..323ca10 100644 --- a/testdata/test5.rules +++ b/testdata/test5.rules @@ -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"; diff --git a/testdata/test7.rules b/testdata/test7.rules index e446232..90fe627 100644 --- a/testdata/test7.rules +++ b/testdata/test7.rules @@ -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)) ||