square / square/wire

FieldOptions.wire_name to dodge name collisions on options

Open
#3,042 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
4.4k
Forks
627
Avg merge
3d 15m
Merged PRs (30d)
20

Description

We’ve got a problem where Wire generates broken code when the same option name is used for different targets in the same package.

extend google.protobuf.MethodOptions {
  optional float objective = 26101;
}

extend google.protobuf.ServiceOptions {
  optional float objective = 26101;
}

This generates two annotations that potentially collide.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ObjectiveOption {
  float value();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ObjectiveOption {
  float value();
}

I really like the name ObjectiveOption here and am reluctant to qualify the name with its target ObjectiveMethodOption because this collision is just so rare. Instead I’d like to introduce a mechanism where developers who introduce such collisions in the .proto can choose disambiguating names.

We’ll create a new extension:

extend google.protobuf.FieldOptions {
  /**
   * Sets the name used for this field in generated code.
   *
   * This may only be applied to extension fields, and only impacts the generated names for option
   * annotations.
   *
   * It is an error to apply this option to regular message fields and oneof fields.
   *
   * This doesn't impact the name used an any encoding or decoding, such as JSON encoding.
   */
  optional string wire_name = 9999; // TODO
}

And users would use it like so:

extend google.protobuf.MethodOptions {
  optional float objective = 26101 [(wire.wire_name) = "rpc_objective"];
}

extend google.protobuf.ServiceOptions {
  optional float objective = 26101 [(wire.wire_name) = "service_objective"];
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the code that defines Wire's protobuf extensions and the generator for option annotations. Trace how extension field names become generated annotation names, then add coverage for the two colliding options and for rejecting the option on regular and oneof fields. Done means the declared wire_name values disambiguate generated annotations without changing encoding or decoding names.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.