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

feat(go): Unions include runtime validation #5403

Merged
merged 2 commits into from
Dec 12, 2024
Merged

Conversation

amckinney
Copy link
Contributor

Add runtime validation for discriminated unions to prevent users from accidentally sending the wrong type of value. With this, users will be expected to set exactly one of the union's values like so:

package example

type Animal struct {
  Type string
  Cat  *Cat
  Dog  *Dog
}

func do() {
  union := &Animal{
    Cat: &Cat{
      Name: "Fluffy",
    },
  }
}

If the user sets both Cat and Dog, the user will receive an error when the type is serialized to JSON (i.e. in the json.Marshaler implementation).

@amckinney amckinney requested a review from dsinghvi as a code owner December 12, 2024 20:56
Copy link
Member

@dsinghvi dsinghvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@amckinney amckinney merged commit 643ba46 into main Dec 12, 2024
52 checks passed
@amckinney amckinney deleted the amckinney/go/validate branch December 12, 2024 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants