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

xsd-makepkg generated code can not unmarshal element based on xsdt types #12

Closed lobatt closed 7 years ago

lobatt commented 9 years ago

Example: http://www.atsc.org/XMLSchemas/pmcp/2007/3.1/pmcp31.xsd

<xsd:complexType name="TextType">
    <xsd:annotation>
        <xsd:documentation>One string of a Multiple String Structure of PSIP (A/65B 6.10)</xsd:documentation>
    </xsd:annotation>
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
            <xsd:attribute name="lang" type="languageType" use="required"/>
            <xsd:attribute name="action" type="actionType" use="optional"/>
            <xsd:attribute name="error" type="errorType" use="optional"/>
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

The schema above will generate:

type TBxfText struct {
    XsdtString 

   XsdGoPkgHasAtts_ActionErrorGroup

   XsdGoPkgHasAttr_Lang_PmcpTlanguageType_

   XsdGoPkgHasAttr_Size_XsdtPositiveInteger_

   XsdGoPkgHasAttr_Type_XsdtString_
}

There are 2 issues here:

  1. XsdtString should actually be xsdt.String
  2. since https://github.com/metaleap/go-xsd/blob/master/types/xsdtypes.go have no xml tags specified, no value will be able to be unmashal into this embedded field.

Possible solution:

diff --git a/elemmakepkg.go b/elemmakepkg.go
index e81d961..2ac785b 100644
--- a/elemmakepkg.go
+++ b/elemmakepkg.go
@@ -281,7 +281,11 @@ func (me *ComplexType) makePkg(bag *PkgBag) {
                }
        }
        if ctBaseType = bag.resolveQnameRef(ctBaseType, "T", nil); len(ctBaseType) > 0 {
-               td.addEmbed(nil, bag.safeName(ctBaseType))
+               if strings.HasPrefix(ctBaseType, "xsdt.") {
+                       td.addEmbed(nil, idPrefix+"HasCdata")
+               } else {
+                       td.addEmbed(nil, bag.safeName(ctBaseType))
+               }
        } else if ctValueType = bag.resolveQnameRef(ctValueType, "T", nil); len(ctValueType) > 0 {
                bag.simpleContentValueTypes[typeSafeName] = ctValueType
                td.addField(nil, idPrefix+"Value", ctValueType, ",chardata")

Thoughts?

purohit commented 9 years ago

I had the same problem with (1. XsdtString should actually be xsdt.String). I changed them all by hand.

grmartin commented 9 years ago

I ran in to this this morning as well. Thanks for the assist.

thiloplanz commented 8 years ago

Is there a reason this patch is not being merged? Are there situations where it does not work?

I just ran into the same problem today (generated code would not compile because of XsdtString) and the fix provided by @lobatt solved that.

lobatt commented 8 years ago

I guess it was mostly because of this repo was abandoned by the author. lol

taiyangc commented 7 years ago

@lobatt Do you think it's worth the effort to put up a PR for the patch? It would be super helpful than each of us doing our own forks. Owner seems to be around lately :) Thanks!

metaleap commented 7 years ago

Yeah I'm around for pull-requests!