Skip to content

Commit aee1e76

Browse files
authored
This fixes #47, support plural items on max occurs on sequence elements (#97)
1 parent d9d0ba0 commit aee1e76

File tree

9 files changed

+151
-0
lines changed

9 files changed

+151
-0
lines changed

parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Options struct {
4242
InGroup int
4343
InUnion bool
4444
InAttributeGroup bool
45+
InPluralSequence []bool
4546

4647
SimpleType *Stack
4748
ComplexType *Stack

test/c/base64.xsd.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ typedef struct {
3434
char OriginAttr; // attr
3535
} MyType7;
3636

37+
// MyType8 ...
38+
typedef struct {
39+
MyType4 Title[];
40+
} MyType8;
41+
42+
// MyType9 ...
43+
typedef struct {
44+
MyType4 Title[];
45+
} MyType9;
46+
47+
// MyType10 ...
48+
typedef struct {
49+
MyType4 Title;
50+
} MyType10;
51+
3752
// TopLevel ...
3853
typedef struct {
3954
float CostAttr; // attr, optional

test/go/base64.xsd.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/java/base64.xsd.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ public class MyType7 {
6868
protected String value;
6969
}
7070

71+
// MyType8 ...
72+
public class MyType8 {
73+
@XmlElement(required = true, name = "title")
74+
protected List<MyType4> Title;
75+
}
76+
77+
// MyType9 ...
78+
public class MyType9 {
79+
@XmlElement(required = true, name = "title")
80+
protected List<MyType4> Title;
81+
}
82+
83+
// MyType10 ...
84+
public class MyType10 {
85+
@XmlElement(required = true, name = "title")
86+
protected MyType4 Title;
87+
}
88+
7189
// TopLevel ...
7290
public class TopLevel extends MyType6 {
7391
@XmlAttribute(name = "cost")

test/rs/base64.xsd.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ pub struct MyType7 {
7474
}
7575

7676

77+
// MyType8 ...
78+
#[derive(Debug, Deserialize, Serialize, PartialEq)]
79+
pub struct MyType8 {
80+
#[serde(rename = "title")]
81+
pub title: Vec<MyType4>,
82+
}
83+
84+
85+
// MyType9 ...
86+
#[derive(Debug, Deserialize, Serialize, PartialEq)]
87+
pub struct MyType9 {
88+
#[serde(rename = "title")]
89+
pub title: Vec<MyType4>,
90+
}
91+
92+
93+
// MyType10 ...
94+
#[derive(Debug, Deserialize, Serialize, PartialEq)]
95+
pub struct MyType10 {
96+
#[serde(rename = "title")]
97+
pub title: MyType4,
98+
}
99+
100+
77101
// TopLevel ...
78102
#[derive(Debug, Deserialize, Serialize, PartialEq)]
79103
pub struct TopLevel {

test/ts/base64.xsd.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ export class MyType7 {
3737
Value: string;
3838
}
3939

40+
// MyType8 ...
41+
export class MyType8 {
42+
Title: Array<MyType4>;
43+
}
44+
45+
// MyType9 ...
46+
export class MyType9 {
47+
Title: Array<MyType4>;
48+
}
49+
50+
// MyType10 ...
51+
export class MyType10 {
52+
Title: MyType4;
53+
}
54+
4055
// TopLevel ...
4156
export class TopLevel extends MyType6 {
4257
CostAttr: number | null;

test/xsd/base64.xsd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@
5353
</simpleContent>
5454
</complexType>
5555

56+
<complexType name="MyType8">
57+
<sequence maxOccurs="unbounded">
58+
<element name="title" type="here:myType4" />
59+
</sequence>
60+
</complexType>
61+
62+
<complexType name="MyType9">
63+
<sequence maxOccurs="2">
64+
<element name="title" type="here:myType4" />
65+
</sequence>
66+
</complexType>
67+
68+
<complexType name="MyType10">
69+
<sequence maxOccurs="1">
70+
<element name="title" type="here:myType4" />
71+
</sequence>
72+
</complexType>
73+
5674
<element name="TopLevel">
5775
<complexType>
5876
<complexContent>

xmlElement.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (opt *Options) OnElement(ele xml.StartElement, protoTree []interface{}) (er
5959
}
6060
}
6161

62+
if len(opt.InPluralSequence) > 0 && opt.InPluralSequence[len(opt.InPluralSequence)-1] {
63+
e.Plural = true
64+
}
65+
6266
alreadyPushedElement := false
6367
if e.Type == "" {
6468
e.Type, err = opt.GetValueType(e.Name, protoTree)

xmlSequence.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2020 - 2024 The xgen Authors. All rights reserved. Use of this
2+
// source code is governed by a BSD-style license that can be found in the
3+
// LICENSE file.
4+
//
5+
// Package xgen written in pure Go providing a set of functions that allow you
6+
// to parse XSD (XML schema files). This library needs Go version 1.10 or
7+
// later.
8+
9+
package xgen
10+
11+
import (
12+
"encoding/xml"
13+
"strconv"
14+
)
15+
16+
// OnSequence evaluates wether the sequence element contains a maxOccurs attribute
17+
// (that in turn mandates plural inner elements) and saves that info on a stack
18+
func (opt *Options) OnSequence(ele xml.StartElement, protoTree []interface{}) (err error) {
19+
for _, attr := range ele.Attr {
20+
if attr.Name.Local == "maxOccurs" {
21+
if attr.Value == "unbounded" {
22+
opt.InPluralSequence = append(opt.InPluralSequence, true)
23+
return nil
24+
}
25+
26+
var maxOccurs int
27+
if maxOccurs, err = strconv.Atoi(attr.Value); err == nil && maxOccurs > 1 {
28+
opt.InPluralSequence = append(opt.InPluralSequence, true)
29+
return nil
30+
}
31+
}
32+
}
33+
opt.InPluralSequence = append(opt.InPluralSequence, false)
34+
return nil
35+
}
36+
37+
// EndSequence removes an item from the stack mentioned above
38+
func (opt *Options) EndSequence(ele xml.EndElement, protoTree []interface{}) (err error) {
39+
opt.InPluralSequence = opt.InPluralSequence[:len(opt.InPluralSequence)-1]
40+
return
41+
}

0 commit comments

Comments
 (0)