From fcae7cc639be3a93f948593923c0fb543f98be03 Mon Sep 17 00:00:00 2001 From: Davide Date: Thu, 5 Feb 2026 07:34:20 +0100 Subject: [PATCH] Support integer variants --- Sources/SWONMacros/Decoding.swift | 8 +++++--- Sources/SWONMacros/Encoding.swift | 6 ++++-- Tests/SWONTests/ComplexStruct.swift | 2 ++ Tests/SWONTests/Resources/populated.json | 3 ++- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Sources/SWONMacros/Decoding.swift b/Sources/SWONMacros/Decoding.swift index 42ac486..97c2ab8 100644 --- a/Sources/SWONMacros/Decoding.swift +++ b/Sources/SWONMacros/Decoding.swift @@ -115,7 +115,8 @@ struct SWONDecodeMacro: MemberMacro { } self = value """) - case "Int", "UInt": + case "Int", "Int8", "Int16", "Int32", "Int64", + "UInt", "UInt8", "UInt16", "UInt32", "UInt64": assignments.append(""" var num: Int32 = 0 try swon_get_integer(&num, root).check("\(enumType)") @@ -196,12 +197,13 @@ private extension SWONDecodeMacro { \(checkExpr) return String(cString: str) """ - case "Int": + case "Int", "Int8", "Int16", "Int32", "Int64", + "UInt", "UInt8", "UInt16", "UInt32", "UInt64": return """ var num: Int32 = 0 let result = swon_get_integer(&num, \(varName)) \(checkExpr) - return Int(num) + return \(typeName)(num) """ case "Double": return """ diff --git a/Sources/SWONMacros/Encoding.swift b/Sources/SWONMacros/Encoding.swift index 71c8f66..ae6b90b 100644 --- a/Sources/SWONMacros/Encoding.swift +++ b/Sources/SWONMacros/Encoding.swift @@ -149,7 +149,8 @@ struct SWONEncodeMacro: MemberMacro { throw SWONError.invalid("\(enumType)(String)") } """) - case "Int", "UInt": + case "Int", "Int8", "Int16", "Int32", "Int64", + "UInt", "UInt8", "UInt16", "UInt32", "UInt64": assignments.append(""" var root = swon_t() guard swon_create_number(&root, Double(rawValue)) else { @@ -399,7 +400,8 @@ private extension DecodedType { switch description { case "Bool": stmts.append("guard swon_create_bool(&\(name), \(element)\(suffix)) else {") - case "Int": + case "Int", "Int8", "Int16", "Int32", "Int64", + "UInt", "UInt8", "UInt16", "UInt32", "UInt64": stmts.append("guard swon_create_number(&\(name), Double(\(element)\(suffix))) else {") case "Double": stmts.append("guard swon_create_number(&\(name), \(element)\(suffix)) else {") diff --git a/Tests/SWONTests/ComplexStruct.swift b/Tests/SWONTests/ComplexStruct.swift index 536a916..97b7e97 100644 --- a/Tests/SWONTests/ComplexStruct.swift +++ b/Tests/SWONTests/ComplexStruct.swift @@ -50,6 +50,8 @@ struct ComplexStruct: Equatable, Codable { let stringToSubStruct: [String: SubStruct] let optionalStringToSubStruct: [String: SubStruct]? let optionalStringToSubStructArray: [String: [SubStruct]]? + + let uint16: UInt16? } @SWON diff --git a/Tests/SWONTests/Resources/populated.json b/Tests/SWONTests/Resources/populated.json index cde5242..3459b9b 100644 --- a/Tests/SWONTests/Resources/populated.json +++ b/Tests/SWONTests/Resources/populated.json @@ -130,5 +130,6 @@ } ] }] - } + }, + "uint16": 60000 }