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

Easier to use oneof=union generated code #1060

Closed
bhollis opened this issue Jun 11, 2024 · 1 comment · Fixed by #1062 · May be fixed by habyyb/CD-AdvancedDigitalIQ#3
Closed

Easier to use oneof=union generated code #1060

bhollis opened this issue Jun 11, 2024 · 1 comment · Fixed by #1062 · May be fixed by habyyb/CD-AdvancedDigitalIQ#3
Labels

Comments

@bhollis
Copy link
Contributor

bhollis commented Jun 11, 2024

Today, if I enable oneof=union, my union might generate code like this:

type?:
  | { $case: "float"; float: FloatOptions }
  | { $case: "double"; double: DoubleOptions }
  | { $case: "int32"; int32: Int32Options }

This can be really difficult to deal with in code, because the name of the property depends on the value of $case. For example:

switch (v.$case) {
  case "float":
  case "double":
    // I can't write generic code to handle both, because one is accessed with v.float, while the other is v.double!
	break;
  case "int32":
    console.log(v.int32.whatever);
    break;
}

This also inspired a lot of the complex type helpers I use (see #1034).

A nicer codegen would be:

type?:
  | { $case: "float"; value: FloatOptions }
  | { $case: "double"; value: DoubleOptions }
  | { $case: "int32"; value: Int32Options }

Which then allows:

switch (v.$case) {
  case "float":
  case "double":
    console.log(v.value.whateverFloat);
	break;
  case "int32":
    console.log(v.value.whateverInt);
    break;
}

This would have to be a new option to avoid breaking previously generated code.

stephenh pushed a commit that referenced this issue Jun 15, 2024
# [1.180.0](v1.179.0...v1.180.0) (2024-06-15)

### Features

* oneof=unions-value to use the same field name for oneof cases ([#1062](#1062)) ([7493090](7493090)), closes [#1060](#1060)
@stephenh
Copy link
Owner

🎉 This issue has been resolved in version 1.180.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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