Skip to content

Commit

Permalink
feat(xml-builder): additional serde helper methods (#5567)
Browse files Browse the repository at this point in the history
* feat(xml-builder): additional serde helper methods

* feat(xml-builder): restore original methods
  • Loading branch information
kuhe authored Dec 11, 2023
1 parent 75eae44 commit 792d3ae
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/xml-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "3.465.0",
"description": "XML builder for the AWS SDK",
"dependencies": {
"tslib": "^2.5.0"
"tslib": "^2.5.0",
"@smithy/types": "^2.7.0"
},
"scripts": {
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
Expand Down
21 changes: 21 additions & 0 deletions packages/xml-builder/src/XmlNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,25 @@ describe("XmlNode", () => {
expect(node.removeAttribute("foo")).toBe(node);
});
});

describe("brevity methods", () => {
describe('.l() "list', () => {
it("creates a list node", () => {
const data = { alist: [XmlNode.of("a", "aaa"), XmlNode.of("b", "bbb")] };

const node = new XmlNode("root");
node.l(data, "alist", "member", () => data.alist);

expect(node.toString()).toEqual("<root><member>aaa</member><member>bbb</member></root>");
});
});
describe('.lc() "list with container"', () => {
const data = { alist: [XmlNode.of("a", "aaa"), XmlNode.of("b", "bbb")] };

const node = new XmlNode("root");
node.lc(data, "alist", "member", () => data.alist);

expect(node.toString()).toEqual("<root><member><a>aaa</a><b>bbb</b></member></root>");
});
});
});
86 changes: 78 additions & 8 deletions packages/xml-builder/src/XmlNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { XmlText } from "./XmlText";

/**
* @internal
*
*
* Represents an XML node.
*/
export class XmlNode {
Expand All @@ -21,36 +21,106 @@ export class XmlNode {
return node;
}

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

withName(name: string): XmlNode {
public withName(name: string): XmlNode {
this.name = name;
return this;
}

addAttribute(name: string, value: any): XmlNode {
public addAttribute(name: string, value: any): XmlNode {
this.attributes[name] = value;
return this;
}

addChildNode(child: Stringable): XmlNode {
public addChildNode(child: Stringable): XmlNode {
this.children.push(child);
return this;
}

removeAttribute(name: string): XmlNode {
public removeAttribute(name: string): XmlNode {
delete this.attributes[name];
return this;
}

toString(): string {
/**
* @internal
* Alias of {@link XmlNode#withName(string)} for codegen brevity.
*/
public n(name: string): XmlNode {
this.name = name;
return this;
}

/**
* @internal
* Alias of {@link XmlNode#addChildNode(string)} for codegen brevity.
*/
public c(child: Stringable): XmlNode {
this.children.push(child);
return this;
}

/**
* @internal
* Checked version of {@link XmlNode#addAttribute(string)} for codegen brevity.
*/
public a(name: string, value: any): XmlNode {
if (value != null) {
this.attributes[name] = value;
}
return this;
}

/**
* Create a child node.
* Used in serialization of string fields.
* @internal
*/
public cc(input: any, field: string, withName: string = field): void {
if (input[field] != null) {
const node = XmlNode.of(field, input[field]).withName(withName);
this.c(node);
}
}

/**
* Creates list child nodes.
* @internal
*/
public l(input: any, listName: string, memberName: string, valueProvider: Function): void {
if (input[listName] != null) {
const nodes = valueProvider();
nodes.map((node: any) => {
node.withName(memberName);
this.c(node);
});
}
}

/**
* Creates list child nodes with container.
* @internal
*/
public lc(input: any, listName: string, memberName: string, valueProvider: Function): void {
if (input[listName] != null) {
const nodes = valueProvider();
const containerNode = new XmlNode(memberName);
nodes.map((node: any) => {
containerNode.c(node);
});
this.c(containerNode);
}
}

public toString(): string {
const hasChildren = Boolean(this.children.length);
let xmlText = `<${this.name}`;
// add attributes
const attributes = this.attributes;
for (const attributeName of Object.keys(attributes)) {
const attribute = attributes[attributeName];
if (typeof attribute !== "undefined" && attribute !== null) {
if (attribute != null) {
xmlText += ` ${attributeName}="${escapeAttribute("" + attribute)}"`;
}
}
Expand Down

0 comments on commit 792d3ae

Please sign in to comment.