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

adding support for XML parsing and shale type mapping #48

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ inherit_from:

AllCops:
TargetRubyVersion: 2.5
NewCops: enable
7 changes: 7 additions & 0 deletions lib/lutaml/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "lutaml/express"
require "lutaml/uml"
require "lutaml/xmi"
require "lutaml/xml"
require "lutaml/uml/lutaml_path/document_wrapper"
require "lutaml/express/lutaml_path/document_wrapper"
require "expressir/express/cache"
Expand Down Expand Up @@ -43,6 +44,8 @@ def parse_into_document
Expressir::Express::Cache.from_file(file_list.first.path)
when 'xmi'
file_list.map { |file| Lutaml::XMI::Parsers::XML.parse(file) }
when "xml"
file_list.map { |file| Lutaml::Xml::Parsers::Xml.parse(file) }
when "lutaml"
file_list.map { |file| Lutaml::Uml::Parsers::Dsl.parse(file) }
when "yml"
Expand All @@ -59,6 +62,10 @@ def document_wrapper(document)
return Lutaml::Express::LutamlPath::DocumentWrapper.new(document)
end

if parse_type == "xml"
return Lutaml::Xml::LutamlPath::DocumentWrapper.new(document)
end

Lutaml::Uml::LutamlPath::DocumentWrapper.new(document)
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/lutaml/xml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "lutaml/xml/parsers/xml"

require "lutaml/xml/mapper"
require "lutaml/xml/lutaml_path/document_wrapper"

require "shale"
require "shale/adapter/nokogiri"

Shale.xml_adapter = Shale::Adapter::Nokogiri
45 changes: 45 additions & 0 deletions lib/lutaml/xml/lutaml_path/document_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "lutaml/lutaml_path/document_wrapper"

module Lutaml
module Xml
module LutamlPath
class DocumentWrapper < ::Lutaml::LutamlPath::DocumentWrapper
protected

def serialize_document(document)
serialize_to_hash(document)
end

def serialize_to_hash(object)
Copy link

Choose a reason for hiding this comment

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

Assignment Branch Condition size for serialize_to_hash is too high. [<11, 45, 16> 49.01/15]
Cyclomatic complexity for serialize_to_hash is too high. [13/6]
Method has too many lines. [25/10]
Perceived complexity for serialize_to_hash is too high. [16/7]

Copy link

Choose a reason for hiding this comment

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

Assignment Branch Condition size for serialize_to_hash is too high. [<11, 45, 15> 48.69/15]
Cyclomatic complexity for serialize_to_hash is too high. [12/6]
Method has too many lines. [25/10]
Perceived complexity for serialize_to_hash is too high. [15/7]

hash = {}
attribute_name = nil

object.all_content.each do |mapping, content|
if mapping == "content"
attribute_name = object.class.xml_mapping.content.attribute.to_s
hash[attribute_name] ||= []
hash[attribute_name] << content.strip unless content.strip&.empty?
Copy link

Choose a reason for hiding this comment

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

Avoid calling empty? with the safe navigation operator in conditionals.

elsif content.is_a?(String)
if object.class.attributes[mapping.attribute].collection?
hash[mapping.name] ||= []
hash[mapping.name] << content.strip
else
hash[mapping.name] = content.strip
end
else
if object.class.attributes[mapping.attribute].collection?
Copy link

Choose a reason for hiding this comment

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

Convert if nested inside else to elsif.

hash[mapping.name] ||= []
hash[mapping.name] << serialize_to_hash(content)
else
hash[mapping.name] = serialize_to_hash(content)
end
end
end

hash[attribute_name] = hash[attribute_name].compact if attribute_name
hash
end
end
end
end
end