Create a macro that can automatically implement Defaults.Serializable
for user-defined custom structures.
Implementing Defaults.Serializable
for custom structures every time can be annoying and leads to redundant code.
For nested structures, developers need to implement it for each struct individually.
Having a macro to handle this automatically would greatly improve efficiency and reduce repetitive work.
Here is an example of the macro, currently named @DefaultSerializable
.
@DefaultsSerializable
struct User {
let name: String
let age: Int
}
// Generated
extension User: Defaults.Serializable {
static let bridge = UserBridge()
struct UserBridge: Defaults.Bridge {
typealias Value = User
typealias Serializable = [String: String]
public func serialize(_ value: Value?) -> Serializable? {
guard let value else {
return nil
}
return [
"name": value.name,
"age": value.age
]
}
public func deserialize(_ object: Serializable?) -> Value? {
guard
let object,
let name = object["name"],
let age = object["age"]
else {
return nil
}
return User(
name: name,
age: age
)
}
}
}
Since this is a large macro, I would like to split the implementation into the following stages:
Implement @DefaultSerializable
only.
Implement common used field attributes such as skip
(The member should be an Optional to apply skip attribute).
Implement skip_serializaing_if/skip_deserializaing_if
(Not quite sure whether this can be achieved or not; further investigation is needed.)
Implement rename
-related fields (though I’m not entirely sure whether this will be useful in our case, as we should always use Defaults
to access them 🤔)."
The ultimate goal is to implement all attributes mentioned in the serde attributes.
Note that this is still in the early stages.
Any feedback is welcome!
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too