Skip to content

Commit

Permalink
some cleanup and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 25, 2024
1 parent cba979c commit 9b0396d
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 139 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/swiftlang/swift-syntax",
from: "510.0.0"
"510.0.0" ..< "601.0.0"
),
.package(
url: "https://github.com/hummingbird-project/swift-mustache",
Expand Down
139 changes: 139 additions & 0 deletions Sources/EnumeratorMacro/Enumerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,142 @@ public macro Enumerator(
module: "EnumeratorMacroImpl",
type: "EnumeratorMacroType"
)

@Enumerator("""
var caseName: String {
switch self {
{{#cases}}
case .{{name}}: "{{#first(parameters)}} {{name}} {{/first(parameters)}}"
{{/cases}}
}
}
""")
enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}

public protocol LocalizationServiceProtocol {
static func localizedString(language: String, term: String, parameters: Any...) -> String
}

package enum SharedConfiguration {
package enum Env: String {
case local
case testing
case prod
}

package static var env: Env { fatalError() }
}

@Enumerator(allowedComments: ["business_error", "l8n_params"],
"""
public enum Subtype: String, Equatable {
{{#cases}}
case {{name}}
{{/cases}}
}
""",
"""
public var subtype: Subtype {
switch self {
{{#cases}}
case .{{name}}:
.{{name}}
{{/cases}}
}
}
""",
"""
public var errorCode: String {
switch self {
{{#cases}}
case .{{name}}:
"ERROR-{{plusOne(index)}}"
{{/cases}}
}
}
""",
"""
public var loggerMetadata: [String: String] {
switch self {
{{#cases}} {{^isEmpty(parameters)}}
case let .{{name}}{{withParens(joined(names(parameters)))}}:
[
"caseName": self.caseName,
{{#names(parameters)}}
"case_{{.}}": String(reflecting: {{.}}),
{{/names(parameters)}}
]
{{/isEmpty(parameters)}} {{/cases}}
default:
["caseName": self.caseName]
}
}
""",
"""
private var localizationParameters: [Any] {
switch self {
{{#cases}} {{^isEmpty(parameters)}}
{{^isEmpty(l8n_params(comments))}}
case let .{{name}}{{withParens(joined(names(parameters)))}}:
[{{l8n_params(comments)}}]
{{/isEmpty(l8n_params(comments))}}
{{^exists(l8n_params(comments))}}
case let .{{name}}{{withParens(joined(names(parameters)))}}:
[
{{#parameters}}
{{name}}{{#isOptional}} as Any{{/isOptional}},
{{/parameters}}
]
{{/exists(l8n_params(comments))}}
{{/isEmpty(parameters)}} {{/cases}}
default:
[]
}
}
""")
@Enumerator(
allowedComments: ["business_error", "l8n_params"],
#"""
package var isBusinessLogicError: Bool {
switch self {
case
{{#cases}}{{#bool(business_error(comments))}}
.{{name}},
{{/bool(business_error(comments))}}{{/cases}}
:
return true
default:
return false
}
}
"""#
)
public enum ErrorMessage {
public static let localizationServiceType: LocalizationServiceProtocol.Type? = nil

case allergenAlreadyAdded // business_error
case alreadyOngoingInventory
case apiKeyWithoutEnoughPermission(integration: String, other: Bool?, Int)
case databaseError(error: Error, isConstraintViolation: Bool) // business_error; l8n_params:

public var caseName: String {
self.subtype.rawValue
}

public func toString(_ language: String) -> String {
let translation = Self.localizationServiceType?.localizedString(
language: language,
term: "api.\(self.caseName)",
parameters: self.localizationParameters
)
return translation ?? (SharedConfiguration.env == .testing ? String(reflecting: self) : "<localization failed to load for \(self.caseName)>")
}
}

114 changes: 0 additions & 114 deletions Sources/EnumeratorMacroImpl/Types/EOptionalsArray.swift

This file was deleted.

Loading

0 comments on commit 9b0396d

Please sign in to comment.