Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Support convert JSON fields to the types of my Swift objects automatically, without me having to manually add @CodedBy. #93

Open
1 task done
zy19890102 opened this issue Jul 18, 2024 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@zy19890102
Copy link

zy19890102 commented Jul 18, 2024

I want to replace the previous tool with it. However, I am facing an issue now. My Swift object data comes from many different APIs, and the same field from different APIs may have different data types. The previous tool automatically converted them to the desired data types for me. I don't know which fields need to be marked with @codedby because there are too many such fields, and it's difficult to identify them manually. Can you provide a feature to automatically convert the data types to the desired types?

Moreover, many APIs have not been maintained for a long time, making it difficult to determine the specific data types.

@zy19890102 zy19890102 added the enhancement New feature or request label Jul 18, 2024
@soumyamahunt soumyamahunt added the good first issue Good for newcomers label Sep 30, 2024
@soumyamahunt
Copy link
Contributor

soumyamahunt commented Sep 30, 2024

Same as discussed #51 while I am not in favour of the approach suggested in #51.

I think such feature could be implemented in a more generic way, i.e. a new parameter commonStrategies could be introduced in @Codable macro which accepts such strategies which can be applied to all the properties. The API definitions

in MetaCodable:

...
public macro Codable(commonStrategies: [CodableCommonStrategy] = []) = ...

public struct CodableCommonStrategy {
    package init() {} // package access level to only allow strategies handled by MetaCodable
}

in HelperCoders:

extension CodableCommonStrategy {
    public static func codedBy(_ helperCoderStrategy: HelperCoderStrategy) -> Self {
        return .init()
    }

    // additional strategies can be added in future

    public enum HelperCoderStrategy {
        case valueCoder(_ additionalTypes: [any ValueCodingStrategy] = []) // additionalTypes can be used to pass custom ValueCodingStrategy implementations, by default only library provided ValueCodingStrategy implementation will be used
        // additional helpercoder cases can be added in future
    }
}

The current macro usage:

@Codable
fileprivate struct Model {
    @CodedBy(ValueCoder<Bool>())
    let bool: Bool
    @CodedBy(ValueCoder<Int>())
    let int: Int
    @CodedBy(ValueCoder<Double>())
    let double: Double
    @CodedBy(ValueCoder<String>())
    let string: String
}

converts to this:

@Codable(commonStrategies: [.codedBy(.valueCoder())])
fileprivate struct Model {
    let bool: Bool
    let int: Int
    let double: Double
    let string: String
}

@wzio @zy19890102 let me know your thoughts on this approach.

@wzio
Copy link

wzio commented Jan 3, 2025

Wow, I’m very happy with the new progress, and I’m glad to see such changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants