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

Changed enum code to allow simple value types, and a rawValue constructor #97

Merged
merged 6 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Assets/Tests/KotlinTokenizer/enums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,42 @@ public sealed class SDKException : Error {
object unauthorized : SDKException()
data class network(val v1: HttpResponse, val v2: Error?) : SDKException()
}
public enum class PaymentMethodType (val rawValue: String) : Equatable {
direct("DIRECT"), creditCard("CREDIT_CARD");

companion object {
operator fun invoke(rawValue: String) = PaymentMethodType.values().firstOrNull { it.rawValue == rawValue }
}
}
enum class AnimationLength {
shot,
long

val duration: Double
get() {
when (this) {
.shot -> return 2
.long -> return 5.0
}
}

fun getDuration() : Double =
this.duration
}
sealed class AnimationLengthAdvanced {
object shot : AnimationLengthAdvanced()
object long : AnimationLengthAdvanced()
data class custom(val v1: Double) : AnimationLengthAdvanced()

val duration: Double
get() {
when (this) {
.shot -> return 2
.long -> return 5.0
.custom -> return duration
}
}

fun getDuration() : Double =
this.duration
}
42 changes: 42 additions & 0 deletions Assets/Tests/KotlinTokenizer/enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,45 @@ public enum SDKException: Error {
case unauthorized
case network(HttpResponse, Error?)
}

public enum PaymentMethodType: String, Equatable {
case direct = "DIRECT", creditCard = "CREDIT_CARD"
}

enum AnimationLength {
case shot
case long
var duration: Double {
switch self {
case .shot:
return 2
case .long:
return 5.0
}
}

func getDuration() -> Double {
return self.duration
}
}

enum AnimationLengthAdvanced {
case shot
case long
case custom(Double)

var duration: Double {
switch self {
case .shot:
return 2
case .long:
return 5.0
case .custom(let duration):
return duration
}
}

func getDuration() -> Double {
return self.duration
}
}
73 changes: 10 additions & 63 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,80 +406,26 @@ public class KotlinTokenizer: SwiftTokenizer {
let unionCases = declaration.members.compactMap { $0.unionStyleEnumCase }
let simpleCases = unionCases.flatMap { $0.cases }
let lineBreak = declaration.newToken(.linebreak, "\n")
let space = declaration.newToken(.space, " ")

guard unionCases.count == declaration.members.count &&
guard unionCases.count <= declaration.members.count && // unionCases is 0 when enums have specific values
declaration.genericParameterClause == nil &&
declaration.genericWhereClause == nil else {
return self.unsupportedTokens(message: "Complex enums not supported yet", element: declaration, node: declaration).suffix(with: lineBreak) +
super.tokenize(declaration)
}

// Simple enums (no tuples)
if !simpleCases.contains(where: { $0.tuple != nil }) && declaration.typeInheritanceClause == nil {
let attrsTokens = tokenize(declaration.attributes, node: declaration)
let modifierTokens = declaration.accessLevelModifier.map { tokenize($0, node: declaration) } ?? []
let headTokens = [
attrsTokens,
modifierTokens,
[declaration.newToken(.keyword, "enum")],
[declaration.newToken(.keyword, "class")],
[declaration.newToken(.identifier, declaration.name)],
].joined(token: space)

let membersTokens = simpleCases.map { c in
return [c.newToken(.identifier, c.name, declaration)]
}.joined(tokens: [
declaration.newToken(.delimiter, ","),
lineBreak
])

return headTokens +
[space, declaration.newToken(.startOfScope, "{"), lineBreak] +
indent(membersTokens) +
[lineBreak, declaration.newToken(.endOfScope, "}")]
// Simple enums (no tuple values)
if !simpleCases.contains(where: { $0.tuple != nil }) {
if declaration.typeInheritanceClause != nil {
return tokenizeSimpleValueEnum(declaration:declaration, simpleCases: simpleCases)
} else {
return tokenizeNoValueEnum(declaration: declaration, simpleCases: simpleCases)
}
}
// Tuples or inhertance required sealed classes
else {
let attrsTokens = tokenize(declaration.attributes, node: declaration)
let modifierTokens = declaration.accessLevelModifier.map { tokenize($0, node: declaration) } ?? []
let inheritanceTokens = declaration.typeInheritanceClause.map { tokenize($0, node: declaration) } ?? []
let headTokens = [
attrsTokens,
modifierTokens,
[declaration.newToken(.keyword, "sealed")],
[declaration.newToken(.keyword, "class")],
[declaration.newToken(.identifier, declaration.name)],
inheritanceTokens
].joined(token: space)

let membersTokens = simpleCases.map { c in
var tokenSections: [[Token]]
if let tuple = c.tuple {
tokenSections = [
[c.newToken(.keyword, "data", declaration)],
[c.newToken(.keyword, "class", declaration)],
[c.newToken(.identifier, c.name, declaration)] + tokenize(tuple, node: declaration)
]
} else {
tokenSections = [
[c.newToken(.keyword, "object", declaration)],
[c.newToken(.identifier, c.name, declaration)]
]
}
tokenSections += [
[c.newToken(.symbol, ":", declaration)],
[c.newToken(.identifier, declaration.name, declaration), c.newToken(.startOfScope, "(", declaration), c.newToken(.endOfScope, ")", declaration)]
]
return tokenSections.joined(token: space)
}.joined(token: lineBreak)

return headTokens +
[space, declaration.newToken(.startOfScope, "{"), lineBreak] +
indent(membersTokens) +
[lineBreak, declaration.newToken(.endOfScope, "}")]
return tokenizeSealedClassEnum(declaration: declaration, simpleCases: simpleCases)
}

}

open override func tokenize(_ codeBlock: CodeBlock) -> [Token] {
Expand Down Expand Up @@ -1121,6 +1067,7 @@ public class KotlinTokenizer: SwiftTokenizer {
}
return newTokens
}

}

public typealias InvertedConditionList = [InvertedCondition]
Expand Down
Loading