Skip to content

Commit

Permalink
minor refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 12, 2024
1 parent b9f4d82 commit 25a90cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
15 changes: 15 additions & 0 deletions Sources/EnumeratorMacro/Enumerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ public macro Enumerator(_ templates: StaticString...) = #externalMacro(
module: "EnumeratorMacroImpl",
type: "EnumeratorMacroType"
)

@Enumerator("""
var caseName: String {
switch self {
{{#cases}}
case .{{name}}: "{{name}}"
{{/cases}}
}
}
""")
enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}
24 changes: 8 additions & 16 deletions Sources/EnumeratorMacroImpl/Types/EParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ extension EParameters: MustacheTransformable {
return convertToCustomTypesIfPossible(defaultTransformed)
} else {
switch name {
case "joined":
let joined = self.underlying
.map(\.name)
.enumerated()
.map { $1?.underlying ?? "param\($0 + 1)" }
.joined(separator: ", ")
let string = EString(joined)
return string
case "names":
let names = self.map(\.name)
let array = EOptionalsArray(underlying: names)
return array
case "types":
let types = self.map(\.type)
let array = EArray(underlying: types)
return array
case "namesAndTypes":
let namesAndTypes = self
.enumerated()
Expand All @@ -54,14 +54,6 @@ extension EParameters: MustacheTransformable {
let array = EArray(underlying: namesAndTypes)
return array
}
case "names":
let names = self.map(\.name)
let array = EOptionalsArray(underlying: names)
return array
case "types":
let types = self.map(\.type)
let array = EArray(underlying: types)
return array
default:
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/EnumeratorMacroImpl/Types/EString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension EString: MustacheTransformable {
} else {
switch name {
case "firstCapitalized":
if self.isEmpty {
if self.isEmpty || self[self.startIndex].isUppercase {
return self
}
let modified = self[self.startIndex].uppercased() + self[self.index(after: self.startIndex)...]
Expand Down

0 comments on commit 25a90cd

Please sign in to comment.