SwiftyLab / MetaCodable

Supercharge Swift's Codable implementations with macros meta-programming.
https://swiftpackageindex.com/SwiftyLab/MetaCodable/main/documentation/metacodable
MIT License
604 stars 22 forks source link

Provide default value of nil for empty strings #50

Closed murad1981 closed 9 months ago

murad1981 commented 9 months ago

I have the following model:

@Codable
struct Model {
 var myVar: String?
}

is there a way to convert myVar's value to nil when its value is empty string when encoding/ decoding the model ?

soumyamahunt commented 9 months ago

There are two ways you can go about this:

  1. Building a custom helper that handles the decoding logic.
  2. Using a custom type that has the data representation.

For your case, 2nd point seems more appropriate as it will handle validation when you want to initialize with member-wise initializer.

murad1981 commented 9 months ago

Can you give me a small example for the custom type to handle such case ? I’m sorry I can’t figure it out.

soumyamahunt commented 9 months ago

Can you give me a small example for the custom type to handle such case ? I’m sorry I can’t figure it out.

You can follow these articles on how to provide custom Codable implementations on custom types:

https://medium.com/@pxpgraphics/decoding-custom-types-with-decodable-in-swift-4-c730597da373 https://www.swiftbysundell.com/articles/customizing-codable-types-in-swift/

Also additionally you can have a look at primitive obsession and how to avoid it:

https://medium.com/@amlcurran/avoiding-primitive-obsession-in-swift-5325b65d521e

murad1981 commented 9 months ago

Can you give me a small example for the custom type to handle such case ? I’m sorry I can’t figure it out.

You can follow these articles on how to provide custom Codable implementations on custom types:

https://medium.com/@pxpgraphics/decoding-custom-types-with-decodable-in-swift-4-c730597da373 https://www.swiftbysundell.com/articles/customizing-codable-types-in-swift/

Also additionally you can have a look at primitive obsession and how to avoid it:

https://medium.com/@amlcurran/avoiding-primitive-obsession-in-swift-5325b65d521e

Thanks for the reply, but I know all of those stuff, if I want to create a custom type for a specific property to handle encoding/decoding that means i will not use MetaCodable. I've built a custom HelperCoder which has some boilerplate code that I didn't like.

soumyamahunt commented 9 months ago

Thanks for the reply, but I know all of those stuff, if I want to create a custom type for a specific property to handle encoding/decoding that means i will not use MetaCodable. I've built a custom HelperCoder which has some boilerplate code that I didn't like.

While you can go ahead with using MetaCodable to generate HelperCoder, what you are looking for in this particular case is not of scope for MetaCodable.

If you go by the definition of type:

a type is a description of a set of values and a set of allowed operations on those values.

From your requirements, it seems you are trying to impose restrictions on existing type (which is a classic example of primitive obsession) rather than creating new type with said restrictions.

The purpose of HelperCoder is to have transformations of representation of various forms of data (i.e. converting date to string or vice versa).

MetaCodable is designed to generate Codable implementation for custom types rather than impose restriction on operation performed with said types.

I am closing this issue since you have gone ahead with your solution and I see nothing in scope of MetaCodable.