enum-value-prefix

Since 0.9.0 · In the rulesets: Recommended at warning, Strict at error, All at error

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.

Turn the rule off 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.

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;
}

Turn it off or change its severity

In apigenie.yaml, off turns the rule off and warning or error sets its severity; a rule with options takes the severity and the options as a pair, [warning, options]. lint.protobuf has the details.

commands:
  lint:
    protobuf:
      rules:
        enum-value-prefix: off

Related rules

Was this helpful?