fix(swift): identifier objects' initializers must be public
This commit is contained in:
@@ -11,20 +11,20 @@ public struct IdentifierObjectMacro: DeclarationMacro {
|
||||
let uppercasedName = entityNameSyntax.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue ?? ""
|
||||
let lowercasedName = uppercasedName.pascalCaseToCamelCase()
|
||||
let snakeName = lowercasedName.camelCaseToSnakeCase()
|
||||
|
||||
|
||||
return [DeclSyntax("""
|
||||
public let \(raw: lowercasedName): Int
|
||||
public var numericId: Int { \(raw: lowercasedName) }
|
||||
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case tag
|
||||
case \(raw: lowercasedName) = \(literal: snakeName)
|
||||
}
|
||||
|
||||
init(_ id: Int) {
|
||||
|
||||
public init(_ id: Int) {
|
||||
self.\(raw: lowercasedName) = id
|
||||
}
|
||||
|
||||
|
||||
public init(from decoder: any Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
let tag = try container.decode(String.self, forKey: .tag)
|
||||
@@ -37,9 +37,9 @@ public struct IdentifierObjectMacro: DeclarationMacro {
|
||||
|
||||
//enum IdentifierObjectError: String, DiagnosticMessage {
|
||||
// case onlyApplicableToEnum
|
||||
//
|
||||
//
|
||||
// var severity: DiagnosticSeverity { return .error }
|
||||
//
|
||||
//
|
||||
// var message: String {
|
||||
// switch self {
|
||||
// case .onlyApplicableToEnum: return "'@DiscriminatedEnum' can only be applied to an 'enum'"
|
||||
@@ -47,7 +47,7 @@ public struct IdentifierObjectMacro: DeclarationMacro {
|
||||
// case .invalidAssociatedType: return "A case with associated values may only have one parameter, which must be unnamed."
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// var diagnosticID: MessageID {
|
||||
// MessageID(domain: "OptolithDatabaseSchemaMacros", id: rawValue)
|
||||
// }
|
||||
@@ -56,21 +56,21 @@ public struct IdentifierObjectMacro: DeclarationMacro {
|
||||
extension String {
|
||||
func camelCaseToSnakeCase() -> String {
|
||||
guard !self.isEmpty else { return self }
|
||||
|
||||
|
||||
let withSimpleTransformed = self.replacing(/([A-Z][a-z])/) { match in
|
||||
"_\(match.output.1.lowercased())"
|
||||
}
|
||||
|
||||
|
||||
let withAllCapsTransformed = withSimpleTransformed.replacing(/([A-Z]+)/) { match in
|
||||
"_\(match.output.1.lowercased())"
|
||||
}
|
||||
|
||||
|
||||
return withAllCapsTransformed
|
||||
}
|
||||
|
||||
|
||||
func pascalCaseToCamelCase() -> String {
|
||||
guard !self.isEmpty else { return self }
|
||||
|
||||
|
||||
return self.first!.lowercased() + self.dropFirst()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user