Linter rulesProtobuffield-naming

field-naming

Since 0.9.0

This rule checks whether the name of every field of a Protobuf file is in lower_snake_case.

Rule Details

Code generators derive the accessor names and the JSON names of fields from their Protobuf names, assuming lower_snake_case: owner_name becomes ownerName in JSON and getOwnerName() in Java. Other conventions produce surprising names, and can make the JSON names of two fields collide.

The field-naming rule checks every field, including the fields of oneofs, map fields and extension fields, and reports Field name '<name>' should be lower_snake_case for every name that is not in lower_snake_case. A name is in lower_snake_case when it starts with a lower-case letter and contains only lower-case letters, digits and single underscores, with no underscore at the end, such as owner_name or address_2.

When the name converted to lower_snake_case differs from the original and is itself valid, the message ends with a suggestion, for example , e.g. '<converted>'. To convert a name, it is split into words at underscores and other separators, at lower-to-upper case changes and before the last capital of an acronym, and the words are joined in lower case with underscores, so ownerName becomes owner_name and HTTPServer becomes http_server.

Configuration

This rule has no options.

Examples

Good.

syntax = "proto3";

package acme.pets.v1;

message Pet {
  string owner_name = 1;
  map<string, string> labels = 2;
}

Bad. The linter reports Field name 'ownerName' should be lower_snake_case, e.g. 'owner_name'.

syntax = "proto3";

package acme.pets.v1;

message Pet {
  string ownerName = 1;
  map<string, string> labels = 2;
}

When Not to Use It

You may disable this rule if the names are part of an existing API that you cannot rename. To allow a single field, add an // apigenie-lint-disable field-naming comment to it.

Compatibility

This rule is compatible with proto3 files. proto2 and editions files are rejected by the reader and are not linted.

Related rules