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

Add support for union to enum #117

Merged
merged 15 commits into from
Jan 9, 2024
Merged

Conversation

miku1958
Copy link
Collaborator

Description

Add basic support to convert string/number literal union to enum, for example:

interface FullSize extends BaseSize, CustomSize {
  stringUnion1: 'A1' | 'B1';
  numUnion1: 11 | 21;
}

⬇️

public struct FullSize: Codable {
  public var stringUnion1: FullSizeStringUnion1
  public var numUnion1: FullSizeNumUnion1
}

public enum FullSizeStringUnion1: String, Codable {
  case a1 = "A1"
  case b1 = "B1"
}

public enum FullSizeNumUnion1: Int, Codable {
  case _11 = 11
  case _21 = 21
}

Known issues

Since mustache does not allow to nest mustache, it will lead to a dead loop, so it can't be simply/neatly converted to:

public struct FullSize: Codable {
  public enum StringUnion1: String, Codable {
    case a1 = "A1"
    case b1 = "B1"
  }

  public var stringUnion1: tringUnion1
}

For simplicity of implementation, it is generated at the outermost level.

src/types.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
@zhuorantan zhuorantan requested a review from Foolment December 26, 2023 06:21
@zhuorantan
Copy link
Contributor

Can you update the documentation to reflect the change of behaviours in this PR?

src/types.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
zhuorantan
zhuorantan previously approved these changes Dec 26, 2023
@zhuorantan zhuorantan merged commit cfdf7b4 into microsoft:main Jan 9, 2024
5 checks passed
@miku1958 miku1958 deleted the union2enum branch May 7, 2024 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants