Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More extensive use of omitempty #118

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/gocomply_xsd2go
/tests/test-schemas/
19 changes: 17 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
GO=GO111MODULE=on go
GOBUILD=$(GO) build
XSD_TEST_DIR=./tests/xsd-examples/valid
TEST_MODELS_DIR=./tests/test-schemas/

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -8,12 +10,17 @@ else
GOBIN=$(shell go env GOBIN)
endif

.PHONY: pkger vendor build clean all ci-update-bundled-deps check

all: build

build:
build: ci-update-bundled-deps
$(GOBUILD) ./cli/gocomply_xsd2go

.PHONY: pkger vendor
clean:
rm -f ./gocomply_xsd2go
rm -rf $(TEST_MODELS_DIR)

pkger:
ifeq ("$(wildcard $(GOBIN)/pkger)","")
go install github.com/markbates/pkger/cmd/pkger@latest
Expand All @@ -23,6 +30,14 @@ ci-update-bundled-deps: pkger
$(GOBIN)/pkger -o pkg/template
go fmt ./pkg/template

generate-test-schemas: build $(XSD_TEST_DIR)/*.xsd
@for xsd in $(shell ls $(XSD_TEST_DIR)/*.xsd); do \
./gocomply_xsd2go convert $${xsd} "" $(TEST_MODELS_DIR); \
done

check: generate-test-schemas
$(GO) test -v -cover -coverpkg=./... -tags=xml_test ./tests

vendor:
$(GO) mod tidy
$(GO) mod vendor
Expand Down
2 changes: 1 addition & 1 deletion pkg/template/pkged.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/template/types.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
{{range .ExportableElements }}
// Element
type {{ .GoName }} struct {
XMLName xml.Name `xml:"{{.Name}}"`
XMLName xml.Name `xml:"{{.Name}}{{.Modifiers}}"`
{{ range .Attributes }}
{{ .GoName }} {{.GoForeignModule}}{{.GoType}} `xml:"{{.XmlName}},{{.Modifiers}}"`
{{end }}

{{ range .Elements }}
{{ .GoFieldName}} {{.GoMemLayout}}{{.GoForeignModule}}{{ .GoTypeName }} `xml:"{{.XmlName}}"`
{{ .GoFieldName}} {{.GoMemLayout}}{{.GoForeignModule}}{{ .GoTypeName }} `xml:"{{.XmlName}}{{.Modifiers}}"`
{{ end }}

{{- if .ContainsText }}
Expand All @@ -39,7 +39,7 @@ import (
{{end }}

{{ range .Elements }}
{{ .GoFieldName}} {{.GoMemLayout}}{{.GoForeignModule}}{{ .GoTypeName }} `xml:"{{.XmlName}}"`
{{ .GoFieldName}} {{.GoMemLayout}}{{.GoForeignModule}}{{ .GoTypeName }} `xml:"{{.XmlName}}{{.Modifiers}}"`
{{end}}

{{- if .ContainsText }}
Expand Down
2 changes: 1 addition & 1 deletion pkg/xsd/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (a *Attribute) XmlName() string {
}

func (a *Attribute) optional() bool {
return a.Use == "optional"
return a.Use == "" || a.Use == "optional"
}

func (a *Attribute) compile(s *Schema) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/xsd/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ func (e *Element) GoForeignModule() string {
return ""
}

func (e *Element) Modifiers() string {
res := ""
if e.optional() {
res += ",omitempty"
}
return res
}

func (e *Element) optional() bool {
return e.MinOccurs == "0"
}

func (e *Element) XmlName() string {
if e.XmlNameOverride != "" {
return e.XmlNameOverride
Expand Down
4 changes: 4 additions & 0 deletions tests/xml-examples/complex.xsd.1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<myelement>
<id>1</id>
<external_id>2</external_id>
</myelement>
4 changes: 4 additions & 0 deletions tests/xml-examples/complex.xsd.2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<myelement>
<id>1</id>
<id_1>2</id_1>
</myelement>
5 changes: 5 additions & 0 deletions tests/xml-examples/complex.xsd.3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<myelement>
<id>1</id>
<id_21>2</id_21>
<id_22>3</id_22>
</myelement>
45 changes: 45 additions & 0 deletions tests/xml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build xml_test

package tests

import (
"encoding/xml"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/gocomply/xsd2go/tests/test-schemas/complex"
)

func TestMarshalComplex(t *testing.T) {
var v complex.Myelement
assertMarshal(&v, "xml-examples/complex.xsd.1.xml", t)
var z complex.Myelement
assertMarshal(&z, "xml-examples/complex.xsd.2.xml", t)
var x complex.Myelement
assertMarshal(&x, "xml-examples/complex.xsd.3.xml", t)
}

func assertMarshal(v any, xmlPath string, t *testing.T) {
//unmarshal xml file into v
in, err := os.ReadFile(xmlPath)
if err != nil {
t.Fatalf("Failure opening test file: %v", err)
}
err = xml.Unmarshal(in, v)
if err != nil {
t.Fatalf("Failure parsing test file: %v", err)
}
//marshal v into buff and compare
out, err := xml.MarshalIndent(v, "", " ")
out = append(out, '\n')
if err != nil {
t.Fatalf("Failure marshalling output: %v", err)
}
expected, err := os.ReadFile(xmlPath)
if err != nil {
t.Fatalf("Failure reading result file: %v", err)
}
assert.Equal(t, string(expected), string(out))
}
4 changes: 2 additions & 2 deletions tests/xsd-examples/valid/complex.xsd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:simple-schema="https://simple.example.com/" targetNamespace="https://simple.example.com/" elementFormDefault="qualified">
<xsd:element name="myelement" type="simple-schema:MyElementType" />
xmlns:complex="https://simple.example.com/" targetNamespace="https://simple.example.com/" elementFormDefault="qualified">
<xsd:element name="myelement" type="complex:MyElementType" />
<xsd:complexType name="MyElementType">
<xsd:sequence>
<xsd:element name="id" type="xsd:integer" />
Expand Down
18 changes: 9 additions & 9 deletions tests/xsd-examples/valid/complex.xsd.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by https://github.com/gocomply/xsd2go; DO NOT EDIT.
// Models for https://simple.example.com/
package simple_schema
package complex

import (
"encoding/xml"
Expand All @@ -12,13 +12,13 @@ type Myelement struct {

Id int64 `xml:"id"`

ExternalId *int64 `xml:"external_id"`
ExternalId *int64 `xml:"external_id,omitempty"`

Id1 *int `xml:"id_1"`
Id1 *int `xml:"id_1,omitempty"`

Id21 *int `xml:"id_21"`
Id21 *int `xml:"id_21,omitempty"`

Id22 *int `xml:"id_22"`
Id22 *int `xml:"id_22,omitempty"`
}

// XSD ComplexType declarations
Expand All @@ -28,13 +28,13 @@ type MyElementType struct {

Id int64 `xml:"id"`

ExternalId *int64 `xml:"external_id"`
ExternalId *int64 `xml:"external_id,omitempty"`

Id1 *int `xml:"id_1"`
Id1 *int `xml:"id_1,omitempty"`

Id21 *int `xml:"id_21"`
Id21 *int `xml:"id_21,omitempty"`

Id22 *int `xml:"id_22"`
Id22 *int `xml:"id_22,omitempty"`
}

// XSD SimpleType declarations
16 changes: 8 additions & 8 deletions tests/xsd-examples/valid/restriction.xsd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:simple-schema="https://simple.example.com/" targetNamespace="https://simple.example.com/" elementFormDefault="qualified">
xmlns:restriction="https://simple.example.com/" targetNamespace="https://simple.example.com/" elementFormDefault="qualified">
<xsd:complexType name="StateRefType">
<xsd:attribute name="state_ref" type="StateIDPattern" use="required"/>
<xsd:attribute name="state_ref" type="restriction:StateIDPattern" use="required"/>
</xsd:complexType>
<xsd:simpleType name="StateIDPattern">
<xsd:restriction base="xsd:string">
Expand Down Expand Up @@ -105,26 +105,26 @@
</xsd:restriction>
</xsd:simpleType>
<xsd:attributeGroup name="EntityAttributeGroup">
<xsd:attribute name="datatype" type="DatatypeEnumeration" use="optional" default="string">
<xsd:attribute name="datatype" type="restriction:DatatypeEnumeration" use="optional" default="string">
</xsd:attribute>
<xsd:attribute name="operation" type="OperationEnumeration" use="optional" default="equals">
<xsd:attribute name="operation" type="restriction:OperationEnumeration" use="optional" default="equals">
</xsd:attribute>
</xsd:attributeGroup>
<xsd:complexType name="MySimpleBaseType" abstract="true">
<xsd:simpleContent>
<xsd:extension base="xsd:anySimpleType">
<xsd:attributeGroup ref="simple-schema:EntityAttributeGroup"/>
<xsd:attributeGroup ref="restriction:EntityAttributeGroup"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:element name="myelement" type="simple-schema:MyElementType" />
<xsd:element name="myelement" type="restriction:MyElementType" />
<xsd:complexType name="MyElementType">
<xsd:simpleContent>
<xsd:restriction base="simple-schema:MySimpleBaseType">
<xsd:restriction base="restriction:MySimpleBaseType">
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:attribute name="datatype" type="SimpleDatatypeEnumeration" use="optional" fixed="string"/>
<xsd:attribute name="datatype" type="restriction:SimpleDatatypeEnumeration" use="optional" fixed="string"/>
</xsd:restriction>
</xsd:simpleContent>
</xsd:complexType>
Expand Down
2 changes: 1 addition & 1 deletion tests/xsd-examples/valid/restriction.xsd.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by https://github.com/gocomply/xsd2go; DO NOT EDIT.
// Models for https://simple.example.com/
package simple_schema
package restriction

import (
"encoding/xml"
Expand Down
12 changes: 7 additions & 5 deletions tests/xsd-examples/valid/simple-8859-1.xsd
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:simple-schema="https://simple.example.com/"
xmlns:simple-8859-1="https://simple.example.com/"
targetNamespace="https://simple.example.com/"
elementFormDefault="qualified">
<xsd:element name="myelement" type="simple-schema:MyElementType" />
<xsd:element name="myelement" type="simple-8859-1:MyElementType" />
<xsd:complexType name="MyElementType">
<xsd:sequence>
<xsd:element name="id" type="xsd:integer" />
</xsd:sequence>
<xs:annotation>
<xs:documentation>Documentation with a character from ISO-8859-1 encoding: ñ</xs:documentation>
</xs:annotation>
</xsd:complexType>

<xsd:annotation>
<xsd:documentation>Documentation with a character from ISO-8859-1 encoding: ñ</xsd:documentation>
</xsd:annotation>

</xsd:schema>
2 changes: 1 addition & 1 deletion tests/xsd-examples/valid/simple-8859-1.xsd.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by https://github.com/gocomply/xsd2go; DO NOT EDIT.
// Models for https://simple.example.com/
package simple_schema
package simple_8859_1

import (
"encoding/xml"
Expand Down
Loading
Loading