xuri / xgen

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

Generate structs in namespaces. #8

Open simenandre opened 4 years ago

simenandre commented 4 years ago

Description

Hello 👋 First of all, thank you for this library!

I'm new to XSD files. I'm having issues using xgen to generate for NORID's EPP .xsd files.

They are available here: https://teknisk.norid.no/en/registrar/system/dokumentasjon/epp-grensesnitt/

The issue I'm having is that the structs have the same name (being already defined). I can see there is a targetNamespace and other schema data that probably could fix this?

Or, maybe I'm doing something wrong?

Steps to reproduce the issue:

  1. Run xgen on EPP-XML-Schemas v1.0.2

Describe the results you received:

E.g.

// File: contact-1.0.xsd.go
type CreateType struct {
    XMLName    xml.Name          `xml:"createType"`
    ...
    Email      string            `xml:"email"`
    AuthInfo   *AuthInfoType     `xml:"authInfo"`
    Disclose   *DiscloseType     `xml:"disclose"`
}
// File: domain-1.0.xsd.go
type CreateType struct {
    XMLName    xml.Name       `xml:"createType"`
    ...
    Registrant string         `xml:"registrant"`
    Contact    []*ContactType `xml:"contact"`
    AuthInfo   *AuthInfoType  `xml:"authInfo"`
}

Describe the results you expected:

Maybe something like this:

// File: contact-1.0.xsd.go
type ContactCreateType struct {
    XMLName    xml.Name          `xml:"createType"`
    ...
    Email      string            `xml:"email"`
    AuthInfo   *AuthInfoType     `xml:"authInfo"`
    Disclose   *DiscloseType     `xml:"disclose"`
}
// File: domain-1.0.xsd.go
type DomainCreateType struct {
    XMLName    xml.Name       `xml:"createType"`
    ...
    Registrant string         `xml:"registrant"`
    Contact    []*ContactType `xml:"contact"`
    AuthInfo   *AuthInfoType  `xml:"authInfo"`
}

Output of go version:

go version go1.14.5 darwin/amd64

Environment details (OS, physical, etc.):

Mac OSX 10.15.6

awillis commented 4 years ago

Prefixing each type name with the namespace is nice. I'd also like the option of having one go package generated per namespace, with a targetNamespace constant defined in the package. Currently if I set the package name via the '-p' option and generate Go code, it only sets the package name for the go source file that corresponds to the named file. Additional schema files that have been xsd:import'ed still use the default package name 'schema'.