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

Support for functionally equivalent xsd:union (a value type sharing root name adhering to one of multiple data definitions) #190

Open
suleman-uzair opened this issue Nov 28, 2024 · 4 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@suleman-uzair
Copy link
Member

Schema supports the union element to provide multiple data type support for a single attribute.

How can we support this in LutaML::Model?

@suleman-uzair suleman-uzair added enhancement New feature or request question Further information is requested labels Nov 28, 2024
@ronaldtse
Copy link
Contributor

We will need a new “value type” or an “attribute” option that supports this.

Today we can implement this by having a new class that contains all union value types as attributes but enforcing it to only have one type using custom methods.

Ideally perhaps we can make an attribute option called “union” and provide multiple value types in it as an array.

@ronaldtse
Copy link
Contributor

Actually, union is an XML specific notion, so we need to specify that in map_element. One possible solution, is to first define multiple attributes, and have map_element map the element to those multiple attributes.

@suleman-uzair
Copy link
Member Author

One possible solution, is to first define multiple attributes, and have map_element map the element to those multiple attributes.

@ronaldtse Something like this?

xml do 
  map_(attribute | content) :<element-name>, to: :<attribute>, union: [DataType1, DataType2, ...]
end

or 

xml do 
  map_(attribute | content) :<element-name>, to: [attribute-one, attribute-two, ...]
end

Yeah, that might work. I will check on this further.

@ronaldtse ronaldtse changed the title Support for xsd:union functionality. Support for functionally equivalent xsd:union (a value type sharing root name adhering to one of multiple data definitions) Jan 6, 2025
@ronaldtse
Copy link
Contributor

ronaldtse commented Jan 6, 2025

I think union is more about defining the attribute value definition of the data model, not really about the serialization format.

The union can apply across serialization formats, whether XML or JSON or YAML.

What about this?

class TemperatureString < Lutaml::Model::Value::String
end

class TemperatureWithUnit < Lutaml::Model::Serialization
  attribute :number, :decimal
  attribute :unit, :string
  xml do
    no_root # no root element
    map_element 'number', to: :number
    map_element 'unit', to: :unit
  end
end

class Ceramic < Lutaml::Model::Serialization
  attribute :firing_temperature, [ TemperatureString, TemperatureWithUnit ]
  # or just
  # attribute :firing_temperature, [ :string, TemperatureWithUnit ]

  xml do
    root 'ceramic'
    map_element 'FiringTemperature', to: :firing_temperature
  end
end

Accepts these:

Case 1 (String):

<ceramic>
  <FiringTemperature>Very Hot</FiringTemperature>
</ceramic>

Case 2 (String):

<ceramic>
  <FiringTemperature><number>1300</number><unit>C</unit></FiringTemperature>
</ceramic>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants