Skip to content

Commit

Permalink
mit enum
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrohrer committed Oct 12, 2024
1 parent e8048c2 commit a3b351c
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Sources/MyMacroMacros/MyMacroMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ func getTypeName(_ type: String) -> String {
}


// Helper function to check if a type is RawRepresentable (i.e., an enum with raw values)
func isRawRepresentableEnum(_ type: String, context: some MacroExpansionContext) -> Bool {
return context.lookupType(named: type)?.conforms(to: "RawRepresentable") == true
}



public struct SynthCodableMacro: MemberMacro {
Expand Down Expand Up @@ -94,6 +89,12 @@ public struct SynthCodableMacro: MemberMacro {
}
return false
}
let isEnum = variableDecl.attributes.contains { attribute in
if let attributeSyntax = attribute.as(AttributeSyntax.self) {
return attributeSyntax.attributeName.description.contains("Enum")
}
return false
}

if let typeAnnotation = binding.typeAnnotation {
let propertyType = typeAnnotation.type.description
Expand Down Expand Up @@ -133,6 +134,18 @@ public struct SynthCodableMacro: MemberMacro {
convenienceInitParameters.append("\(propertyName): \(propertyType)")
convenienceInitAssignments.append("self.\(propertyName) = \(propertyName)")
}

// Handle enums with raw values
else if isEnum {
initFromDecoderStatements.append("self.\(propertyName) = \(nonOptionalPropertyType)(rawValue: try container.decode(\(nonOptionalPropertyType).RawValue.self, forKey: .\(propertyName)))!")
encodeStatements.append("try container.encode(\(propertyName).rawValue, forKey: .\(propertyName))")

// Add to convenience initializer
convenienceInitParameters.append("\(propertyName): \(propertyType)")
convenienceInitAssignments.append("self.\(propertyName) = \(propertyName)")
}


// Regular attributes
else {
initFromDecoderStatements.append("self.\(propertyName) = try container.decode(\(propertyType).self, forKey: .\(propertyName))")
Expand Down

0 comments on commit a3b351c

Please sign in to comment.