Skip to content

Commit

Permalink
add isLast/isFirst to cases and parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 19, 2024
1 parent 56f950e commit a48468b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 40 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,26 +436,32 @@ Here's a sample context object:
{
"cases": [
{
"index": 0,
"name": "somethingWentWrong",
"parameters": [
{
"index": 0,
"name": "error",
"type": "Error?",
"isOptional": true
"isOptional": true,
"index": 0,
"isFirst": true,
"isLast": false
},
{
"index": 1,
"name": "statusCode",
"type": "String",
"isOptional": false
"isOptional": false,
"index": 1,
"isFirst": false,
"isLast": true
}
],
"comments": [
"business_error",
"l8n_params: error as Any, statusCode"
]
],
"index": 0,
"isFirst": true,
"isLast": true
}
]
}
Expand Down
37 changes: 26 additions & 11 deletions Sources/EnumeratorMacroImpl/Types/ECase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,60 @@ import Foundation
import Mustache

struct ECase {
let index: EInt
let name: EString
let parameters: EParameters
let comments: EArray<EString>
let index: EInt
let isFirst: Bool
let isLast: Bool

init(index: Int, from element: EnumCaseElementSyntax) throws {
self.index = .init(index)
init(
from element: EnumCaseElementSyntax,
index: Int,
isFirst: Bool,
isLast: Bool
) throws {
self.index = EInt(index)
self.isFirst = isFirst
self.isLast = isLast

self.name = .init(element.name.trimmedDescription)
let parameters = element.parameterClause?.parameters ?? []
let lastIdx = parameters.count - 1
self.parameters = .init(
underlying: parameters.enumerated().map { idx, parameter in
EParameter(
from: parameter,
index: idx,
parameter: parameter
isFirst: idx == 0,
isLast: idx == lastIdx
)
}
)

let keyValueParts = element.trailingTrivia
.description
.replacingOccurrences(of: "///", with: "") /// remove comment signs
.replacingOccurrences(of: "//", with: "") /// remove comment signs
.split(separator: ";") /// separator of parameters
.map {
$0.trimmingCharacters(in: .whitespaces)
}

.map { $0.trimmingCharacters(in: .whitespaces) }
self.comments = .init(underlying: keyValueParts.map(EString.init(stringLiteral:)))
}

init(
index: Int,
name: EString,
parameters: EParameters,
comments: [EString]
comments: [EString],
index: Int,
isFirst: Bool,
isLast: Bool
) {
self.index = EInt(index)
self.name = name
self.parameters = parameters
self.comments = .init(underlying: comments)
self.index = EInt(index)
self.isFirst = isFirst
self.isLast = isLast
}
}

Expand Down
12 changes: 10 additions & 2 deletions Sources/EnumeratorMacroImpl/Types/ECases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ struct ECases {
}

init(elements: [EnumCaseElementSyntax]) throws {
let lastIdx = elements.count - 1
self.underlying = .init(
underlying: try elements.enumerated().map { idx, element in
try ECase(index: idx, from: element)
underlying: try elements.enumerated().map {
idx,
element in
try ECase(
from: element,
index: idx,
isFirst: idx == 0,
isLast: idx == lastIdx
)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Protocol for sequence that can be sorted
/// Protocol for sequences that can be sorted
protocol EComparableSequence {
func comparableTransform(_ name: String) -> Any?
}
25 changes: 22 additions & 3 deletions Sources/EnumeratorMacroImpl/Types/EParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ struct EParameter {
let name: EString
let type: EString
let isOptional: Bool
let isFirst: Bool
let isLast: Bool

init(index: Int, parameter: EnumCaseParameterSyntax) {
init(
from parameter: EnumCaseParameterSyntax,
index: Int,
isFirst: Bool,
isLast: Bool
) {
self.index = EInt(index)
self.isFirst = isFirst
self.isLast = isLast

let parameterName = (parameter.secondName ?? parameter.firstName)?.trimmedDescription
if let parameterName,
!parameterName.isEmpty {
Expand All @@ -19,11 +29,20 @@ struct EParameter {
self.isOptional = parameter.type.isOptional
}

init(index: EInt, name: EString, type: EString, isOptional: Bool) {
self.index = index
init(
name: EString,
type: EString,
isOptional: Bool,
index: Int,
isFirst: Bool,
isLast: Bool
) {
self.name = name
self.type = type
self.isOptional = isOptional
self.index = EInt(index)
self.isFirst = isFirst
self.isLast = isLast
}
}

Expand Down
8 changes: 5 additions & 3 deletions Tests/EnumeratorMacroTests/EParameterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ final class EParameterTests: XCTestCase {
type: String
) throws -> EParameter {
EParameter(
index: .random(in: .min ... .max),
parameter: EnumCaseParameterSyntax(
from: EnumCaseParameterSyntax(
firstName: firstName,
secondName: secondName,
type: try makeTypeSyntax(for: type)
)
),
index: .random(in: .min ... .max),
isFirst: .random(),
isLast: .random()
)
}

Expand Down
42 changes: 28 additions & 14 deletions Tests/EnumeratorMacroTests/TransformTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,46 +63,60 @@ final class TransformTests: XCTestCase {

let testCases: [ECase] = [
ECase(
index: 0,
name: "case1",
parameters: .init(underlying: []),
comments: ["bool_value"]
comments: ["bool_value"],
index: 0,
isFirst: true,
isLast: false
),
ECase(
index: 1,
name: "case2",
parameters: .init(underlying: []),
comments: []
comments: [],
index: 1,
isFirst: false,
isLast: false
),
ECase(
index: 2,
name: "case3",
parameters: .init(underlying: [EParameter(
index: EInt(0),
name: "thing",
type: "String",
isOptional: false
isOptional: false,
index: 0,
isFirst: true,
isLast: true
)]),
comments: []
comments: [],
index: 2,
isFirst: false,
isLast: false
),
ECase(
index: 3,
name: "case4",
parameters: .init(underlying: [
EParameter(
index: EInt(0),
name: "error",
type: "Error",
isOptional: false
isOptional: false,
index: 0,
isFirst: true,
isLast: false
),
EParameter(
index: EInt(1),
name: "critical",
type: "Bool",
isOptional: false
isOptional: false,
index: 1,
isFirst: false,
isLast: true
)
]),
comments: ["bool_value", "custom_params:"]
comments: ["bool_value", "custom_params:"],
index: 3,
isFirst: false,
isLast: true
)
]
}

0 comments on commit a48468b

Please sign in to comment.