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

Issue with http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd #26

Closed stepanselyuk closed 7 years ago

stepanselyuk commented 7 years ago

Hello @metaleap ! Firstly, thank you for great package! I have issue with http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd

I generated .go file using

[vagrant@localhost goprojects]$ bin/xsd-makepkg -uri="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd" -basepath="github.com/stepanselyuk/doctrine-mappings-xsd-go"
2017/02/17 16:45:51 LOAD:   http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd
2017/02/17 16:45:53 MKPKG:  /home/vagrant/goprojects/src/github.com/stepanselyuk/doctrine-mappings-xsd-go/doctrine-project.org/schemas/orm/doctrine-mapping.xsd_go/doctrine-mapping.xsd.go
2017/02/17 16:45:53 [RUN]   go install github.com/stepanselyuk/doctrine-mappings-xsd-go/doctrine-project.org/schemas/orm/doctrine-mapping.xsd_go
2017/02/17 16:45:54 TOTAL BUILD TIME: 420.515732ms

Ok then, in the project I use this xml document:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="AppBundle\Entity\Products" table="products">
    <indexes>
      <index name="idx_products_deleted_at" columns="deleted_at"/>
    </indexes>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="createdAt" type="datetime" column="created_at" nullable="true"/>
    <field name="updatedAt" type="datetime" column="updated_at" nullable="true"/>
    <field name="deletedAt" type="datetime" column="deleted_at" nullable="true"/>
    <field name="code" type="string" column="code" length="255" nullable="true">
      <options>
        <option name="fixed"/>
      </options>
    </field>
    <field name="price" type="integer" column="price" nullable="true">
      <options>
        <option name="unsigned">1</option>
      </options>
    </field>
    <field name="vendor" type="string" column="vendor" length="255" nullable="true">
      <options>
        <option name="fixed"/>
      </options>
    </field>
  </entity>
</doctrine-mapping>

Tried to use in this way:

package db_mappings

import (
    "encoding/xml"
    doctrine "github.com/stepanselyuk/doctrine-mappings-xsd-go/doctrine-project.org/schemas/orm/doctrine-mapping.xsd_go"
    "log"
    "path/filepath"
    "github.com/metaleap/go-util-fs"
    "fmt"
)

type ProductMapping struct {
    XMLName xml.Name `xml:"http://doctrine-project.org/schemas/orm/doctrine-mapping doctrine-mapping"`
    doctrine.TxsdDoctrineMapping
}

func GetProductMapping() {

    filename, _ := filepath.Abs("db_mappings/xml/Products.orm.xml")
    log.Printf("Loading %s", filename)

    doc, dataOrig := &ProductMapping{}, ufs.ReadBinaryFile(filename, true)

    err := xml.Unmarshal(dataOrig, doc)

    if err != nil {
        panic(err)
    }

    v := doc.Entities[0].Table.String()

    fmt.Printf("%v %p\n", &v, v)
}

and the result:

[vagrant@localhost gorm-play]$ go run main.go xml_load
2017/02/17 17:30:50 Loading /home/vagrant/goprojects/src/github.com/stepanselyuk/gorm-play/db_mappings/xml/Products.orm.xml
0xc42022ee50 %!p(string=)

I expect to print "products" table name. I don't know where exactly I'm wrong.

stepanselyuk commented 7 years ago

It seems I can get access to xml nodes values, but don't know how to get access to attributes, cause if I call:

v := doc.Entities[0].Fields[4].Options.Options[0].XsdGoPkgCDATA
fmt.Printf("Product: %+v %p\n", v, v)

it prints: Product: 1 %!p(string=1) which conform the xml document content.

The same thing I tried on github.com/metaleap/go-xsd-pkg/kbcafe.com/rss/atom.xsd.xml_go - can access to nodes' values, but cannot to get access to attributes' values (I added values to feed/id node: base="foobar" lang="en").

stepanselyuk commented 7 years ago

It seems the same issue as described in #18. I fixed by removing namespaces from generated .xsd.go file.