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

DynamicNodeEncoding not working on child attribute #218

Open
1n9i9c7om opened this issue Jun 21, 2021 · 0 comments
Open

DynamicNodeEncoding not working on child attribute #218

1n9i9c7om opened this issue Jun 21, 2021 · 0 comments

Comments

@1n9i9c7om
Copy link

1n9i9c7om commented Jun 21, 2021

Hi,

first of all, thanks for this project. I'm just starting with Swift and so far this library hasn't been hard to understand.

However, I'm having a problem when it comes to encoding an attribute of a child element as an attribute instead of an element.

I can't provide the exact code, but the situation is similar to this:

class A : Codable, DynamicNodeEncoding, DynamicNodeDecoding {
    var attr1: String
    var attr2: Double
    var version: Double

    // CodingKeys, required init(from decoder: Decoder),  override func encode, nodeDecoding

    static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
        print("A encoding: \(key.stringValue)")
        switch key {
        case Child.CodingKeys.version: return .attribute
        default: return .element
        }
    }
}
class B : A {
    var child: Child
    // CodingKeys, required init(from decoder: Decoder),  override func encode
}
class Child : Codable, DynamicNodeEncoding, DynamicNodeDecoding {
    var version: Double
    var text: String

    // CodingKeys, etc.

    static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
        print("child encoding: \(key.stringValue)")
        switch key {
        case Child.CodingKeys.version: return .attribute
        default: return .element
        }
    }
}

When I encode an instance of class B, I can see that A.nodeEncoding is being called. However, Child.nodeEncoding is never called and version is being added as an element instead of an attribute:

<message version="1.0">
    <attr1>Hello World!</attr1>
    <attr2>3.141</attr2>
    <child>
        <version>123.4</version>
        <text>I'm not a cat.</text>
    </child>
</message>

While I'd expect the following output:

<message version="1.0">
    <attr1>Hello World!</attr1>
    <attr2>3.141</attr2>
    <child version="123.4">
        <text>I'm not a cat.</text>
    </child>
</message>

Am I doing something wrong?

@1n9i9c7om 1n9i9c7om changed the title DynamicNodeEncoder not working on child attribute DynamicNodeEncoding not working on child attribute Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant