-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #691 from pmcelhaney/the-xml-problem
Partial support for XML
- Loading branch information
Showing
10 changed files
with
262 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"counterfact": minor | ||
--- | ||
|
||
Partial support for XML (if anyone still uses XML). See https://swagger.io/docs/specification/data-models/representing-xml/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
high code, low effort mock REST APIs | ||
Are you mocking me? | ||
stop, mock, and roll | ||
fake it till you make it | ||
things are about to get real | ||
when opportunity mocks | ||
your time is very important to us | ||
This is an HTTP joke. GET it? | ||
the REPL alliance | ||
there is no spoon | ||
what you say is what you get | ||
pay no attention to man behind the curtain | ||
at your service | ||
I'm afraid I can do that, Dave | ||
Hey! I'm mockin' here! | ||
all mock no friction | ||
a cubic zirconia in the rough | ||
maybe she’s born with it… | ||
made on an Apple in Carolina | ||
a Three Kids in a Trench Coat production | ||
a little sleight of && | ||
there’s no place like localhost | ||
my favorite book is Charlatan’s Web | ||
fact follows fiction | ||
your lovely assistant | ||
my other server’s a Kubernetes cluster | ||
you wrote it here first | ||
here lies “waiting on back-end”; REST in peace | ||
your front-end’s REST friend | ||
first past the post | ||
🎶 mockin' the casbah 🎶 | ||
you ready to mock and roll? | ||
I also do weddings and bar mocks-vahs | ||
I’m thinking about going into fake estate | ||
I bet you’re real fun at mocktail parties | ||
Eat. Sleep. Mock. Repeat. | ||
hand over the cache and no one gets hurt | ||
Storybook for the back-end | ||
you are now free to mock about the cabin | ||
close enough for government work | ||
an API in hand is worth two in the cloud | ||
where we're going, we don't need wifi | ||
paper prototyping the back-end | ||
the cloud natives are restless | ||
your front-end dev flight simulator | ||
your front-end’s sparring partner | ||
your back-end's stunt double |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
interface XmlHints { | ||
attribute?: boolean; | ||
name?: string; | ||
namespace?: string; | ||
prefix?: string; | ||
wrapped?: boolean; | ||
} | ||
|
||
interface Schema { | ||
[key: string]: unknown; | ||
items?: Schema; | ||
properties?: { | ||
[key: string]: Schema; | ||
}; | ||
xml?: XmlHints; | ||
} | ||
|
||
function xmlEscape(xmlString: string): string { | ||
// eslint-disable-next-line unicorn/prefer-string-replace-all | ||
return xmlString.replace(/["&'<>]/gu, (character: string) => { | ||
switch (character) { | ||
case "<": { | ||
return "<"; | ||
} | ||
case ">": { | ||
return ">"; | ||
} | ||
case "&": { | ||
return "&"; | ||
} | ||
case "'": { | ||
return "'"; | ||
} | ||
case '"': { | ||
return """; | ||
} | ||
default: { | ||
return character; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function objectToXml( | ||
json: object, | ||
schema: Schema | undefined, | ||
name: string, | ||
): string { | ||
const xml: string[] = []; | ||
|
||
const attributes: string[] = []; | ||
|
||
Object.entries(json).forEach(([key, value]) => { | ||
const properties = schema?.properties?.[key]; | ||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions | ||
if (properties?.attribute) { | ||
attributes.push(` ${key}="${xmlEscape(String(value))}"`); | ||
} else { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
xml.push(jsonToXml(value, properties, key)); | ||
} | ||
}); | ||
|
||
return `<${name}${attributes.join("")}>${String(xml.join(""))}</${name}>`; | ||
} | ||
|
||
export function jsonToXml( | ||
json: unknown, | ||
schema: Schema | undefined, | ||
keyName = "root", | ||
): string { | ||
const name = schema?.xml?.name ?? keyName; | ||
|
||
if (Array.isArray(json)) { | ||
const items = json | ||
.map((item) => jsonToXml(item, schema?.items, name)) | ||
.join(""); | ||
|
||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions | ||
if (schema?.xml?.wrapped) { | ||
return `<${name}>${items}</${name}>`; | ||
} | ||
|
||
return items; | ||
} | ||
|
||
if (typeof json === "object" && json !== null) { | ||
return objectToXml(json, schema, name); | ||
} | ||
|
||
return `<${name}>${String(json)}</${name}>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.