-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(samples): move example generation code to samples plugin (#8727
) Refs #8577
- Loading branch information
Showing
15 changed files
with
360 additions
and
160 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...e/plugins/json-schema-2020-12/fn/index.js → src/core/plugins/json-schema-2020-12/fn.js
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
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
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
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,32 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import some from "lodash/some" | ||
|
||
const shouldStringifyTypesConfig = [ | ||
{ | ||
when: /json/, | ||
shouldStringifyTypes: ["string"], | ||
}, | ||
] | ||
const defaultStringifyTypes = ["object"] | ||
const makeGetJsonSampleSchema = | ||
(getSystem) => (schema, config, contentType, exampleOverride) => { | ||
const { fn } = getSystem() | ||
const res = fn.memoizedSampleFromSchema(schema, config, exampleOverride) | ||
const resType = typeof res | ||
|
||
const typesToStringify = shouldStringifyTypesConfig.reduce( | ||
(types, nextConfig) => | ||
nextConfig.when.test(contentType) | ||
? [...types, ...nextConfig.shouldStringifyTypes] | ||
: types, | ||
defaultStringifyTypes | ||
) | ||
|
||
return some(typesToStringify, (x) => x === resType) | ||
? JSON.stringify(res, null, 2) | ||
: res | ||
} | ||
|
||
export default makeGetJsonSampleSchema |
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,30 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
const makeGetSampleSchema = | ||
(getSystem) => | ||
(schema, contentType = "", config = {}, exampleOverride = undefined) => { | ||
const { fn } = getSystem() | ||
|
||
if (typeof schema?.toJS === "function") { | ||
schema = schema.toJS() | ||
} | ||
if (typeof exampleOverride?.toJS === "function") { | ||
exampleOverride = exampleOverride.toJS() | ||
} | ||
|
||
if (/xml/.test(contentType)) { | ||
return fn.getXmlSampleSchema(schema, config, exampleOverride) | ||
} | ||
if (/(yaml|yml)/.test(contentType)) { | ||
return fn.getYamlSampleSchema( | ||
schema, | ||
config, | ||
contentType, | ||
exampleOverride | ||
) | ||
} | ||
return fn.getJsonSampleSchema(schema, config, contentType, exampleOverride) | ||
} | ||
|
||
export default makeGetSampleSchema |
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,31 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
const makeGetXmlSampleSchema = | ||
(getSystem) => (schema, config, exampleOverride) => { | ||
const { fn } = getSystem() | ||
|
||
if (schema && !schema.xml) { | ||
schema.xml = {} | ||
} | ||
if (schema && !schema.xml.name) { | ||
if ( | ||
!schema.$$ref && | ||
(schema.type || | ||
schema.items || | ||
schema.properties || | ||
schema.additionalProperties) | ||
) { | ||
// eslint-disable-next-line quotes | ||
return '<?xml version="1.0" encoding="UTF-8"?>\n<!-- XML example cannot be generated; root element name is undefined -->' | ||
} | ||
if (schema.$$ref) { | ||
let match = schema.$$ref.match(/\S*\/(\S+)$/) | ||
schema.xml.name = match[1] | ||
} | ||
} | ||
|
||
return fn.memoizedCreateXMLExample(schema, config, exampleOverride) | ||
} | ||
|
||
export default makeGetXmlSampleSchema |
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,34 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import YAML, { JSON_SCHEMA } from "js-yaml" | ||
|
||
const makeGetYamlSampleSchema = | ||
(getSystem) => (schema, config, contentType, exampleOverride) => { | ||
const { fn } = getSystem() | ||
const jsonExample = fn.getJsonSampleSchema( | ||
schema, | ||
config, | ||
contentType, | ||
exampleOverride | ||
) | ||
let yamlString | ||
try { | ||
yamlString = YAML.dump( | ||
YAML.load(jsonExample), | ||
{ | ||
lineWidth: -1, // don't generate line folds | ||
}, | ||
{ schema: JSON_SCHEMA } | ||
) | ||
if (yamlString[yamlString.length - 1] === "\n") { | ||
yamlString = yamlString.slice(0, yamlString.length - 1) | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
return "error: could not generate yaml example" | ||
} | ||
return yamlString.replace(/\t/g, " ") | ||
} | ||
|
||
export default makeGetYamlSampleSchema |
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 |
---|---|---|
@@ -1,5 +1,32 @@ | ||
import * as fn from "./fn" | ||
/** | ||
* @prettier | ||
*/ | ||
import { | ||
sampleFromSchema, | ||
inferSchema, | ||
sampleFromSchemaGeneric, | ||
createXMLExample, | ||
memoizedCreateXMLExample, | ||
memoizedSampleFromSchema, | ||
} from "./fn/index" | ||
import makeGetJsonSampleSchema from "./fn/get-json-sample-schema" | ||
import makeGetYamlSampleSchema from "./fn/get-yaml-sample-schema" | ||
import makeGetXmlSampleSchema from "./fn/get-xml-sample-schema" | ||
import makeGetSampleSchema from "./fn/get-sample-schema" | ||
|
||
export default function () { | ||
return { fn } | ||
} | ||
const SamplesPlugin = ({ getSystem }) => ({ | ||
fn: { | ||
inferSchema, | ||
sampleFromSchema, | ||
sampleFromSchemaGeneric, | ||
createXMLExample, | ||
memoizedSampleFromSchema, | ||
memoizedCreateXMLExample, | ||
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem), | ||
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem), | ||
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem), | ||
getSampleSchema: makeGetSampleSchema(getSystem), | ||
}, | ||
}) | ||
|
||
export default SamplesPlugin |
Oops, something went wrong.