Giter VIP home page Giter VIP logo

metaleap / go-xsd Goto Github PK

View Code? Open in Web Editor NEW
215.0 10.0 66.0 24.23 MB

[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.

Home Page: http://www.reddit.com/r/golang/comments/12g6sl/is_there_a_tool_that_generates_go_source_code_for/

License: MIT License

Go 100.00%
go golang xsd xml-schema

go-xsd's Introduction

go-xsd

A Go package for loading ( xml.Unmarshal()-ing ) an XML Schema Definition (XSD) document into an xsd.Schema structure.

With this, you could probably write an XML validator, or otherwise utilize or further process the loaded XSD --- but the main use-case here was:

go-xsd/xsd-makepkg

A command-line tool to generate Go "XML wrapper" package sources for specified XSD schema URIs.

If no arguments are specified, this tool proceeds to (re)generate all Go packages for various common XML formats in your local $GOPATH-relative directory corresponding to the http://github.com/metaleap/go-xsd-pkg repository. For more details on command-line arguments for xsd-makepkg: scroll down to the bottom of this readme.

Each generated wrapper package contains the type structures required to easily xml.Unmarshal() an XML document based on that XSD.

XSD simple-types are represented by the corresponding native Go scalar data type, augmented by utility methods where applicable:

  • enumerated simple-types (eg. "value can be 'sunny', 'cloudy', 'rainy'") get handy corresponding IsXyz() bool methods (eg. "IsSunny()", "IsCloudy()", "IsRainy()")

  • simple-types that define a whitespace-separated list of scalar values get a corresponding, properly typed Values() method

  • for attributes or elements that define a fixed or default value, their corresponding generated Go simple-type will have a properly typed ElemnameDefault() / ElemnameFixed() / AttnameDefault() / AttnameFixed() method (eg. if the langpref attribute is defined to default to "Go", then its simple-type will have a LangprefDefault() method returning "Go")

XSD complex-types, attribute-groups, element-groups, elements etc. are ultimately represented by corresponding generated Go struct types.

XSD includes are all loaded and processed together into a single output .go source file.

XSD imports are rewritten as Go imports but not otherwise auto-magically processed. If you see the generated .go package importing another "some-xsd-xml-whatever-name-_go" package that will cause a "package not found" compiler error, then to make that import work, you'll first need to also auto-generate that package with xsd-makepkg yourself as well.

XSD documentation annotation is rewritten as Go // code comments. Yeah, that's rather neat.

Regarding the auto-generated code:

  • it's by necessity not idiomatic and most likely not as terse/slim as manually-written structs would be. For very simplistic XML formats, writing your own 3 or 4 custom structs might be a tiny bit more efficient. For highly intricate, unwieldy XML formats, the auto-generated packages beat hand-writing 100s of custom structs, however. Auto-generated code will never win a code-beauty contest, you're expected to simply import the compiled package rather than having to work inside its generated source files.

  • most (XSD-declared) types are prefixed with T -- thanks to the SVG schema which taught me that in XSD, "scriptType" and "ScriptType" are two valid and uniquely different type names. To have all types exported from the generated Go package, then, some kind of prefix is indeed needed.

  • most XSDs are chock-full of anonymous types, as well as implicit ones (unions, restrictions, extensions...) Go does support "anonymous types" per se, but I decided against using them. Every type is declared and exported, no anonymous magic. This makes most auto-generated packages "look" even more confusing than their XSD counterparts at first glance. Indeed they may appear quite bloated, and when coding with the imported generated package you'll probably be better off working with the particular XML format's specification document rather than the godoc for the generated package... this is not a perfect situation but at least for now I can work with this for the few XML formats I occasionally need to "parse, convert and forget" -- ultimately, most XML formats at my end are mere interchange or legacy formats, and never really the "main topic" at hand.

go-xsd/types

A tiny package automatically imported by all go-xsd auto-generated packages. Maps all XSD built-in simple-types to Go types, which affords us easy mapping of any XSD type references in the schema to Go imports: every xs:string and xs:boolean automatically becomes xsdt.String and xsdt.Boolean etc. Types are mapped to Go types depending on how encoding/xml.Unmarshal() can handle them: ie. it parses bools and numbers, but dates/durations have too many format mismatches and thus are just declared string types. Same for base64- and hex-encoded binary data: since Unmarshal() won't decode them, we leave them as strings. If you need their binary data, your code needs to import Go's base64/hex codec packages and use them as necessary.

How to use auto-generated packages:

Take a look at the "test progs" under xsd-makepkg/tests, they're basically simple usage examples. For unmarshal you need to define just one small custom struct like this --- using the rss package as a simple example, as demonstrated in xsd-makepkg/tests/xsd-test-rss/main.go:

type MyRssDoc struct {
    XMLName xml.Name `xml:"rss"`
    rss.TxsdRss
}

So your custom struct specifies two things:

  • the XML name of the root element in your XML file, as is typical when working with encoding/xml.Unmarshal().

  • the Go struct type from the auto-generated package to embed right inside your custom struct.

The second part is the only tricky part. XML Schema Definition has no real concept of "root element", partly because they're designed to support use-cases where you embed a full document defined in one XSD deep inside a full document defined in another XSD. So a Collada document may contain a full or partial MathML document somewhere inside it. Some well-designed XSDs define a single top-level element, so we could infer "this is the root element" and generate a "XyzDoc" struct (like the MyRssDoc above) for you. But many don't. Some formats may legally have one of two or more possible "root" elements, ie. Atom allegedly may have either a "feed" root element or an "entry" root element. So go-xsd does not magically infer which of the XSD's top-level elements might be the root element, you define this by writing a small struct as shown above. The naming of the root element Go type to be embedded is not consistent across different packages, because their naming is directly based on the XSD that was used to generate the package. So for example...

  • for rss we have rss.TxsdRss
  • for atom: atom.TentryType and atom.TfeedType
  • for svg: svg.TsvgType
  • for Collada: collada.TxsdCollada

Seems like Collada and RSS share a certain naming pattern, and yet Atom/SVG share another one? Mere coincidence, the naming is completely arbitrary and up to the XSD author. Ultimately, to find out the proper Go type name to embed, you'll have to dig a bit inside the generated package. That's actually pretty straightforward, here's how you do it:

A) Suppose you have an XML format where the root element (and only that one) is known to be named:

