Linter rulesProtobufenum-value-prefix

enum-value-prefix

Since 0.9.0

This rule checks whether the names of the values of every enum start with the name of the enum in UPPER_SNAKE_CASE, for example PET_KIND_DOG in the enum PetKind.

Rule Details

Protobuf enum values follow C++ scoping rules: they are siblings of their enum, not children of it. Two enums of the same package or message therefore cannot both have a value named UNKNOWN or OTHER. Prefixing every value with the name of its enum avoids such clashes, and keeps the values unambiguous in the generated code of languages without scoped enums.

The enum-value-prefix rule converts the name of each enum to UPPER_SNAKE_CASE and appends an underscore, and reports Enum value '<value>' should start with '<prefix>' for every value whose name does not start with this prefix. The name is split into words at underscores, at lower-to-upper case changes and before the last capital of an acronym, so the prefix of HTTPMethod is HTTP_METHOD_.

The prefix is made of the name of the enum only, also for enums nested in messages.

Configuration

This rule has no options.

Examples

Good.

syntax = "proto3";

package acme.pets.v1;

enum PetKind {
  PET_KIND_UNSPECIFIED = 0;
  PET_KIND_DOG = 1;
  PET_KIND_CAT = 2;
}

Bad. The linter reports Enum value 'UNSPECIFIED' should start with 'PET_KIND_', Enum value 'DOG' should start with 'PET_KIND_' and Enum value 'CAT' should start with 'PET_KIND_'.

syntax = "proto3";

package acme.pets.v1;

enum PetKind {
  UNSPECIFIED = 0;
  DOG = 1;
  CAT = 2;
}

When Not to Use It

You may disable this rule if the enum values are part of an existing API that you cannot rename. To allow the values of a single enum, add an // apigenie-lint-disable enum-value-prefix comment to the enum.

Compatibility

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

Related rules