Skip to content

Commit ca26c15

Browse files
authored
Adding pointers to optional attributes (#102)
1 parent 4e70015 commit ca26c15

File tree

9 files changed

+36
-29
lines changed

9 files changed

+36
-29
lines changed

genGo.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,26 @@ func (gen *CodeGenerator) GoComplexType(v *ComplexType) {
193193
}
194194

195195
for _, attribute := range v.Attributes {
196+
fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree))
196197
var optional string
197198
if attribute.Optional {
198-
optional = `,omitempty`
199+
if !strings.HasPrefix(fieldType, `*`) {
200+
fieldType = "*" + fieldType
201+
} else {
202+
optional = `,omitempty`
203+
}
199204
}
200-
fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree))
201205
if fieldType == "time.Time" {
202206
gen.ImportTime = true
203207
}
204208
content += fmt.Sprintf("\t%sAttr\t%s\t`xml:\"%s,attr%s\"`\n", genGoFieldName(attribute.Name, false), fieldType, attribute.Name, optional)
205209
}
206210
for _, group := range v.Groups {
207-
var plural string
211+
fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree))
208212
if group.Plural {
209-
plural = "[]"
213+
fieldType = "[]" + fieldType
210214
}
211-
content += fmt.Sprintf("\t%s\t%s%s\n", genGoFieldName(group.Name, false), plural, genGoFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree)))
215+
content += fmt.Sprintf("\t%s\t%s\n", genGoFieldName(group.Name, false), fieldType)
212216
}
213217

214218
for _, element := range v.Elements {

genJava.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ func (gen *CodeGenerator) JavaComplexType(v *ComplexType) {
149149
}
150150

151151
for _, attribute := range v.Attributes {
152-
required := ", required = true"
152+
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree))
153+
required := `required = true, `
153154
if attribute.Optional {
154155
required = ""
155156
}
156-
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree))
157-
content += fmt.Sprintf("\t@XmlAttribute(name = \"%s\"%s)\n\tprotected %s %sAttr;\n", attribute.Name, required, fieldType, genJavaFieldName(attribute.Name, false))
157+
content += fmt.Sprintf("\t@XmlAttribute(%sname = \"%s\")\n\tprotected %s %sAttr;\n", required, attribute.Name, fieldType, genJavaFieldName(attribute.Name, false))
158158
}
159159
for _, group := range v.Groups {
160160
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree))

genTypeScript.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,15 @@ func (gen *CodeGenerator) TypeScriptComplexType(v *ComplexType) {
151151
}
152152

153153
for _, attribute := range v.Attributes {
154-
var optional string
154+
fieldType := genTypeScriptFieldType(
155+
getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree),
156+
attribute.Plural,
157+
)
158+
fieldName := genTypeScriptFieldName(attribute.Name, false) + "Attr"
155159
if attribute.Optional {
156-
optional = ` | null`
160+
fieldName += "?"
157161
}
158-
fieldType := genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree), attribute.Plural)
159-
content += fmt.Sprintf("\t%sAttr: %s%s;\n", genTypeScriptFieldName(attribute.Name, false), fieldType, optional)
162+
content += fmt.Sprintf("\t%s: %s;\n", fieldName, fieldType)
160163
}
161164
for _, group := range v.Groups {
162165
content += fmt.Sprintf("\t%s: %s;\n", genTypeScriptFieldName(group.Name, false), genTypeScriptFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree), group.Plural))

test/c/base64.xsd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ typedef struct {
5353
// TopLevel ...
5454
typedef struct {
5555
float CostAttr; // attr, optional
56-
char LastUpdatedAttr; // attr, optional
56+
char LastUpdatedAttr; // attr
5757
MyType7 Nested;
5858
char MyType1[];
5959
MyType2 MyType2[];

test/go/base64.xsd.go

Lines changed: 6 additions & 6 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class MyType6 {
6464

6565
// MyType7 ...
6666
public class MyType7 {
67-
@XmlAttribute(name = "origin", required = true)
67+
@XmlAttribute(required = true, name = "origin")
6868
protected String OriginAttr;
6969
@XmlValue
7070
protected String value;
@@ -92,7 +92,7 @@ public class MyType10 {
9292
public class TopLevel extends MyType6 {
9393
@XmlAttribute(name = "cost")
9494
protected Float CostAttr;
95-
@XmlAttribute(name = "LastUpdated")
95+
@XmlAttribute(required = true, name = "LastUpdated")
9696
protected String LastUpdatedAttr;
9797
@XmlElement(name = "nested")
9898
protected MyType7 Nested;

test/rs/base64.xsd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub struct TopLevel {
106106
#[serde(rename = "cost")]
107107
pub cost: Option<f64>,
108108
#[serde(rename = "LastUpdated")]
109-
pub last_updated: Option<u8>,
109+
pub last_updated: u8,
110110
#[serde(rename = "nested")]
111111
pub nested: Option<MyType7>,
112112
#[serde(rename = "myType1")]

test/ts/base64.xsd.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ export type MyType1 = Uint8Array;
55

66
// MyType2 ...
77
export class MyType2 {
8-
LengthAttr: number | null;
8+
LengthAttr?: number;
99
Value: Uint8Array;
1010
}
1111

1212
// MyType3 ...
1313
export class MyType3 {
14-
LengthAttr: number | null;
14+
LengthAttr?: number;
1515
Value: string;
1616
}
1717

@@ -28,8 +28,8 @@ export type MyType5 = string;
2828

2929
// MyType6 ...
3030
export class MyType6 {
31-
CodeAttr: string | null;
32-
IdentifierAttr: number | null;
31+
CodeAttr?: string;
32+
IdentifierAttr?: number;
3333
}
3434

3535
// MyType7 ...
@@ -55,8 +55,8 @@ export class MyType10 {
5555

5656
// TopLevel ...
5757
export class TopLevel extends MyType6 {
58-
CostAttr: number | null;
59-
LastUpdatedAttr: string | null;
58+
CostAttr?: number;
59+
LastUpdatedAttr: string;
6060
Nested?: MyType7;
6161
MyType1: Uint8Array;
6262
MyType2: Array<MyType2>;

test/xsd/base64.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
</choice>
8585
</sequence>
8686
<attribute name="cost" type="double"/>
87-
<attribute name="LastUpdated" type="dateTime"/>
87+
<attribute name="LastUpdated" type="dateTime" use="required"/>
8888
</extension>
8989
</complexContent>
9090
</complexType>

0 commit comments

Comments
 (0)