Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ let package = Package(
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"602.0.0"),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
.package(url: "https://github.com/swiftlang/swift-format", from: "600.0.0"),
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
],
targets: [
Expand Down Expand Up @@ -75,7 +74,6 @@ let package = Package(
dependencies: [
"PluginCore", "MacroPlugin", "MetaCodable", "HelperCoders",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosGenericTestSupport", package: "swift-syntax"),
],
plugins: ["MetaProtocolCodable"]
),
Expand Down
2 changes: 0 additions & 2 deletions Package@swift-5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ let package = Package(
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.1.0"..<"602.0.0"),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
.package(url: "https://github.com/swiftlang/swift-format", from: "600.0.0"),
],
targets: [
// MARK: Core
Expand Down Expand Up @@ -86,7 +85,6 @@ let package = Package(
dependencies: [
"PluginCore", "MacroPlugin", "MetaCodable", "HelperCoders", "Testing",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosGenericTestSupport", package: "swift-syntax"),
],
plugins: ["MetaProtocolCodable"]
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import MetaCodable
import HelperCoders
import MetaCodable

@Codable(commonStrategies: [.codedBy(.valueCoder())])
struct User {
let id: Int // Will use ValueCoder
let name: String // Will use ValueCoder
let active: Bool // Will use ValueCoder
let score: Double // Will use ValueCoder
let id: Int // Will use ValueCoder
let name: String // Will use ValueCoder
let active: Bool // Will use ValueCoder
let score: Double // Will use ValueCoder
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import MetaCodable
import HelperCoders
import MetaCodable

@Codable(commonStrategies: [
.codedBy(.valueCoder()),
.codedBy(.sequenceCoder(elementHelper: .valueCoder(), configuration: .lossy))
.codedBy(
.sequenceCoder(elementHelper: .valueCoder(), configuration: .lossy
),
])
struct User {
let id: Int // Will use ValueCoder
let tags: [String] // Will use SequenceCoder with ValueCoder
let scores: [Double] // Will use SequenceCoder with ValueCoder
let id: Int // Will use ValueCoder
let tags: [String] // Will use SequenceCoder with ValueCoder
let scores: [Double] // Will use SequenceCoder with ValueCoder
@CodedBy(ISO8601DateCoder()) // Override with specific coder
let createdAt: Date
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MetaCodable
import HelperCoders
import MetaCodable

@Codable
struct Product {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MetaCodable
import HelperCoders
import MetaCodable

@Codable(commonStrategies: [.codedBy(.valueCoder())])
struct Product {
Expand Down
3 changes: 2 additions & 1 deletion Sources/PluginCore/Attributes/CodedAs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ package struct CodedAs: PropertyAttribute {
/// the identifier to this type.
var type: TypeSyntax? {
return node.attributeName.as(IdentifierTypeSyntax.self)?
.genericArgumentClause?.arguments.first?.argument.as(TypeSyntax.self)
.genericArgumentClause?.arguments
.first?.argument.as(TypeSyntax.self)
}

/// Creates a new instance with the provided node.
Expand Down
8 changes: 8 additions & 0 deletions Sources/TestingMacroPlugin/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,11 @@ struct ExpectThrows: ExpressionMacro {
)
}
}

#if !canImport(SwiftSyntax510)
extension FreestandingMacroExpansionSyntax {
var arguments: LabeledExprListSyntax {
return self.argumentList
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ struct CommonStrategiesValueCoderTests {
// Test encoding
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let encoded = try String(data: encoder.encode(status), encoding: .utf8)
let encoded = try String(
data: encoder.encode(status), encoding: .utf8
)
#expect(encoded == #"{"since":"20250520","type":"active"}"#)

// Test decoding other cases with numeric values
Expand All @@ -425,7 +427,9 @@ struct CommonStrategiesValueCoderTests {
}
"""
let inactiveData = try #require(inactiveJson.data(using: .utf8))
let inactiveStatus = try decoder.decode(Status.self, from: inactiveData)
let inactiveStatus = try decoder.decode(
Status.self, from: inactiveData
)
if case .inactive(let reason) = inactiveStatus {
#expect(reason == "404")
} else {
Expand All @@ -440,7 +444,9 @@ struct CommonStrategiesValueCoderTests {
}
"""
let pendingData = try #require(pendingJson.data(using: .utf8))
let pendingStatus = try decoder.decode(Status.self, from: pendingData)
let pendingStatus = try decoder.decode(
Status.self, from: pendingData
)
if case .pending(let until) = pendingStatus {
#expect(until == "20251231")
} else {
Expand Down Expand Up @@ -553,16 +559,20 @@ struct CommonStrategiesValueCoderTests {

let jsonData = try #require(json.data(using: .utf8))
let decoder = JSONDecoder()
let model = try decoder.decode(ModelWithOverride.self, from: jsonData)
let model = try decoder.decode(
ModelWithOverride.self, from: jsonData
)

#expect(model.id == 42) // Due to CustomIntCoder doubling the value
#expect(model.count == 42) // Normal ValueCoder behavior
#expect(model.id == 42) // Due to CustomIntCoder doubling the value
#expect(model.count == 42) // Normal ValueCoder behavior

// Test encoding
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let encoded = try String(data: encoder.encode(model), encoding: .utf8)
#expect(encoded == #"{"count":42,"id":"21"}"#) // CustomIntCoder halves the value for id
let encoded = try String(
data: encoder.encode(model), encoding: .utf8
)
#expect(encoded == #"{"count":42,"id":"21"}"#) // CustomIntCoder halves the value for id
}

@Test
Expand Down Expand Up @@ -632,11 +642,11 @@ fileprivate struct CustomIntCoder: HelperCoder {
debugDescription: "Could not decode value"
)
}
return intValue * 2 // Double the value during decoding
return intValue * 2 // Double the value during decoding
}

func encode(_ value: Int, to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(String(value / 2)) // Halve the value during encoding
try container.encode(String(value / 2)) // Halve the value during encoding
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"test": "METACODABLE_CI=true npm exec --package=swiftylab-ci -- test.mjs",
"archive": "echo implement",
"generate": "echo implement",
"format": "METACODABLE_CI=true npm exec --package=swiftylab-ci -- format.mjs",
"format": "METACODABLE_CI=true swift format --in-place --recursive .",
"pod-lint": "Utils/pod_lint.rb",
"preview-doc": "METACODABLE_CI=true SPI_GENERATE_DOCS=true npm exec --package=swiftylab-ci -- preview-doc.mjs MetaCodable",
"build-doc": "METACODABLE_CI=true SPI_GENERATE_DOCS=true npm exec --package=swiftylab-ci -- build-doc.mjs MetaCodable HelperCoders",
Expand Down
Loading