metaleap / go-xsd

[stable since 2013] a lib for loading XML Schema Definition (XSD) files ➜ plus, a tool `makepkg` to code-generate from any *.xsd your Go package with all needed `struct`s to readily `xml.Unmarshal()` documents into, based on the XSD's schema definitions. NOT REALLY MAINTAINED FOR YEARS NOW: try the forks if running into issues.
http://www.reddit.com/r/golang/comments/12g6sl/is_there_a_tool_that_generates_go_source_code_for/
MIT License
216 stars 66 forks source link

ComplexType in SimpleContent will cause unmarshal fail #6

Closed lobatt closed 7 years ago

lobatt commented 10 years ago

I had some code generated by go-xsd that worked perfectly fine under go1.1, however, from go1.2, the unmarshal will fail for some of the data structure, example:

<xsd:complexType name="DeprecatedStringType">
 <xsd:simpleContent>
   <xsd:extension base="NonEmptyStringType">
     <xsd:attribute name="deprecated" type="xsd:boolean" use="required" fixed="true"/>
   </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>

from: http://www.cablelabs.com/wp-content/uploads/specdocs/MD-SP-CONTENTv3.0-I02-121210.pdf

which result in:

type TDeprecatedStringType struct {
  XsdGoPkgValue TNonEmptyStringType `xml:",chardata"`

  XsdGoPkgHasAttr_Deprecated_XsdtBoolean_True
}

//  Simply returns the value of its XsdGoPkgValue field.
func (me *TDeprecatedStringType) ToTNonEmptyStringType() TNonEmptyStringType { return me.XsdGoPkgValue }

//  Returns the value of its XsdGoPkgValue field as a xsdt.String (which TNonEmptyStringType is just aliasing).
func (me *TDeprecatedStringType) ToXsdtString() xsdt.String { return me.XsdGoPkgValue.ToXsdtString() }

//  If the WalkHandlers.TDeprecatedStringType function is not nil (ie. was set by outside code), calls it with this TDeprecatedStringType instance as the single argument. Then calls the Walk() method on 0/1 embed(s) and 0/1 field(s) belonging to this TDeprecatedStringType instance.
func (me *TDeprecatedStringType) Walk() (err error) {
  if fn := WalkHandlers.TDeprecatedStringType; me != nil {
    if fn != nil {
      if err = fn(me, true); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
        return
      }
    }
    if fn != nil {
      if err = fn(me, false); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
        return
      }
    }
  }
  return
}

I have compared src/pkg/encoding/xml/read.go in go1.1 and go1.2, the error message is new.

Any thought?

lobatt commented 10 years ago

Ok, the problem is that when a complexType is used in restriction for simpleContent, makePkg will always assign the chardata to the underneath complexType, hence Unmarshal will fail later.

According to http://www.w3schools.com/schema/el_simpleContent.asp Text-only complexType can be used in simpleContent.