drmohundro / SWXMLHash

Simple XML parsing in Swift
MIT License
1.41k stars 205 forks source link

XMLIndexerDeserializable compiler error #258

Closed toseefkhilji closed 2 years ago

toseefkhilji commented 2 years ago

Getting compile time error if my xml is more than 8 fields

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

struct User: XMLIndexerDeserializable {
var userID: Int
    var username: String
    var email: String
    var name: String
    var role: String
    var roleID: Int
    var zipCode: String
    var isPaidUser: Bool
    var profileImage: String?

    static func deserialize(_ node: XMLIndexer) throws -> User {
        return try User(userID: node["UserId"].value(),
                        username: node["UserName"].value(),
                        email: node["Email"].value(),
                        name: node["UserName"].value(),
                        role: node["Role"].value(),
                        roleID: node["RoleID"].value(),
                        zipCode: node["ZipCode"].value(),
                        isPaidUser: node["IsPaidUser"].value(),
                        profileImage: node["ProfileImage"].value())
    }
}

To Reproduce Steps to reproduce the behavior:

  1. XML with more params it gives Compiler time error
    <UserLoginComplexType>
    <UserId>1106</UserId>
                        <Role>Teacher </Role>
                        <UserName>stk22</UserName>
                        <Email>stk22@app.com</Email>
                        <ZipCode>0</ZipCode>
                        <IsRead>false</IsRead>
                        <IsWrite>false</IsWrite>
                        <IsDelete>false</IsDelete>
                        <NoOfUser>1</NoOfUser>
                        <isPediaPurchased>false</isPediaPurchased>
                        <isPaymentDone>false</isPaymentDone>
                        <RoleID>0</RoleID>
                        <IsPaidUser>false</IsPaidUser>
                        <ProfileImage />
    </UserLoginComplexType>

    Expected behavior No error

Screenshots Compile error

Environment:

Additional context Add any other context about the problem here.

drmohundro commented 2 years ago

I'm unable to reproduce this on my machine... I'm on Xcode 13.3, Swift 5.6, and 12.3.1.

Can you try moving the individual .value() lines onto their own line, though, with a refactoring to variable?

Like this:

extension User: XMLIndexerDeserializable {
    static func deserialize(_ node: XMLIndexer) throws -> User {
        // these are refactored out... I just did these three
        let userId: Int = try node["UserId"].value()
        let username: String = try node["UserName"].value()
        let email: String = try node["Email"].value()

        return try User(userID: userId,
                        username: username,
                        email: email,
                        name: node["UserName"].value(),
                        role: node["Role"].value(),
                        roleID: node["RoleID"].value(),
                        zipCode: node["ZipCode"].value(),
                        isPaidUser: node["IsPaidUser"].value(),
                        profileImage: node["ProfileImage"].value())
    }
}
toseefkhilji commented 2 years ago

Thanks.!!

I have work with custom init method

  init?(_ node: XMLIndexer?) {
        guard let node = node else { return nil }
        do {
            userID = try node["UserId"].value()
            username = try node["UserName"].value()
            email = try node["Email"].value()
            name = try node["UserName"].value()
            role = try node["Role"].value()
            roleID = try node["RoleID"].value()
            zipCode = try node["ZipCode"].value()
            isPaidUser = try node["IsPaidUser"].value()
            profileImage = try node["ProfileImage"].value()
        } catch {
            print("user init error:\(error))")
            return nil
        }
    }