protobuf-rules-gen icon indicating copy to clipboard operation
protobuf-rules-gen copied to clipboard

Keep '_' in attribute name in generated code

Open nilsreichardt opened this issue 4 years ago • 4 comments

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@@

nilsreichardt avatar May 27 '21 17:05 nilsreichardt

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.

rockwotj avatar May 27 '21 18:05 rockwotj

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

nilsreichardt avatar May 27 '21 19:05 nilsreichardt

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

rockwotj avatar May 28 '21 02:05 rockwotj

@rockwotj thanks for chiming in here!

samtstern avatar Jun 01 '21 09:06 samtstern