Test case
1 generated file · +195 −0test/inputs/json/priority/name-style.json
Atypescript-top-level-namedefault / TopLevel.ts+195 −0
| @@ -0,0 +1,195 @@ | ||
| 1 | +// To parse this data: | |
| 2 | +// | |
| 3 | +// import { Convert, Acme } from "./TopLevel"; | |
| 4 | +// | |
| 5 | +// const acme = Convert.toAcme(json); | |
| 6 | +// | |
| 7 | +// These functions will throw an error if the JSON doesn't | |
| 8 | +// match the expected interface, even if the JSON is valid. | |
| 9 | + | |
| 10 | +export interface Acme { | |
| 11 | + " spaces are separators too ": boolean; | |
| 12 | + JSONString: string; | |
| 13 | + PrettyEasyToo: boolean; | |
| 14 | + ZSR_HH_DEMO3_DELMATNR: boolean; | |
| 15 | + anEasyOne: number; | |
| 16 | + isMNISTLetter: boolean; | |
| 17 | + should_know_that_id_is_an_initialism: string; | |
| 18 | +} | |
| 19 | + | |
| 20 | +// Converts JSON strings to/from your types | |
| 21 | +// and asserts the results of JSON.parse at runtime | |
| 22 | +export class Convert { | |
| 23 | + public static toAcme(json: string): Acme { | |
| 24 | + return cast(JSON.parse(json), r("Acme")); | |
| 25 | + } | |
| 26 | + | |
| 27 | + public static acmeToJson(value: Acme): string { | |
| 28 | + return JSON.stringify(uncast(value, r("Acme")), null, 2); | |
| 29 | + } | |
| 30 | +} | |
| 31 | + | |
| 32 | +function invalidValue(typ: any, val: any, key: any, parent: any = ''): never { | |
| 33 | + const prettyTyp = prettyTypeName(typ); | |
| 34 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 35 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 36 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 37 | +} | |
| 38 | + | |
| 39 | +function prettyTypeName(typ: any): string { | |
| 40 | + if (Array.isArray(typ)) { | |
| 41 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 42 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 43 | + } else { | |
| 44 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 45 | + } | |
| 46 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 47 | + return typ.literal; | |
| 48 | + } else { | |
| 49 | + return typeof typ; | |
| 50 | + } | |
| 51 | +} | |
| 52 | + | |
| 53 | +function jsonToJSProps(typ: any): any { | |
| 54 | + if (typ.jsonToJS === undefined) { | |
| 55 | + const map: any = {}; | |
| 56 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 57 | + typ.jsonToJS = map; | |
| 58 | + } | |
| 59 | + return typ.jsonToJS; | |
| 60 | +} | |
| 61 | + | |
| 62 | +function jsToJSONProps(typ: any): any { | |
| 63 | + if (typ.jsToJSON === undefined) { | |
| 64 | + const map: any = {}; | |
| 65 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 66 | + typ.jsToJSON = map; | |
| 67 | + } | |
| 68 | + return typ.jsToJSON; | |
| 69 | +} | |
| 70 | + | |
| 71 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 72 | + function transformPrimitive(typ: string, val: any): any { | |
| 73 | + if (typeof typ === typeof val) return val; | |
| 74 | + return invalidValue(typ, val, key, parent); | |
| 75 | + } | |
| 76 | + | |
| 77 | + function transformUnion(typs: any[], val: any): any { | |
| 78 | + // val must validate against one typ in typs | |
| 79 | + const l = typs.length; | |
| 80 | + for (let i = 0; i < l; i++) { | |
| 81 | + const typ = typs[i]; | |
| 82 | + try { | |
| 83 | + return transform(val, typ, getProps); | |
| 84 | + } catch (_) {} | |
| 85 | + } | |
| 86 | + return invalidValue(typs, val, key, parent); | |
| 87 | + } | |
| 88 | + | |
| 89 | + function transformEnum(cases: string[], val: any): any { | |
| 90 | + if (cases.indexOf(val) !== -1) return val; | |
| 91 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 92 | + } | |
| 93 | + | |
| 94 | + function transformArray(typ: any, val: any): any { | |
| 95 | + // val must be an array with no invalid elements | |
| 96 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 97 | + return val.map(el => transform(el, typ, getProps)); | |
| 98 | + } | |
| 99 | + | |
| 100 | + function transformDate(val: any): any { | |
| 101 | + if (val === null) { | |
| 102 | + return null; | |
| 103 | + } | |
| 104 | + const d = new Date(val); | |
| 105 | + if (isNaN(d.valueOf())) { | |
| 106 | + return invalidValue(l("Date"), val, key, parent); | |
| 107 | + } | |
| 108 | + return d; | |
| 109 | + } | |
| 110 | + | |
| 111 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 112 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 113 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 114 | + } | |
| 115 | + const result: any = {}; | |
| 116 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 117 | + const prop = props[key]; | |
| 118 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 119 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 120 | + }); | |
| 121 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 122 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 123 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 124 | + } | |
| 125 | + }); | |
| 126 | + return result; | |
| 127 | + } | |
| 128 | + | |
| 129 | + if (typ === "any") return val; | |
| 130 | + if (typ === null) { | |
| 131 | + if (val === null) return val; | |
| 132 | + return invalidValue(typ, val, key, parent); | |
| 133 | + } | |
| 134 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 135 | + let ref: any = undefined; | |
| 136 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 137 | + ref = typ.ref; | |
| 138 | + typ = typeMap[typ.ref]; | |
| 139 | + } | |
| 140 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 141 | + if (typeof typ === "object") { | |
| 142 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 143 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 144 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 145 | + : invalidValue(typ, val, key, parent); | |
| 146 | + } | |
| 147 | + // Numbers can be parsed by Date but shouldn't be. | |
| 148 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 149 | + return transformPrimitive(typ, val); | |
| 150 | +} | |
| 151 | + | |
| 152 | +function cast<T>(val: any, typ: any): T { | |
| 153 | + return transform(val, typ, jsonToJSProps); | |
| 154 | +} | |
| 155 | + | |
| 156 | +function uncast<T>(val: T, typ: any): any { | |
| 157 | + return transform(val, typ, jsToJSONProps); | |
| 158 | +} | |
| 159 | + | |
| 160 | +function l(typ: any) { | |
| 161 | + return { literal: typ }; | |
| 162 | +} | |
| 163 | + | |
| 164 | +function a(typ: any) { | |
| 165 | + return { arrayItems: typ }; | |
| 166 | +} | |
| 167 | + | |
| 168 | +function u(...typs: any[]) { | |
| 169 | + return { unionMembers: typs }; | |
| 170 | +} | |
| 171 | + | |
| 172 | +function o(props: any[], additional: any) { | |
| 173 | + return { props, additional }; | |
| 174 | +} | |
| 175 | + | |
| 176 | +function m(additional: any) { | |
| 177 | + const props: any[] = []; | |
| 178 | + return { props, additional }; | |
| 179 | +} | |
| 180 | + | |
| 181 | +function r(name: string) { | |
| 182 | + return { ref: name }; | |
| 183 | +} | |
| 184 | + | |
| 185 | +const typeMap: any = { | |
| 186 | + "Acme": o([ | |
| 187 | + { json: " spaces are separators too ", js: " spaces are separators too ", typ: true }, | |
| 188 | + { json: "JSONString", js: "JSONString", typ: "" }, | |
| 189 | + { json: "PrettyEasyToo", js: "PrettyEasyToo", typ: true }, | |
| 190 | + { json: "ZSR_HH_DEMO3_DELMATNR", js: "ZSR_HH_DEMO3_DELMATNR", typ: true }, | |
| 191 | + { json: "anEasyOne", js: "anEasyOne", typ: 0 }, | |
| 192 | + { json: "isMNISTLetter", js: "isMNISTLetter", typ: true }, | |
| 193 | + { json: "should_know_that_id_is_an_initialism", js: "should_know_that_id_is_an_initialism", typ: "" }, | |
| 194 | + ], false), | |
| 195 | +}; |
No generated files match these filters.