Skip to content

Commit

Permalink
add index to cases
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 15, 2024
1 parent b5d5980 commit 6da19bf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ Here's a sample context object:
{
"cases": [
{
"index": 0,
"name": "caseName",
"parameters": {
"name": "parameterName",
Expand Down
4 changes: 3 additions & 1 deletion Sources/EnumeratorMacroImpl/Types/ECase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import SwiftSyntax
import Mustache

struct ECase {
let index: Int
let name: EString
let parameters: EParameters

init(from element: EnumCaseElementSyntax) throws {
init(index: Int, from element: EnumCaseElementSyntax) throws {
self.index = index
self.name = .init(element.name.trimmedDescription)
let parameters = element.parameterClause?.parameters ?? []
self.parameters = .init(
Expand Down
4 changes: 3 additions & 1 deletion Sources/EnumeratorMacroImpl/Types/ECases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ struct ECases {

init(elements: [EnumCaseElementSyntax]) throws {
self.underlying = .init(
underlying: try elements.map(ECase.init(from:))
underlying: try elements.enumerated().map { idx, element in
try ECase(index: idx, from: element)
}
)
}
}
Expand Down

0 comments on commit 6da19bf

Please sign in to comment.