FirebaseExtended / protobuf-rules-gen

This is an experimental protoc plugin that generates Firebase Rules for Cloud Firestore based on Google's Protocol Buffer format. This allows you to easily validate your data in a platform independent manner.
Apache License 2.0
197 stars 12 forks source link

Keep '_' in attribute name in generated code #40

Open nilsreichardt opened 3 years ago

nilsreichardt commented 3 years ago

Description

If you have an underscore (_) as an attribute name, then it will be transformed into camel case. I didn't except this.

Steps to reproduce

syntax = "proto3";

message Notification {
    map<string, string> fcm_token = 1;
    bool receive_notifications = 2;
}

will be generated into

// @@START_GENERATED_FUNCTIONS@@
function isNotificationMessage(resource) {
  return resource.keys().hasAll([]) &&
          (resource.keys().hasOnly(['receiveNotifications','fcmToken'])) &&
          ((!resource.keys().hasAny(['fcmToken'])) || (resource.fcmToken is map)) &&
          ((!resource.keys().hasAny(['receiveNotifications'])) || (resource.receiveNotifications is bool));
}
// @@END_GENERATED_FUNCTIONS@@

Is there a way to keep the _ in the attribute name? My excepted out is:

// @@START_GENERATED_FUNCTIONS@@
function isNotificationMessage(resource) {
  return resource.keys().hasAll([]) &&
          (resource.keys().hasOnly(['receive_notifications','fcm_token'])) &&
          ((!resource.keys().hasAny(['fcm_token'])) || (resource.fcm_token is map)) &&
          ((!resource.keys().hasAny(['receive_notifications'])) || (resource.receive_notifications is bool));
}
// @@END_GENERATED_FUNCTIONS@@
rockwotj commented 3 years ago

There is a way to do this, right now we use the proto3 json mapping to get the names. You'd have to add an option to support keeping the original names around.

nilsreichardt commented 3 years ago

Ok, nice. What option is this exactly and how to use it?

rockwotj commented 3 years ago

Sorry I wasn't clear this is pretty straightforward to do, you have to change the code that looks at json_name and replace it with name in the places where it's used via a file option. There are existing examples of file options and here is one place we assume json naming

https://github.com/FirebaseExtended/protobuf-rules-gen/blob/d413219bca50d96b58247d2c5b4981d9182b28b2/firebase_rules_generator/generator.cc#L98

samtstern commented 3 years ago

@rockwotj thanks for chiming in here!