Skip to content

Commit 792d3ae

Browse files
authored
feat(xml-builder): additional serde helper methods (#5567)
* feat(xml-builder): additional serde helper methods * feat(xml-builder): restore original methods
1 parent 75eae44 commit 792d3ae

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

packages/xml-builder/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "3.465.0",
44
"description": "XML builder for the AWS SDK",
55
"dependencies": {
6-
"tslib": "^2.5.0"
6+
"tslib": "^2.5.0",
7+
"@smithy/types": "^2.7.0"
78
},
89
"scripts": {
910
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",

packages/xml-builder/src/XmlNode.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,25 @@ describe("XmlNode", () => {
103103
expect(node.removeAttribute("foo")).toBe(node);
104104
});
105105
});
106+
107+
describe("brevity methods", () => {
108+
describe('.l() "list', () => {
109+
it("creates a list node", () => {
110+
const data = { alist: [XmlNode.of("a", "aaa"), XmlNode.of("b", "bbb")] };
111+
112+
const node = new XmlNode("root");
113+
node.l(data, "alist", "member", () => data.alist);
114+
115+
expect(node.toString()).toEqual("<root><member>aaa</member><member>bbb</member></root>");
116+
});
117+
});
118+
describe('.lc() "list with container"', () => {
119+
const data = { alist: [XmlNode.of("a", "aaa"), XmlNode.of("b", "bbb")] };
120+
121+
const node = new XmlNode("root");
122+
node.lc(data, "alist", "member", () => data.alist);
123+
124+
expect(node.toString()).toEqual("<root><member><a>aaa</a><b>bbb</b></member></root>");
125+
});
126+
});
106127
});

packages/xml-builder/src/XmlNode.ts

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { XmlText } from "./XmlText";
44

55
/**
66
* @internal
7-
*
7+
*
88
* Represents an XML node.
99
*/
1010
export class XmlNode {
@@ -21,36 +21,106 @@ export class XmlNode {
2121
return node;
2222
}
2323

24-
constructor(private name: string, public readonly children: Stringable[] = []) {}
24+
public constructor(private name: string, public readonly children: Stringable[] = []) {}
2525

26-
withName(name: string): XmlNode {
26+
public withName(name: string): XmlNode {
2727
this.name = name;
2828
return this;
2929
}
3030

31-
addAttribute(name: string, value: any): XmlNode {
31+
public addAttribute(name: string, value: any): XmlNode {
3232
this.attributes[name] = value;
3333
return this;
3434
}
3535

36-
addChildNode(child: Stringable): XmlNode {
36+
public addChildNode(child: Stringable): XmlNode {
3737
this.children.push(child);
3838
return this;
3939
}
4040

41-
removeAttribute(name: string): XmlNode {
41+
public removeAttribute(name: string): XmlNode {
4242
delete this.attributes[name];
4343
return this;
4444
}
4545

46-
toString(): string {
46+
/**
47+
* @internal
48+
* Alias of {@link XmlNode#withName(string)} for codegen brevity.
49+
*/
50+
public n(name: string): XmlNode {
51+
this.name = name;
52+
return this;
53+
}
54+
55+
/**
56+
* @internal
57+
* Alias of {@link XmlNode#addChildNode(string)} for codegen brevity.
58+
*/
59+
public c(child: Stringable): XmlNode {
60+
this.children.push(child);
61+
return this;
62+
}
63+
64+
/**
65+
* @internal
66+
* Checked version of {@link XmlNode#addAttribute(string)} for codegen brevity.
67+
*/
68+
public a(name: string, value: any): XmlNode {
69+
if (value != null) {
70+
this.attributes[name] = value;
71+
}
72+
return this;
73+
}
74+
75+
/**
76+
* Create a child node.
77+
* Used in serialization of string fields.
78+
* @internal
79+
*/
80+
public cc(input: any, field: string, withName: string = field): void {
81+
if (input[field] != null) {
82+
const node = XmlNode.of(field, input[field]).withName(withName);
83+
this.c(node);
84+
}
85+
}
86+
87+
/**
88+
* Creates list child nodes.
89+
* @internal
90+
*/
91+
public l(input: any, listName: string, memberName: string, valueProvider: Function): void {
92+
if (input[listName] != null) {
93+
const nodes = valueProvider();
94+
nodes.map((node: any) => {
95+
node.withName(memberName);
96+
this.c(node);
97+
});
98+
}
99+
}
100+
101+
/**
102+
* Creates list child nodes with container.
103+
* @internal
104+
*/
105+
public lc(input: any, listName: string, memberName: string, valueProvider: Function): void {
106+
if (input[listName] != null) {
107+
const nodes = valueProvider();
108+
const containerNode = new XmlNode(memberName);
109+
nodes.map((node: any) => {
110+
containerNode.c(node);
111+
});
112+
this.c(containerNode);
113+
}
114+
}
115+
116+
public toString(): string {
47117
const hasChildren = Boolean(this.children.length);
48118
let xmlText = `<${this.name}`;
49119
// add attributes
50120
const attributes = this.attributes;
51121
for (const attributeName of Object.keys(attributes)) {
52122
const attribute = attributes[attributeName];
53-
if (typeof attribute !== "undefined" && attribute !== null) {
123+
if (attribute != null) {
54124
xmlText += ` ${attributeName}="${escapeAttribute("" + attribute)}"`;
55125
}
56126
}

0 commit comments

Comments
 (0)