-
-
Notifications
You must be signed in to change notification settings - Fork 17
Enum inferred raw value #30
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,27 +4,58 @@ import SwiftSyntax | |
| public struct EnumCase { | ||
| public var _syntax: EnumCaseElementSyntax | ||
|
|
||
| public init(_ syntax: EnumCaseElementSyntax) { | ||
| public init(_ syntax: EnumCaseElementSyntax, rawRepresentableType: EnumRawRepresentableType? = nil, precedingCase: EnumCase? = nil) { | ||
| _syntax = syntax | ||
| value = if let rawValue = _syntax.rawValue { | ||
| .rawValue(rawValue) | ||
| } else if let associatedValue = _syntax.parameterClause { | ||
| .associatedValue(Array(associatedValue.parameters).map(EnumCaseAssociatedValueParameter.init)) | ||
| } else if let rawRepresentableType { | ||
| switch rawRepresentableType { | ||
| case .string: .inferredRawValue(.init(value: "\"\(raw: _syntax.name.text)\"" as ExprSyntax)) | ||
| case .character: nil // Characters cannot be inferred | ||
| case .integer, .floatingPoint: .inferredRawValue(.init(value: "\(raw: (previousValue() ?? -1) + 1)" as ExprSyntax)) | ||
| } | ||
| } else { | ||
| nil | ||
| } | ||
|
|
||
| /// Raw representable conformance is only synthesized when using integer literals (eg 1), not float literals (eg 1.0). | ||
| func previousValue() -> Int? { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this helper function to the top of the function to make things read a bit nicer. |
||
| precedingCase?.rawValue.flatMap(IntegerLiteral.init)?.value | ||
| } | ||
| } | ||
|
|
||
| /// The case's name | ||
| public var identifier: String { | ||
| _syntax.name.withoutTrivia().description | ||
| } | ||
|
|
||
| /// The value associated with the enum case (either associated or raw). | ||
| public var value: EnumCaseValue? { | ||
| if let rawValue = _syntax.rawValue { | ||
| return .rawValue(rawValue) | ||
| } else if let associatedValue = _syntax.parameterClause { | ||
| let parameters = Array(associatedValue.parameters) | ||
| .map(EnumCaseAssociatedValueParameter.init) | ||
| return .associatedValue(parameters) | ||
| } else { | ||
| return nil | ||
|
|
||
| /// The value associated with the enum case (either associated, raw or inferred). | ||
| public var value: EnumCaseValue? | ||
|
|
||
| /// Helper that gets the associated values from `EnumCase.value` or returns an empty array. | ||
| public var associatedValues: [EnumCaseAssociatedValueParameter] { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this should be renamed to make it clear that it's the types of the associated values and not actual values.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To what?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe associatedValueParameters? (given that they include labels as well) |
||
| switch value { | ||
| case .associatedValue(let values): values | ||
| default: [] | ||
| } | ||
| } | ||
|
|
||
| /// Helper that gets the raw or inferred raw value from `EnumCase.value` or returns nil. | ||
| public var rawValue: ExprSyntax? { | ||
| switch value { | ||
| case .rawValue(let initializer), .inferredRawValue(let initializer): initializer.value | ||
| default: nil | ||
| } | ||
| } | ||
|
|
||
| /// Helper that gets the raw or inferred raw value text, eg "value", 1, or 1.0. | ||
| public var rawValueText: String? { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this essentially equivalent to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had no idea that was possible so just my inexperience with swift syntax showing. |
||
| rawValue.flatMap(StringLiteral.init)?.value.map { "\"\($0)\"" } ?? | ||
| rawValue.flatMap(IntegerLiteral.init)?.value.description ?? | ||
| rawValue.flatMap(FloatLiteral.init)?.value.description | ||
| } | ||
|
|
||
| public func withoutValue() -> Self { | ||
| EnumCase(_syntax.with(\.rawValue, nil).with(\.parameterClause, nil)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import SwiftSyntax | ||
|
|
||
| /// Enum raw values can be strings, characters, or any of the integer or floating-point number types. | ||
| public enum EnumRawRepresentableType { | ||
| case string(syntax: IdentifierTypeSyntax) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every case has the same associated value, so I feel it may be better to turn this enum into a struct with a nested |
||
| case character(syntax: IdentifierTypeSyntax) | ||
| case integer(syntax: IdentifierTypeSyntax) | ||
| case floatingPoint(syntax: IdentifierTypeSyntax) | ||
|
|
||
| init?(possibleRawType syntax: InheritedTypeSyntax?) { | ||
| guard let type = syntax?.type.as(IdentifierTypeSyntax.self) else { return nil } | ||
| switch type.name.text { | ||
| case "String", "NSString": | ||
| self = .string(syntax: type) | ||
| case "Character": | ||
| self = .character(syntax: type) | ||
| case "Int", "Int8", "Int16", "Int32", "Int64", "Int128", | ||
| "UInt", "UInt8", "UInt16", "UInt32", "UInt64", "UInt128": | ||
| self = .integer(syntax: type) | ||
| case "Float", "Float16", "Float32", "Float64", | ||
| "Double", "CGFloat", "NSNumber": | ||
| self = .floatingPoint(syntax: type) | ||
| default: return nil | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the values (after the colons) onto new lines to reduce line length and make things a bit easier to skim