Skip to content

Commit

Permalink
Implement a nullable annotation for fields and messages (#26)
Browse files Browse the repository at this point in the history
* Implement a nullable annotation for fields and messages

* Add back deleted tests
  • Loading branch information
rockwotj authored Sep 4, 2018
1 parent ca8082b commit 3dbf0e2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion firebase_rules_generator/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,15 @@ bool RulesGenerator::GenerateField(const protobuf::FieldDescriptor *field,
if (options.has_validate()) {
printer.Print(" && ($validate$)", "validate", options.validate());
}
printer.Print("))");
printer.Print(")");
const auto &msg_options =
field->containing_type()->options().GetExtension(firebase_rules_message);
bool nullable =
(!options.has_nullable() && msg_options.nullable()) || options.nullable();
if (nullable) {
printer.Print(" || resource.$name$ is null", "name", field->json_name());
}
printer.Print(")");
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions proto/firebase_rules_options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import "google/protobuf/descriptor.proto";
message RulesMessageOptions {
optional string validate = 1;
optional bool extra_properties = 2 [default = false];
optional bool nullable = 3 [default = false];
}

message RulesFieldOptions {
optional string validate = 1;
optional bool reference_type = 2 [default = false];
optional bool nullable = 3 [default = false];
}

message RulesFileOptions {
Expand Down
17 changes: 17 additions & 0 deletions testdata/testA.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package example;

import "firebase_rules_options.proto";

message NullableExample {
string foo = 1;
string bar = 2
[(google.firebase.rules.firebase_rules_field).nullable = false];
option (google.firebase.rules.firebase_rules_message).nullable = true;
}

message Example {
string foo = 1;
string bar = 2 [(google.firebase.rules.firebase_rules_field).nullable = true];
}
14 changes: 14 additions & 0 deletions testdata/testA.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @@START_GENERATED_FUNCTIONS@@
function isNullableExampleMessage(resource) {
return resource.keys().hasAll([]) &&
(resource.keys().hasOnly(['bar','foo'])) &&
((!resource.keys().hasAny(['foo'])) || (resource.foo is string) || resource.foo is null) &&
((!resource.keys().hasAny(['bar'])) || (resource.bar is string));
}
function isExampleMessage(resource) {
return resource.keys().hasAll([]) &&
(resource.keys().hasOnly(['bar','foo'])) &&
((!resource.keys().hasAny(['foo'])) || (resource.foo is string)) &&
((!resource.keys().hasAny(['bar'])) || (resource.bar is string) || resource.bar is null);
}
// @@END_GENERATED_FUNCTIONS@@

0 comments on commit 3dbf0e2

Please sign in to comment.