Skip to content

Surfacing variants for enums #46

Open
@steved-stripe

Description

@steved-stripe

Problem

With the generated types for enums it's tricky to figure out what the possible variants are without spelunking through .proto files or other usages in the codebase.

For example, with the following enum:

enum Mode {
  INVALID_MODE = 0;
  LIVEMODE = 1;
  TESTMODE = 2;
}

the generated .rbi doesn't help when figuring out the possible variants:

class Common::Feature < ::Google::Protobuf::AbstractMessage
  sig do
    params(
      mode: T.nilable(T.any(Symbol, String, Integer)),
    ).void
  end
  def initialize(
    mode: :INVALID_MODE,
  )
  end

  sig { returns(Symbol) }
  def mode
  end

  sig { params(value: T.any(Symbol, String, Integer)).void }
  def mode=(value)
  end
end

relevant generator code:

https://github.com/coinbase/protoc-gen-rbi/blob/f96debeaa9464f96ca75034d3b7631304357306a/ruby_types/ruby_types.go#L141-L146

Ideas

Add a comment with the Symbol variants:

  # variants: :INVALID_MODE, :LIVEMODE, :TESTMODE
  sig { params(value: T.any(Symbol, String, Integer)).void }
  def mode=(value)
  end

somehow express the union in the type itself

but also keep the more general Symbol type since other variants could be passed in

not sure about this one

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions