Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated Kotlin code adds unnecessary underscore #3157

Closed
simonsickle opened this issue Oct 23, 2024 · 3 comments
Closed

Generated Kotlin code adds unnecessary underscore #3157

simonsickle opened this issue Oct 23, 2024 · 3 comments

Comments

@simonsickle
Copy link

Context

I have a simple proto file that is essentially representing a map with a name and value field.

message TestMap {
  optional string name = 2;
  optional string value = 3;
}

I expect the generated Kotlin code to look something like

Actual

The generated Kotlin code is generated with an underscore appended to the end of the value property name.

public class TestMap(
  @field:WireField(
    tag = 2,
    adapter = "com.squareup.wire.ProtoAdapter#STRING",
    schemaIndex = 0,
  )
  public val name: String? = null,
  @field:WireField(
    tag = 3,
    adapter = "com.squareup.wire.ProtoAdapter#STRING",
    declaredName = "value",
    schemaIndex = 1,
  )
  public val value_: String? = null,
  unknownFields: ByteString = ByteString.EMPTY,
) : Message<TestMap, Nothing>(ADAPTER, unknownFields)

Expected

I expect value to not contain an underscore since it is not a keyword as a property name.

public class TestMap(
  @field:WireField(
    tag = 2,
    adapter = "com.squareup.wire.ProtoAdapter#STRING",
    schemaIndex = 0,
  )
  public val name: String? = null,
  @field:WireField(
    tag = 3,
    adapter = "com.squareup.wire.ProtoAdapter#STRING",
    declaredName = "value",
    schemaIndex = 1,
  )
  public val value: String? = null,
  unknownFields: ByteString = ByteString.EMPTY,
) : Message<TestMap, Nothing>(ADAPTER, unknownFields)
@oldergod
Copy link
Member

The _ is adding by KotlinPoet which does so because value can be a keyword depending on its usage.
If you want backtick instead, you can use escapeKotlinKeywords = true in the kotlin {} block like so

wire {
  kotlin {
    escapeKotlinKeywords = true
  }
}

@simonsickle
Copy link
Author

Since it isn't a keyword in this scenario, escaping doesn't seem like the correct behavior to me (and the conventions plugin here won't let me configure wire as far as I can tell, no one internally seems to be using escapeKotlinKeywords).

That said, this issue is in the KotlinPoet project, not Wire.

@simonsickle
Copy link
Author

Further context for the next person, square/kotlinpoet#994

This seems to be expected behavior to avoid having to write all of the compiler rules into KotlinPoet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants