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

Surfacing variants for enums #46

Open
steved-stripe opened this issue Nov 22, 2023 · 0 comments
Open

Surfacing variants for enums #46

steved-stripe opened this issue Nov 22, 2023 · 0 comments

Comments

@steved-stripe
Copy link
Contributor

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:

if pt == pgs.EnumT {
if mt == methodTypeGetter {
return "Symbol"
}
return "T.any(Symbol, String, Integer)"
}

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

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

No branches or pull requests

1 participant