<gopher>

B) Open the generated Go package source files under $GOPATH/src/github.com/metaleap/go-xsd-pkg/yourdomain.org/xsd/gopher.xsd_go/*.go (unless you used custom paths when you ran the go-xsd/xsd-makepkg tool)

C) Search for an occurence of either:

"gopher"`

( quote, gopher, quote, backtick ), or:

 gopher"`

( whitespace, gopher, quote, backtick )

D) The found occurence is likely the tag for a field in a type named something like XsdGoPkgHasElem_Gopher or XsdGoPkgHasElems_Gopher. Ignore that type, instead focus on the type of the field itself. That's the one you're looking for, the one to embed in your tiny custom doc struct.

Command-line flags for go-xsd/xsd-makepkg tool:

  • -basepath="": Defaults to github.com/metaleap/go-xsd-pkg. A $GOPATH/src/-relative path (always a slash-style path, even on Windows) where XSD files are downloaded to / loaded from and generated Go wrapper packages are created. Any XSD imports are also rewritten as Go imports from that path (but are not otherwise auto-magically processed in any way).
  • -local=true: Local copy -- only downloads if file does not exist locally
  • -parse=false: Not necessary, unless the generated Go wrapper package fails to compile with either the error "cannot convert {value} to type {type}" or "cannot use {value} (type {type}) as type {type} in return argument" -- ultimately down to a slightly faulty XSD file, but while rare, those exist (hello, KML using 0 and 1 for xs:booleans that are clearly spec'd to be only ever either true or false...)
  • -uri="": The XML Schema Definition file URIs to generate a Go wrapper packages from, whitespace-separated. (For each, the protocol prefix can be omitted, it then defaults to http://. Only protocols understood by the net/http package are supported.)
  • -gofmt=true: Run 'gofmt' against the generated Go wrapper package?
  • -goinst=true: Run 'go-buildrun' ( http://github.com/metaleap/go-buildrun ) against the generated Go wrapper package?

go-xsd's People

Contributors

crell avatar dmitshur avatar klauern avatar lobatt avatar metaleap avatar sprin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-xsd's Issues

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

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.

How to use this tool to generate go structs from xsd files

Hello,

I am new to golang and i am not clear on how to setup this command line tool. Can you point me in right direction, i am using windows OS. I need help with following:

  1. Setup this project
  2. Use this to convert xsd files to go classes

Thanks,
Rahul

Nil pointer dereference with http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

Trying to get a package generated to parse web.xml files. This was built from commit 56ab80f.

╰─>$ xsd-makepkg -uri "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" -gofmt
2016/08/23 11:20:12 LOAD:   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x70 pc=0x47759a]

goroutine 1 [running]:
panic(0x657400, 0xc420014100)
    /home/jchase/.local/lib/go/src/runtime/panic.go:500 +0x1a1
github.com/metaleap/go-xsd.(*RestrictionSimpleEnumeration).makePkg(0xc4201e6840, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/elemmakepkg.go:621 +0x8a
github.com/metaleap/go-xsd.(*hasElemsEnumeration).makePkg(0xc42013fc40, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/hasmakepkg.go:89 +0x55
github.com/metaleap/go-xsd.(*RestrictionSimpleContent).makePkg(0xc42013fb80, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/elemmakepkg.go:605 +0x111
github.com/metaleap/go-xsd.(*hasElemRestrictionSimpleContent).makePkg(0xc42010d770, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/hasmakepkg.go:221 +0x47
github.com/metaleap/go-xsd.(*SimpleContent).makePkg(0xc42010d700, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/elemmakepkg.go:733 +0x73
github.com/metaleap/go-xsd.(*hasElemSimpleContent).makePkg(0xc42017e7d8, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/hasmakepkg.go:251 +0x47
github.com/metaleap/go-xsd.(*ComplexType).makePkg(0xc42017e6c0, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/elemmakepkg.go:179 +0x371
github.com/metaleap/go-xsd.(*hasElemsComplexType).makePkg(0xc420180400, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/hasmakepkg.go:71 +0x55
github.com/metaleap/go-xsd.(*Schema).makePkg(0xc420180280, 0xc4200fcd20)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/elemmakepkg.go:707 +0xd6
github.com/metaleap/go-xsd.(*Schema).MakeGoPkgSrcFile(0xc420180280, 0x28, 0xc42004fe01, 0xc420180280, 0x0)
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/xsd-schema.go:170 +0x377
main.main()
    /home/jchase/Dev/src/github.com/metaleap/go-xsd/xsd-makepkg/main.go:65 +0x21a

Generated tags for attributes can incorrectly have namespaces

When code is generated from a .xsd with namespaces, those namespaces are included in struct field tags for attribute elements. However, encoding/xml does not properly marshal/unmarshal such fields. Deleting the namespace manually from the struct tag corrects the issue.

Here's the command I ran:

$GOPATH/bin/xsd-makepkg -goinst=false -basepath=github.com/jesand/crowds/amt/gen -uri=http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd

The resulting code fails to unmarshal MinLength and MaxLength in the following XML:

<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
          <Question>
            <QuestionIdentifier>nextmove</QuestionIdentifier>
            <DisplayName>The Next Move</DisplayName>
            <IsRequired>true</IsRequired>
            <QuestionContent>
              <Text>
                What are the coordinates of the best move for player "X" in this game?
              </Text>
            </QuestionContent>
            <AnswerSpecification>
              <FreeTextAnswer>
                <Constraints>
                  <Length minLength="2" maxLength="2" />
                </Constraints>
                <DefaultText>C1</DefaultText>
              </FreeTextAnswer>
            </AnswerSpecification>
          </Question>
</QuestionForm>

Undefined fsnotify.Filevent in go-util

The output from the terminal if go install go install github.com/metaleap/go-xsd/xsd-makepkg@latest

go/pkg/mod/github.com/metaleap/[email protected]/fs/watcher-default.go:32:31: undefined: fsnotify.FileEvent
go/pkg/mod/github.com/metaleap/[email protected]/fs/watcher-default.go:64:44: undefined: fsnotify.FileEvent
go/pkg/mod/github.com/metaleap/[email protected]/fs/watcher-default.go:72:53: undefined: fsnotify.FileEvent
go/pkg/mod/github.com/metaleap/[email protected]/fs/watcher-default.go:79:19: me.Event undefined (type *Watcher has no field or method Event)
go/pkg/mod/github.com/metaleap/[email protected]/fs/watcher-default.go:97:19: me.Error undefined (type *Watcher has no field or method Error)
go/pkg/mod/github.com/metaleap/[email protected]/fs/watcher-default.go:119:16: me.Watch undefined (type *Watcher has no field or method Watch)

extremely long generated names

Here's an example: XsdGoPkgHasElem_Harmonics5ThallTxsdHarmonicsAndFrequencyDataSequenceHarmonicsAndFrequencySequenceHarmonicsPhaseDataharmonicsPhaseDatasequenceTxsdHarmonicsAndFrequencyDataSequenceHarmonicsAndFrequencyharmonicsAndFrequencysequenceTxsdHarmonicsAndFrequencyDataharmonicsAndFrequencyDataschema_Harmonics5Th_TminMaxIntervalValue_

Is there a way to handle this, I assume it's the namespace? (somehow)

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

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?

Minor correction on README

(hello, KML using 0 and 1 for xs:booleans that are clearly spec'd to be only ever either true or false...)

I hate to be that guy, but they are spec'd to evaluate to true/false. Their accepted values do in fact include 1 and 0.

Sources:
Schema 1.0 § 3.3.2.1
Schema 1.1 § 3.3.2.2

("Lexical" refers to the literals, "Canonical" refers to what they should evaluate to in the Schema spec parlance)

Sorry, I'm a huge XML/Schema nerd and that was just nibbling at my brain.

error while building tool to convert xsd to go struct

ip % go get -u github.com/metaleap/go-xsd/xsd-makepkg
# github.com/metaleap/go-util/fs
/home/ip/go/src/github.com/metaleap/go-util/fs/watcher-default.go:32:22: undefined: fsnotify.FileEvent
/home/ip/go/src/github.com/metaleap/go-util/fs/watcher-default.go:64:35: undefined: fsnotify.FileEvent
/home/ip/go/src/github.com/metaleap/go-util/fs/watcher-default.go:72:44: undefined: fsnotify.FileEvent
/home/ip/go/src/github.com/metaleap/go-util/fs/watcher-default.go:79:18: me.Event undefined (type *Watcher has no field or method Event)
/home/ip/go/src/github.com/metaleap/go-util/fs/watcher-default.go:83:21: cannot assign type func(<T>) to onEvt (type func(<T>)) in range
/home/ip/go/src/github.com/metaleap/go-util/fs/watcher-default.go:97:18: me.Error undefined (type *Watcher has no field or method Error)
/home/ip/go/src/github.com/metaleap/go-util/fs/watcher-default.go:119:15: me.Watch undefined (type *Watcher has no field or method Watch)

SimpleContentExtensions not working

In file elemmakepkg.go, line 284
if ctBaseType = bag.resolveQnameRef(ctBaseType, "T", nil); len(ctBaseType) > 0 {
td.addEmbed(nil, bag.safeName(ctBaseType))
} else if ctValueType = bag.resolveQnameRef(ctValueType, "T", nil); len(ctValueType) > 0 {
Here, ctBaseType may contain "." when the simpleContent is extended from built-in type. And when you run safeName again here, you get rid of the dot, and cause the generated file has compile error. My easy fix here is don't call safeName here, because the resolveQnameRef more like guaranteed the name is already safe. Maybe fix the implementation of safeName to whitelist "." is a better fix.

You can reproduce the issue by using https://raw.githubusercontent.com/chrisdinn/vast/master/lib/vast3_draft.xsd

ComplexType in SimpleContent will cause unmarshal fail

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?

go get issue: cannot get go-forks/fsnotify

Hi,

I am new to Go, and I was trying to clone the repo with following command

env GIT_TERMINAL_PROMPT=1 go get -u github.com/metaleap/go-xsd/xsd-makepkg

I am getting the following error:

Cloning into '$GOPATH/src/github.com/go-forks/fsnotify'...

remote: Repository not found.
fatal: repository 'https://github.com/go-forks/fsnotify/' not found
package github.com/go-forks/fsnotify: exit status 128

Not sure how to resolve this issue. Would appreciate any pointers..

Thanks...

Error adding musicxml

I tried to add musicxml support by adding the following bottom three values to schemas:

schemas = []string{
        "www.w3.org/2001/xml.xsd",
...
        "khronos.org/files/collada_schema_1_5",
        "www.musicxml.org/xsd/xml.xsd",
        "www.musicxml.org/xsd/xlink.xsd",
        "www.musicxml.org/xsd/musicxml.xsd",
    }

After this, I can rebuild and run xsd-makepkg and it properly generates musicxml.xsd.go, xlink.xsd.go, and xml.xsd.go. However, when creating a boilerplate program and simply including this new package:

package main
import fmt "fmt"
import "github.com/metaleap/go-xsd-pkg/www.musicxml.com/xsd/musicxml.xsd_go"

I get the following error at the command line when building or running this test program:

# github.com/metaleap/go-xsd-pkg/www.musicxml.com/xsd/musicxml.xsd_go
../../github.com/metaleap/go-xsd-pkg/www.musicxml.com/xsd/musicxml.xsd_go/musicxml.xsd.go:2234: undefined: XsdtNonNegativeInteger

I get the same error in a test.go file I created based on your other tests in go-xsd

Am I doing something wrong?

Undefined has attribute error.

Great library - thanks for the work! When I try to generate a go file from an XSD, I get a error compiling the file:

assets\model\items.xsd_go\items.xsd.go:430: undefined: XsdGoPkgHasAttr_Uniquename

on this code block generated:


type XsdGoPkgHasAtts_ItemBase struct {
	XsdGoPkgHasAttr_Uniquename

	XsdGoPkgHasAttr_Tier_Ttiernumber_

	XsdGoPkgHasAttr_Unlockedtocraft_XsdtBoolean_False

	XsdGoPkgHasAttr_Foodcategory

}

XSD file:
https://www.dropbox.com/s/6lwj4maomah7pn0/items.xsd?dl=0
GO file:
https://www.dropbox.com/s/lqy97t53tmxwvir/items.xsd.go?dl=0
XML file:
https://www.dropbox.com/s/zcqdvrypmv9jflz/items.xml?dl=0

License Missing

Hi Phil,

Am I right in assuming you are putting this code public domain or is there a specific license you are releasing your generator under?

Cheers,

James

What is type of license?

Hi.

Your work is so great!
What is type of license of this library?
Because I couldn't find the license file, where is it?

Best regards,
Takuya Ueda.

The order of elements in a go struct varies from the order in xsd

Hi,

First of all this is a wonderful package which reduced my task significantly. So kudos for that.

Secondly, I am trying to use the go-xsd package to convert a xsd file to go schema.
As my xsd uses xs:Sequence tag, I want the xsd order to be maintained in go struct.
The order of attributes of the go structs which are generated differ from the order specified in xsd. Moreover, the order of attributes of go struct is different every time.

This results in an incorrect order of attribute tags in xml request while serialization of go struct.

Please let me know if you already have a functionality to maintain the xsd order (by using some flag) or you plan to include this.

Thanks,
Ashish

Create a Golang struct from an XSD

I want to create a Golang struct from an XSD (Structure XSD).

I've read the post http://stackoverflow.com/questions/20734450/generate-go-structs-from-xsd which recommend using go-xsd, but I have downloaded go-xsd and installed xsd-makepkg and I cannot generate my struct.

What I am doing?

xsd-makepkg -basepath="/Users/XSD_Access/" -goinst=false

-xsd-makepkg: it is the binary create from apart go-xsd-pkg
-basepath: Contains the route where I have Structure XSD that I want to transform to struct.
-goinst : I have not installed go-buildrun and I think it is not neccesary , for that reason is ser false

What is the result of the command?

A folder($GOPATH/usr/Users/XSD_Access/) that contains other folders with all followers XML wrappers

  • docbook.org
  • docs.oasis-open.org
  • kbcafe.com
  • khronos.org
  • schemas.opengis.net
  • thearchitect.co.uk
  • Users
  • www.w3.org

Structure XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xs:element name="Request" type="Request"/>
   <xs:complexType name="Request">
      <xs:annotation>
         <xs:documentation xml:lang="sp"><![CDATA[
        Comment xxxxx
     ]]></xs:documentation>
  </xs:annotation>
  <xs:sequence>
     <xs:element name="idOne" type="xs:string" minOccurs="0" maxOccurs="1">
      <xs:annotation>
     <xs:documentation xml:lang="sp"><![CDATA[Comment xxxxx
     ]]></xs:documentation>
      </xs:annotation>
    </xs:element>
         <xs:element name="idTwo" type="xs:string" minOccurs="0" maxOccurs="1">
  <xs:annotation>
     <xs:documentation xml:lang="sp"><![CDATA[Comment xxxxxx
     ]]></xs:documentation>
  </xs:annotation>
</xs:element>
  </xs:sequence>
   </xs:complexType>
</xs:schema>

Can anyone tell me what I am doing wrong or what step I missed that it does not let me create a struct from my Structure XSD?

Thanks in advance

LICENSE file

I know this was mentioned in a few other already closed issues, but there's still no LICENSE file, and the README doesn't list a license. While it's clear from the previous issues that you're fine with anyone doing anything, if there is no license actually listed it means it is illegal for people to use this code. (Yes, the law is stupid at times.) Please include a LICENSE file, whatever it is, so that people can legally use this code.

As a side note, while I appreciate the copyright flippancy behind the WTF license that actually makes it harder for some people to use, as they have to get a license approved by corporate lawyers. Generally they've already approved MIT, BSD, and other licenses that are effectively identical to the WTF license, so using WTF actually inhibits reuse. I'd recommend using MIT or BSD instead for an actual license.

(Disclaimer: I used to be Director of Legal Affairs for the Drupal project, and am helping to establish the new Drupal licensing team. I've spent a lot of time dealing with the intricacies of Open Source licensing; I'm speaking from experience and lengthy conversations with copyright attorneys, not just out of my arse.)

Import cycle not allowed

Hello, in my job I need to implement the CSW 3 norm in Go, and I found your library. However, with this command: go run main.go -uri="http://schemas.opengis.net/cat/csw/3.0/cswAll.xsd"

I get

import cycle not allowed
package github.com/metaleap/go-xsd-pkg/schemas.opengis.net/cat/csw/3.0/cswAll.xsd_go
        imports github.com/metaleap/go-xsd-pkg/schemas.opengis.net/gml/3.2.1/gml.xsd_go
        imports github.com/metaleap/go-xsd-pkg/schemas.opengis.net/iso/19139/20070417/gmd/gmd.xsd_go
        imports github.com/metaleap/go-xsd-pkg/schemas.opengis.net/iso/19139/20070417/gco/gco.xsd_go
        imports github.com/metaleap/go-xsd-pkg/schemas.opengis.net/gml/3.2.1/gml.xsd_go

exit status 1

What do you think about it ? Maybe I could trick the program by removing a dependancy but if you have an idea I'd be glad to know :)

Nil pointer dereference

Hi, first of all thanks for this package!

I'm trying to generate go structs from the file AllSchemas.xsd. I use the following command...

xsd-makepkg -local=true -uri="AllSchemas.xsd" -basepath="github.com/coussej/b2mml"

... which results in a nil pointer dereference error:

2017/04/18 11:59:14 LOAD:	AllSchemas.xsd
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x70 pc=0x7707a]

goroutine 1 [running]:
panic(0x256580, 0xc4200140b0)
	/usr/local/Cellar/go/1.7.1/libexec/src/runtime/panic.go:500 +0x1a1
github.com/metaleap/go-xsd.(*RestrictionSimpleEnumeration).makePkg(0xc420123c80, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/elemmakepkg.go:620 +0x8a
github.com/metaleap/go-xsd.(*hasElemsEnumeration).makePkg(0xc4201944e0, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/hasmakepkg.go:89 +0x55
github.com/metaleap/go-xsd.(*RestrictionSimpleContent).makePkg(0xc420194420, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/elemmakepkg.go:605 +0x111
github.com/metaleap/go-xsd.(*hasElemRestrictionSimpleContent).makePkg(0xc42013ee70, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/hasmakepkg.go:221 +0x47
github.com/metaleap/go-xsd.(*SimpleContent).makePkg(0xc42013ee00, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/elemmakepkg.go:731 +0x73
github.com/metaleap/go-xsd.(*hasElemSimpleContent).makePkg(0xc4200f1f78, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/hasmakepkg.go:251 +0x47
github.com/metaleap/go-xsd.(*ComplexType).makePkg(0xc4200f1e60, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/elemmakepkg.go:179 +0x371
github.com/metaleap/go-xsd.(*hasElemsComplexType).makePkg(0xc4200c0b80, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/hasmakepkg.go:71 +0x55
github.com/metaleap/go-xsd.(*Schema).makePkg(0xc4200c0a00, 0xc42028a000)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/elemmakepkg.go:705 +0xd6
github.com/metaleap/go-xsd.(*Schema).MakeGoPkgSrcFile(0xc4200c0500, 0xe, 0xc420055e01, 0xc4200c0500, 0x0)
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/xsd-schema.go:171 +0x377
main.main()
	/Users/coussej/Code/go/src/github.com/metaleap/go-xsd/xsd-makepkg/main.go:65 +0x21a

I also tried serving the files over a local server, but this results in the exact same error!

The complete schema can be found attached: b2mml.zip

Any ideas?

Fails to compile urn:ietf:params:xml:schema:eppcom-1.0

urn:ietf:params:xml:schema:eppcom-1.0 is part of the IETF schemas (https://www.iana.org/assignments/xml-registry/xml-registry.xhtml#schema). The XML Schema file can be found at https://www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd but it fails to compile:

$ ./main -uri=https://www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd
2017/06/16 13:49:41 LOAD:       https://www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd
2017/06/16 13:49:41 MKPKG:      /home/kaplan/gocode/src/github.com/metaleap/go-xsd-pkg/www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd_go/eppcom-1.0.xsd.go
2017/06/16 13:49:41 [RUN]       go install github.com/metaleap/go-xsd-pkg/www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd_go
# github.com/metaleap/go-xsd-pkg/www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd_go
../../../../gocode/src/github.com/metaleap/go-xsd-pkg/www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd_go/eppcom-1.0.xsd.go:70: undefined: XsdtNormalizedString

exit status 2
2017/06/16 13:49:42 TOTAL BUILD TIME: 79.950966ms

Idiot-proof how to install and use go-xsd

Being an idiot - I still have absolutely no idea on how to use this tool after reading the README.
As the title says it - may I suggest an idiot-proof guide on how to install and use this package for all idiots out there like me. It could be a step by step guide for instance.

For example;

  1. go get github.com/metaleap/go-xsd
  2. ...

Much thanks!

Strip unused attributes on Marshal

I am not sure if this is an issue with this library or with the Marshal command. However, I am trying to generate (not read) an SVG file, using the generated SVG type definitions. The files are being generated, however, elements are including every single possible attribute that a given element could have, just with an empty string value.

While technically legal XML at that point and technically valid SVG, it's hugely wasteful (lots of unnecessary bytes to send in an HTTP request) and very difficult to read. Is there some way to cause empty attributes to simply be skipped? A given Walk command maybe, or some parameter to Marshal, or...?

Any tips welcome.

has the Identifier schema changed in go 1.1?

when I run this command:

./xsd-makepkg -uri="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd"

I'm getting

2013/07/29 09:35:43 GOFMT: /Code/go-projects/src/github.com/metaleap/go-xsd-pkg/graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd_go/DWML.xsd.go:122:30: expected type, found '-'

(among many such errors)

it appears that inside the identifier, a - is no longer okay?

type XsdGoPkgHasElem_creation-dateallproductTypeschema_CreationDate_TcreationDateType_ struct {
    CreationDate *TcreationDateType `xml:"creation-date"`

}

seems that the - in "...creation-date..." above is a compiler error.

Generated structs do not conform to xs:sequence specifications

This popped up during testing today. Since xml.Marshal marshals fields in the order they are in the struct, the struct fields need to match the ordering in the schema.

<xs:element name="Top">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="A" maxOccurs="unbounded"/>
            <xs:element ref="B"/>
            <xs:element ref="C" maxOccurs="unbounded"/>
            <xs:element ref="D"/>
            <xs:element ref="E"/>
            <xs:element ref="F" minOccurs="0"/>
            <xs:element ref="G" maxOccurs="unbounded"/>
            <xs:element ref="H" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element ref="I"/>
            <xs:element ref="J" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element ref="K" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Actual Output:

type TxsdTop struct {
    XsdGoPkgHasElem_B
    XsdGoPkgHasElems_C
    XsdGoPkgHasElem_F
    XsdGoPkgHasElems_H
    XsdGoPkgHasElems_J
    XsdGoPkgHasElems_K
    XsdGoPkgHasElems_A
    XsdGoPkgHasElem_D
    XsdGoPkgHasElem_E
    XsdGoPkgHasElems_G
    XsdGoPkgHasElem_I
}

Expected Output:

type TxsdTop struct {
    XsdGoPkgHasElems_A
    XsdGoPkgHasElem_B
    XsdGoPkgHasElems_C
    XsdGoPkgHasElem_D
    XsdGoPkgHasElem_E
    XsdGoPkgHasElem_F
    XsdGoPkgHasElems_G
    XsdGoPkgHasElems_H
    XsdGoPkgHasElem_I
    XsdGoPkgHasElems_J
    XsdGoPkgHasElems_K
}

Error when go get

Showing error while running go get

go get github.com/metaleap/go-xsd

go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src;
        ignoring go.mod;
        see 'go help modules'
# github.com/metaleap/go-util/fs
../github.com/metaleap/go-util/fs/watcher-default.go:32:22: undefined: fsnotify.FileEvent
../github.com/metaleap/go-util/fs/watcher-default.go:64:35: undefined: fsnotify.FileEvent
../github.com/metaleap/go-util/fs/watcher-default.go:72:44: undefined: fsnotify.FileEvent
../github.com/metaleap/go-util/fs/watcher-default.go:79:18: me.Event undefined (type *Watcher has no field or method Event)
../github.com/metaleap/go-util/fs/watcher-default.go:83:21: cannot assign type func(<T>) to onEvt (type func(<T>)) in range
../github.com/metaleap/go-util/fs/watcher-default.go:97:18: me.Error undefined (type *Watcher has no field or method Error)
../github.com/metaleap/go-util/fs/watcher-default.go:119:15: me.Watch undefined (type *Watcher has no field or method Watch)

Make loadSchema(io.Reader....) public

In some situations, it is desirable to allow loading a schema from something else than the supported URIs.

For example, a program could have XSD definitions built in, so they don't have to be fetched from a file or the internet.

Please make loadSchema(r io.Reader, loadUri, localPath string) in xsd-schema.go public or add a new constructor that allows passing in a map of readers for the included schemas.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.