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(map): Codegen for XMLSchema Target #64

Merged
merged 22 commits into from
Nov 16, 2023

Conversation

jonathan-casey
Copy link
Member

@jonathan-casey jonathan-casey commented Oct 13, 2023

Description

Add XMLSchema code generation for Map Type. Validated against xmlvalidation.com & w3schools.com.

Sample <String, String>

<xs:complexType name="Map1">
   <xs:sequence>
      <xs:complexType>
         <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="xs:string"/>
         </xs:sequence>
      </xs:complexType>
   </xs:sequence>
</xs:complexType>

Sample <String, DateTime>

<xs:complexType name="Map2">
   <xs:sequence>
      <xs:complexType>
         <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="xs:dateTime"/>
         </xs:sequence>
      </xs:complexType>
   </xs:sequence>
</xs:complexType>

Sample <String, SSN>

<xs:complexType name="Map3">
   <xs:sequence>
      <xs:complexType>
         <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="xs:string"/>
         </xs:sequence>
      </xs:complexType>
   </xs:sequence>
</xs:complexType>

Sample <String, Concept>

<xs:complexType name="Map4">
   <xs:sequence>
      <xs:complexType>
         <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="concerto:Concept"/>
         </xs:sequence>
      </xs:complexType>
   </xs:sequence>
</xs:complexType>

Sample <SSN, String>

<xs:complexType name="Map5">
   <xs:sequence>
      <xs:complexType>
         <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="xs:string"/>
         </xs:sequence>
      </xs:complexType>
   </xs:sequence>
</xs:complexType>

Sample <SSN, Employee>

<xs:complexType name="Map6">
   <xs:sequence>
      <xs:complexType>
         <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="org.acme.hr:Employee"/>
         </xs:sequence>
      </xs:complexType>
   </xs:sequence>
</xs:complexType>

@jonathan-casey jonathan-casey requested a review from a team October 13, 2023 14:28
@jonathan-casey jonathan-casey force-pushed the jonathan/codegen_map_xmlschema branch from f1a035d to cd7066c Compare October 16, 2023 12:48
Copy link
Member

@mttrbrts mttrbrts left a comment

Choose a reason for hiding this comment

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

I had trouble validating the output schema. I'm also unsure about what the target XML document would look like for instances.

For example, the following fails to validate in VS Code.

<?xml version="1.0"?>
<xs:schema xmlns:org.acme.hr="org.acme.hr" targetNamespace="org.acme.hr" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:concerto="concerto"
  >
  <xs:element name="root">
    <xs:complexType name="Map1">
      <xs:sequence>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Problems:

  • s4s-att-not-allowed: Attribute 'name' cannot appear in element 'complexType'.
  • s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). A problem was found starting at: complexType.

If I change the definition to the following, it does validate, but produces unexpected instances.

<?xml version="1.0"?>
<xs:schema xmlns:org.acme.hr="org.acme.hr" targetNamespace="org.acme.hr" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:concerto="concerto"
  >
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="key" type="xs:string"/>
        <xs:element name="value" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<root xmlns="org.acme.hr" xmlns:concerto="concerto" xsi:schemaLocation="org.acme.hr schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <key>string</key>
  <value>1981-11-16T18:39:36.34</value>
</root>

Is the design here better suited to our use case?

@mttrbrts mttrbrts added this to the v3.x milestone Nov 8, 2023
@coveralls
Copy link

coveralls commented Nov 9, 2023

Coverage Status

coverage: 98.91% (-0.05%) from 98.955%
when pulling c78ccde on jonathan/codegen_map_xmlschema
into 79653dd on main.

@jonathan-casey
Copy link
Member Author

I had trouble validating the output schema. I'm also unsure about what the target XML document would look like for instances.

For example, the following fails to validate in VS Code.

<?xml version="1.0"?>
<xs:schema xmlns:org.acme.hr="org.acme.hr" targetNamespace="org.acme.hr" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:concerto="concerto"
  >
  <xs:element name="root">
    <xs:complexType name="Map1">
      <xs:sequence>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="key" type="xs:string"/>
            <xs:element name="value" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Problems:

  • s4s-att-not-allowed: Attribute 'name' cannot appear in element 'complexType'.
  • s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). A problem was found starting at: complexType.

If I change the definition to the following, it does validate, but produces unexpected instances.

<?xml version="1.0"?>
<xs:schema xmlns:org.acme.hr="org.acme.hr" targetNamespace="org.acme.hr" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:concerto="concerto"
  >
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="key" type="xs:string"/>
        <xs:element name="value" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<root xmlns="org.acme.hr" xmlns:concerto="concerto" xsi:schemaLocation="org.acme.hr schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <key>string</key>
  <value>1981-11-16T18:39:36.34</value>
</root>

Is the design here better suited to our use case?

@mttrbrts thanks for catching this. I've switched validators and was able to reproduce the same issues which are now patched.

RedHats VSCode plugin used for inline validation and liquid-technologies online instance validator both suggest the current generation of XSD is valid. Here is a sample:

XSD:

<?xml version="1.0"?>
<xs:schema xmlns:org.acme.hr="org.acme.hr" targetNamespace="org.acme.hr"
    elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:concerto="concerto"
>
       <xs:complexType name="CompanyProperties">
        <xs:sequence>
           <xs:element name="key" type="xs:string"/>
           <xs:element name="value" type="xs:string"/>
        </xs:sequence>
     </xs:complexType>
     <xs:element name="CompanyProperties" type="org.acme.hr:CompanyProperties"/>
</xs:schema>

Instance:

<?xml version="1.0" encoding="UTF-8"?>
<org.acme.hr:CompanyProperties xmlns:org.acme.hr="org.acme.hr">
  <org.acme.hr:key>Some Thing</org.acme.hr:key>
  <org.acme.hr:value>Some Other Thing</org.acme.hr:value>
</org.acme.hr:CompanyProperties>

@jonathan-casey
Copy link
Member Author

A sample XSD which supports multiple entries in a Map. Each Map will have its own 'entry' type defining the entries within the instance.

<xs:element name="diary">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="entry" type="diary_entryType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:complexType name="diary_entryType">
  <xs:sequence>
    <xs:element name="key" type="xs:xs:dateTime"/>
    <xs:element name="value" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

jonathan-casey and others added 3 commits November 15, 2023 15:38
* feat(*): keys are variable typed

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): update test

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): update snapshot

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): adds scalar test coverage

Signed-off-by: Jonathan Casey <[email protected]>

* feat(*): minor refactor, update tests

Signed-off-by: Jonathan Casey <[email protected]>

* test(*): update snapshot

Signed-off-by: Jonathan Casey <[email protected]>

---------

Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
@jonathan-casey jonathan-casey merged commit e22a321 into main Nov 16, 2023
11 checks passed
@jonathan-casey jonathan-casey deleted the jonathan/codegen_map_xmlschema branch November 16, 2023 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants