square / square/wire

Simplify code for ProtoAdapters of types with no fields

Open
#757 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Example:

companion object {
    @JvmField
    val ADAPTER: ProtoAdapter<NodeEvent> = object : ProtoAdapter<NodeEvent>(
        FieldEncoding.LENGTH_DELIMITED,
        NodeEvent::class.java
    ) {
        override fun decode(reader: ProtoReader): NodeEvent {
            val unknownFields = reader.forEachTag { tag ->
                when (tag) {
                    else -> TagHandler.UNKNOWN_TAG
                }
            }
   ...
}

The when block is unnecessary, since NodeEvent doesn't have any fields. This can be simplified to:

val unknownFields = reader.forEachTag { TagHandler.UNKNOWN_TAG }

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

Locate the ProtoAdapter generation for types with no fields and compare it with the NodeEvent example in the issue. Check existing generated-code tests or fixtures for empty types, then verify that the generated adapter uses the simplified tag handler and that the relevant tests still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.