xuri / xgen

XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
BSD 3-Clause "New" or "Revised" License
313 stars 74 forks source link

Go Generator - Main type generates incorrect xml: tag #46

Open jimidle opened 2 years ago

jimidle commented 2 years ago

Description

The xml tag for the top type is generated incorrectly, which makes i impossible to unmarshall in to the top structure.

Steps to reproduce the issue:

  1. Start with the svg xsd definition and generate a struct for it
  2. Try to unmarshal an svg string in to an instance
    <?xml version="1.0" encoding="UTF-8"?>
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="500pt" height="300pt" viewBox="0 0 500 300" version="1.1">
    <defs>
        <clipPath id="clip1">
            <path d="M 51 203 L 135 203 L 135 269.839844 L 51 269.839844 Z M 51 203 "/>
        </clipPath>
        <clipPath id="clip2">
            <path d="M 167 190 L 251 190 L 251 269.839844 L 167 269.839844 Z M 167 190 "/>
        </clipPath>
        <clipPath id="clip3">
            <path d="M 282 177 L 366 177 L 366 269.839844 L 282 269.839844 Z M 282 177 "/>
        </clipPath>
        <clipPath id="clip4">
            <path d="M 397 32 L 481 32 L 481 269.839844 L 397 269.839844 Z M 397 32 "/>
        </clipPath>
    </defs>
    <g id="surface1521">
    ...
  3. You will receive an error: expected element type <svgType> but have <svg>

Describe the results you received:

Unable to parse an svg XML file because the generated tag for xml appends Type to the XML tag:

// SvgType ...
type SvgType struct {
    XMLName                       xml.Name `xml:"svgType"`

It does this for all XMLName declarations. Then will declare:

type Svg *SvgType

Which also will then sometimes generate duplicates because it does not distinguish between things like simpletype and complextype. For instance with SVG you will see:

type Title string
type Title *TitleType

Describe the results you expected:

An unmarshalled svg struct, with the generated tag being xml:"svg"

Output of go version:

go version go1.18 darwin/amd64

xgen version or commit ID:

xgen version: 0.1.0

Environment details (OS, physical, etc.): OSX version 12.3

jimidle commented 2 years ago

xsd files for generating and .swg file for unmarshalling attached. They will reproduce the issue described above.