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

feat: add map_all for xml mapping #129

Merged
merged 5 commits into from
Nov 1, 2024
Merged

Conversation

HassanAkbar
Copy link
Member

Added map_all for mapping all content inside an XML tag to an attribute.

fixes #57
can be used for -> #106

@HassanAkbar HassanAkbar requested a review from ronaldtse October 29, 2024 15:10
Copy link
Contributor

@ronaldtse ronaldtse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HassanAkbar looks good! Could you please help document this in the README with an example? Thanks.

@HassanAkbar HassanAkbar requested a review from ronaldtse October 30, 2024 07:19
@ronaldtse
Copy link
Contributor

@HassanAkbar what happens with map_all when the format is JSON? Does this mean that a class that uses map_all cannot do JSON etc?

@HassanAkbar
Copy link
Member Author

HassanAkbar commented Oct 31, 2024

@ronaldtse Yes this PR only adds map_all for XML mappings, For JSON we do not need the map_all method because our internal representation is hash and we can map an attribute to a string, something like below and it will work

person = { 
  name: "test", 
  age: "20", 
  description: { title: "foobar", awards: "many" }
}.to_json

class Person < Lutaml::Model::Serializable
  attribute :name, :string
  attribute :age, :integer
  attribute :description, :string
  
  json do
    map "name", to: :name
    map "age", to: :age
    map "description", to: :description
  end
end

Person.from_json(person)
#<Person:0x00000001044dd9b0                                     
 @age=20,                                                       
 @description="{\"title\"=>\"foobar\", \"awards\"=>\"many\"}",  
 @name="test",                                                  
 @using_default={:name=>false, :age=>false, :description=>false},
 @validate_on_set=false> 

I will open a separate PR for YAML and toml.
I added this only in a separate PR because this was marked urgent and has been in progress for a long time.

@ronaldtse
Copy link
Contributor

I think what I don't understand is, how does the concept of map_all work with JSON, YAML and TOML?

JSON is like: { "attr" : "value" }. Does that mean map_all will give the raw value of { "attr" : "value" }?

YAML is like: attr: value. Does that mean map_all will give the raw value?

Does map_all on this element produce:

<element>
  <one>text</one>
</element>
  1. The entire element?
<element attr=1>
  <one>text</one>
</element>
  1. Only the inner content of the element?
  <one>text</one>

@ronaldtse ronaldtse merged commit 4fd7b78 into main Nov 1, 2024
13 of 14 checks passed
@ronaldtse ronaldtse deleted the add_map_all_for_xml_mapping branch November 1, 2024 05:33
@HassanAkbar
Copy link
Member Author

@ronaldtse the map_all will work by assigning everything inside the tag or the key it is defined in. for example

  • For XML

    map_all will assign all the content inside that tag but will not assign the attributes in the current tag. i.e

    class Element < Lutaml::Model::Serializable
      attribute :all_content, :string
      
      xml do
        map_all to: :all_content
      end
    end
    <element attr1="1">
      <one attr2="2" attr3="3">text</one>
    </element>
    Element.from_xml(xml).all_content
    > "<one attr2=\"2\" attr3=\"3\">text</one>"
  • For YAML, JSON, and TOML

    map_all will assign all the content against the key to the model attribute. i.e

    class Element < Lutaml::Model::Serializable
      attribute :all_content, :string
      
      yaml do
        map_all to: :all_content
      end
      
      json do
        map_all to: :all_content
      end
    
      toml do
        map_all to: :all_content
      end
    end

    YAML Example

    element:
    - another_element
    - element_with_childs
      name: "John"
    Element.from_yaml(yaml).all_content
    > "- another_element\n- element_with_childs\n  name: \"John\""

    JSON Example

    { 
      "element": [
        "another_element",
        "element_with_childs": { "name": "John" }
      ]
    }
    Element.from_json(json).all_content
    > "[\"another_element\", \"element_with_childs\": {\"name\": \"John\"}]"

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

Successfully merging this pull request may close these issues.

(URGENT) Mixed content at root not working
2 participants