apollo-conn-gen 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/oas/io/writer.d.ts +1 -1
- package/dist/oas/io/writer.js +8 -43
- package/dist/oas/io/writer.js.map +1 -1
- package/dist/oas/nodes/comp.d.ts +1 -1
- package/dist/oas/nodes/comp.js +7 -5
- package/dist/oas/nodes/comp.js.map +1 -1
- package/dist/oas/nodes/factory.js +21 -3
- package/dist/oas/nodes/factory.js.map +1 -1
- package/dist/oas/nodes/get.js +3 -1
- package/dist/oas/nodes/get.js.map +1 -1
- package/dist/oas/nodes/internal.d.ts +1 -0
- package/dist/oas/nodes/internal.js +1 -0
- package/dist/oas/nodes/internal.js.map +1 -1
- package/dist/oas/nodes/propArray.d.ts +1 -1
- package/dist/oas/nodes/propArray.js +12 -4
- package/dist/oas/nodes/propArray.js.map +1 -1
- package/dist/oas/nodes/propComp.d.ts +16 -0
- package/dist/oas/nodes/propComp.js +73 -0
- package/dist/oas/nodes/propComp.js.map +1 -0
- package/dist/oas/nodes/propObj.js +4 -0
- package/dist/oas/nodes/propObj.js.map +1 -1
- package/dist/oas/nodes/propScalar.js +3 -0
- package/dist/oas/nodes/propScalar.js.map +1 -1
- package/dist/oas/nodes/typeUtils.d.ts +1 -0
- package/dist/oas/nodes/typeUtils.js +6 -1
- package/dist/oas/nodes/typeUtils.js.map +1 -1
- package/dist/oas/nodes/union.d.ts +4 -1
- package/dist/oas/nodes/union.js +111 -56
- package/dist/oas/nodes/union.js.map +1 -1
- package/dist/oas/oasContext.d.ts +6 -1
- package/dist/oas/oasContext.js +10 -4
- package/dist/oas/oasContext.js.map +1 -1
- package/dist/tests/runners.js +3 -2
- package/dist/tests/runners.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal.js","sourceRoot":"","sources":["../../../src/oas/nodes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O,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,CAAC,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["// -- internal.js --\nimport { IType } from './iType.js';\nimport { Type } from './type.js';\nimport { Prop } from './prop.js';\nimport { Param } from './param.js';\nimport { PropArray } from './propArray.js';\nimport { PropObj } from './propObj.js';\nimport { PropComp } from './propComp.js';\nimport { PropRef } from './propRef.js';\nimport { PropScalar } from './propScalar.js';\nimport { ReferenceObject } from './referenceObject.js';\nimport { Arr } from './arr.js';\nimport { CircularRef } from './circularRef.js';\nimport { Composed } from './comp.js';\nimport { En } from './en.js';\nimport { Factory } from './factory.js';\nimport { Get } from './get.js';\nimport { Post } from './post.js';\nimport { Put } from './put.js';\nimport { Patch } from './patch.js';\nimport { Delete } from './delete.js';\nimport { Body } from './body.js';\nimport { Obj } from './obj.js';\nimport { Ref } from './ref.js';\nimport { Response } from './response.js';\nimport { Scalar } from './scalar.js';\nimport { Union } from './union.js';\nimport { T } from './typeUtils.js';\nimport { Op } from './op.js';\n\nexport * from './iType.js';\nexport { Type } from './type.js';\nexport { type ReferenceObject } from './referenceObject.js';\nexport { Prop } from './prop.js';\nexport { Param } from './param.js';\nexport { Arr } from './arr.js';\nexport { CircularRef } from './circularRef.js';\nexport { Composed } from './comp.js';\nexport { En } from './en.js';\nexport { Factory } from './factory.js';\nexport { Get } from './get.js';\nexport { Post } from './post.js';\nexport { Put } from './put.js';\nexport { Patch } from './patch.js';\nexport { Delete } from './delete.js';\nexport { Body } from './body.js';\nexport { Obj } from './obj.js';\nexport { Ref } from './ref.js';\nexport { Response } from './response.js';\nexport { Scalar } from './scalar.js';\nexport { Union } from './union.js';\nexport { PropArray } from './propArray.js';\nexport { PropObj } from './propObj.js';\nexport { PropComp } from './propComp.js';\nexport { PropRef } from './propRef.js';\nexport { PropScalar } from './propScalar.js';\nexport { T } from './typeUtils.js';\nexport { type Op } from './op.js';\n"]}
|
|
@@ -10,5 +10,5 @@ export declare class PropArray extends Prop {
|
|
|
10
10
|
add(child: IType): void;
|
|
11
11
|
forPrompt(context: OasContext): string;
|
|
12
12
|
select(context: OasContext, writer: Writer, selection: string[]): void;
|
|
13
|
-
needsBrackets(child
|
|
13
|
+
needsBrackets(child?: IType): boolean;
|
|
14
14
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Factory, Prop, PropObj, PropRef } from './internal.js';
|
|
1
|
+
import { Factory, Prop, PropComp, PropObj, PropRef } from './internal.js';
|
|
2
2
|
import { trace } from '../log/trace.js';
|
|
3
3
|
import { Naming } from '../utils/naming.js';
|
|
4
4
|
export class PropArray extends Prop {
|
|
@@ -55,18 +55,26 @@ export class PropArray extends Prop {
|
|
|
55
55
|
context.enter(this);
|
|
56
56
|
}
|
|
57
57
|
// Select each child of the items Prop.
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
if (this.needsBrackets(this.items)) {
|
|
59
|
+
for (const child of this.items.children) {
|
|
60
|
+
child.select(context, writer, selection);
|
|
61
|
+
}
|
|
60
62
|
}
|
|
61
63
|
if (this.needsBrackets(this.items)) {
|
|
62
64
|
context.leave(this);
|
|
63
65
|
writer.append(' '.repeat(context.indent + context.stack.length)).append('}');
|
|
64
66
|
}
|
|
67
|
+
// writer.append('\n');
|
|
68
|
+
if (context.generateOptions.debugParentInSelection) {
|
|
69
|
+
writer.append(' # ').append(Naming.getRefName(this.parent.name));
|
|
70
|
+
}
|
|
65
71
|
writer.append('\n');
|
|
66
72
|
trace(context, '<- [prop:array:select]', 'out');
|
|
67
73
|
}
|
|
68
74
|
needsBrackets(child) {
|
|
69
|
-
|
|
75
|
+
if (!child)
|
|
76
|
+
return false;
|
|
77
|
+
return child instanceof PropRef || child instanceof PropObj || child instanceof PropComp;
|
|
70
78
|
}
|
|
71
79
|
}
|
|
72
80
|
//# sourceMappingURL=propArray.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"propArray.js","sourceRoot":"","sources":["../../../src/oas/nodes/propArray.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAS,IAAI,EAAE,
|
|
1
|
+
{"version":3,"file":"propArray.js","sourceRoot":"","sources":["../../../src/oas/nodes/propArray.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAS,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAU,MAAM,eAAe,CAAC;AACzF,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,MAAM,OAAO,SAAU,SAAQ,IAAI;IAC1B,KAAK,CAAQ;IAEpB,IAAI,EAAE;QACJ,OAAO,eAAe,IAAI,CAAC,IAAI,EAAE,CAAC;IACpC,CAAC;IAEe,KAAK,CAAC,OAAmB;QACvC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO;QACT,CAAC;QAED,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACpB,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE,IAAI,CAAC,CAAC;QAE9C,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;QAC/D,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QAEpB,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE,KAAK,CAAC,CAAC;QAC/C,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAEM,QAAQ,CAAC,KAAW;QACzB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,IAAI,CAAC,GAAG,CAAC,KA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C;QACpE,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEpB,KAAK,CAAC,OAAO,EAAE,wBAAwB,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;IAEM,aAAa,CAAC,KAAa;QAChC,IAAI,CAAC,KAAK;YAAE,OAAO,KAAK,CAAC;QACzB,OAAO,KAAK,YAAY,OAAO,IAAI,KAAK,YAAY,OAAO,IAAI,KAAK,YAAY,QAAQ,CAAC;IAC3F,CAAC;CACF","sourcesContent":["import { Factory, IType, Prop, PropComp, PropObj, PropRef, Scalar } from './internal.js';\nimport { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\n\nexport class PropArray extends Prop {\n public items?: Prop;\n\n get id(): string {\n return `prop:array:#${this.name}`;\n }\n\n public override visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n trace(context, '-> [prop-array:visit]', 'in');\n\n trace(context, ' [prop-array:visit]', 'type: ' + this.items);\n this.items?.visit(context);\n this.visited = true;\n\n trace(context, '<- [prop:array:visit]', 'out');\n context.leave(this);\n }\n\n public setItems(items: Prop): void {\n this.items = items;\n if (!this.children.includes(items)) {\n this.add(items);\n }\n }\n\n public override getValue(context: OasContext): string {\n return `[${this.items!.getValue(context)}]`;\n }\n\n public add(child: IType): void {\n const paths: IType[] = this.ancestors();\n const contains: boolean = paths.map((p) => p.id).includes(child.id);\n\n trace(null, '-> [prop-array:add]', 'contains child? ' + contains);\n\n if (contains) {\n const ancestor: IType = paths[paths.map((p) => p.id).indexOf(child.id)];\n const wrapper: IType = Factory.fromCircularRef(this, ancestor);\n super.add(wrapper);\n this.visited = true;\n } else {\n super.add(child);\n }\n }\n\n public forPrompt(context: OasContext): string {\n return `${this.name}: [${this.items!.getValue(context)}]`;\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, '-> [prop-array:select]', 'in: ' + this.name);\n\n const fieldName = this.name;\n const sanitised = Naming.sanitiseFieldForSelect(fieldName);\n writer.append(' '.repeat(context.indent + context.stack.length)).append(sanitised);\n\n if (this.needsBrackets(this.items!)) {\n writer.append(' {');\n writer.append('\\n');\n context.enter(this);\n }\n\n // Select each child of the items Prop.\n if (this.needsBrackets(this.items)) {\n for (const child of this.items!.children) {\n child.select(context, writer, selection);\n }\n }\n\n if (this.needsBrackets(this.items!)) {\n context.leave(this);\n writer.append(' '.repeat(context.indent + context.stack.length)).append('}');\n }\n // writer.append('\\n');\n if (context.generateOptions.debugParentInSelection) {\n writer.append(' # ').append(Naming.getRefName(this.parent!.name));\n }\n\n writer.append('\\n');\n\n trace(context, '<- [prop:array:select]', 'out');\n }\n\n public needsBrackets(child?: IType): boolean {\n if (!child) return false;\n return child instanceof PropRef || child instanceof PropObj || child instanceof PropComp;\n }\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IType } from './internal.js';
|
|
2
|
+
import { Prop } from './prop.js';
|
|
3
|
+
import { SchemaObject } from 'oas/types';
|
|
4
|
+
import { OasContext } from '../oasContext.js';
|
|
5
|
+
import { Writer } from '../io/writer.js';
|
|
6
|
+
export declare class PropComp extends Prop {
|
|
7
|
+
schema: SchemaObject;
|
|
8
|
+
comp?: IType;
|
|
9
|
+
constructor(parent: IType, name: string, schema: SchemaObject);
|
|
10
|
+
forPrompt(_context: OasContext): string;
|
|
11
|
+
get id(): string;
|
|
12
|
+
visit(context: OasContext): void;
|
|
13
|
+
getValue(_context: OasContext): string;
|
|
14
|
+
select(context: OasContext, writer: Writer, selection: string[]): void;
|
|
15
|
+
private needsBrackets;
|
|
16
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Obj, Union } from './internal.js';
|
|
2
|
+
import { Prop } from './prop.js';
|
|
3
|
+
import _ from 'lodash';
|
|
4
|
+
import { trace } from '../log/trace.js';
|
|
5
|
+
import { Composed } from './comp.js';
|
|
6
|
+
import { Naming } from '../utils/naming.js';
|
|
7
|
+
export class PropComp extends Prop {
|
|
8
|
+
schema;
|
|
9
|
+
comp;
|
|
10
|
+
constructor(parent, name, schema) {
|
|
11
|
+
super(parent, name, schema);
|
|
12
|
+
this.schema = schema;
|
|
13
|
+
}
|
|
14
|
+
forPrompt(_context) {
|
|
15
|
+
const type = this.schema.oneOf ? 'One Of' : this.schema.allOf ? 'All Of' : 'Unknown';
|
|
16
|
+
return _.lowerFirst(this.name) + ': ' + Naming.getRefName(this.comp.name) + ` (${type})`;
|
|
17
|
+
}
|
|
18
|
+
get id() {
|
|
19
|
+
return 'prop:comp:' + this.name;
|
|
20
|
+
}
|
|
21
|
+
visit(context) {
|
|
22
|
+
if (this.visited) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
context.enter(this);
|
|
26
|
+
const comp = this.comp;
|
|
27
|
+
trace(context, '-> [prop-comp:visit]', 'in ' + this.name + ', obj: ' + comp.name);
|
|
28
|
+
comp.visit(context);
|
|
29
|
+
if (!this.children.includes(comp)) {
|
|
30
|
+
this.add(comp);
|
|
31
|
+
}
|
|
32
|
+
this.visited = true;
|
|
33
|
+
trace(context, '<- [prop-comp:visit]', 'out ' + this.name + ', obj: ' + comp.name);
|
|
34
|
+
context.leave(this);
|
|
35
|
+
}
|
|
36
|
+
getValue(_context) {
|
|
37
|
+
// we'll make an assumption here: that if the child obj has no properties,
|
|
38
|
+
// then it's a free-form JSON payload. not sure if the right one, but it will
|
|
39
|
+
// compose for now.
|
|
40
|
+
if (_.isEmpty(this.comp?.props))
|
|
41
|
+
return 'JSON';
|
|
42
|
+
// return Naming.genTypeName(this.name);
|
|
43
|
+
return Naming.genTypeName(this.comp.name) + this.comp.nameSuffix();
|
|
44
|
+
}
|
|
45
|
+
select(context, writer, selection) {
|
|
46
|
+
const comp = this.comp;
|
|
47
|
+
trace(context, '-> [prop-comp:select]', 'in ' + this.name + ', obj: ' + comp.name);
|
|
48
|
+
const fieldName = this.name;
|
|
49
|
+
const sanitised = Naming.sanitiseFieldForSelect(fieldName);
|
|
50
|
+
writer.append(' '.repeat(context.indent + context.stack.length)).append(sanitised);
|
|
51
|
+
if (this.needsBrackets(comp)) {
|
|
52
|
+
writer.append(' {').append('\n');
|
|
53
|
+
context.enter(this);
|
|
54
|
+
}
|
|
55
|
+
for (const child of this.children) {
|
|
56
|
+
child.select(context, writer, selection);
|
|
57
|
+
}
|
|
58
|
+
if (this.needsBrackets(comp)) {
|
|
59
|
+
context.leave(this);
|
|
60
|
+
writer.append(' '.repeat(context.indent + context.stack.length)).append('}');
|
|
61
|
+
}
|
|
62
|
+
// writer.append('\n');
|
|
63
|
+
if (context.generateOptions.debugParentInSelection) {
|
|
64
|
+
writer.append(' # ').append(Naming.getRefName(this.parent.name));
|
|
65
|
+
}
|
|
66
|
+
writer.append('\n');
|
|
67
|
+
trace(context, '<- [prop-comp:select]', 'out ' + this.name + ', obj: ' + comp?.name);
|
|
68
|
+
}
|
|
69
|
+
needsBrackets(child) {
|
|
70
|
+
return (child instanceof Obj || child instanceof Union || child instanceof Composed) && !_.isEmpty(child.props);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=propComp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"propComp.js","sourceRoot":"","sources":["../../../src/oas/nodes/propComp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,GAAG,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAGrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,MAAM,OAAO,QAAS,SAAQ,IAAI;IAMvB;IALF,IAAI,CAAS;IAEpB,YACE,MAAa,EACb,IAAY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C,CAAC;QAED,IAAI,IAAI,CAAC,aAAa,CAAC,IAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACpB,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC/E,CAAC;QACD,uBAAuB;QACvB,IAAI,OAAO,CAAC,eAAe,CAAC,sBAAsB,EAAE,CAAC;YACnD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,MAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACpE,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEpB,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE,MAAM,GAAG,IAAI,CAAC,IAAI,GAAG,SAAS,GAAG,IAAI,EAAE,IAAI,CAAC,CAAC;IACvF,CAAC;IAEO,aAAa,CAAC,KAAY;QAChC,OAAO,CAAC,KAAK,YAAY,GAAG,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,YAAY,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAClH,CAAC;CACF","sourcesContent":["import { IType, Obj, Union } from './internal.js';\nimport { Prop } from './prop.js';\nimport _ from 'lodash';\nimport { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { Composed } from './comp.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\n\nexport class PropComp extends Prop {\n public comp?: IType;\n\n constructor(\n parent: IType,\n name: string,\n public schema: SchemaObject,\n ) {\n super(parent, name, schema);\n }\n\n public forPrompt(_context: OasContext): string {\n const type: string = this.schema.oneOf ? 'One Of' : this.schema.allOf ? 'All Of' : 'Unknown';\n return _.lowerFirst(this.name) + ': ' + Naming.getRefName(this.comp!.name) + ` (${type})`;\n }\n\n get id(): string {\n return 'prop:comp:' + this.name;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n const comp = this.comp!;\n trace(context, '-> [prop-comp:visit]', 'in ' + this.name + ', obj: ' + comp.name);\n\n comp.visit(context);\n if (!this.children.includes(comp)) {\n this.add(comp);\n }\n this.visited = true;\n\n trace(context, '<- [prop-comp:visit]', 'out ' + this.name + ', obj: ' + comp.name);\n context.leave(this);\n }\n\n public getValue(_context: OasContext): string {\n // we'll make an assumption here: that if the child obj has no properties,\n // then it's a free-form JSON payload. not sure if the right one, but it will\n // compose for now.\n if (_.isEmpty(this.comp?.props)) return 'JSON';\n\n // return Naming.genTypeName(this.name);\n return Naming.genTypeName(this.comp!.name!) + (this.comp as Composed).nameSuffix();\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n const comp = this.comp!;\n trace(context, '-> [prop-comp:select]', 'in ' + this.name + ', obj: ' + comp.name);\n\n const fieldName = this.name;\n const sanitised = Naming.sanitiseFieldForSelect(fieldName);\n\n writer.append(' '.repeat(context.indent + context.stack.length)).append(sanitised);\n\n if (this.needsBrackets(comp!)) {\n writer.append(' {').append('\\n');\n context.enter(this);\n }\n\n for (const child of this.children) {\n child.select(context, writer, selection);\n }\n\n if (this.needsBrackets(comp!)) {\n context.leave(this);\n writer.append(' '.repeat(context.indent + context.stack.length)).append('}');\n }\n // writer.append('\\n');\n if (context.generateOptions.debugParentInSelection) {\n writer.append(' # ').append(Naming.getRefName(this.parent!.name));\n }\n\n writer.append('\\n');\n\n trace(context, '<- [prop-comp:select]', 'out ' + this.name + ', obj: ' + comp?.name);\n }\n\n private needsBrackets(child: IType): boolean {\n return (child instanceof Obj || child instanceof Union || child instanceof Composed) && !_.isEmpty(child.props);\n }\n}\n"]}
|
|
@@ -63,6 +63,10 @@ export class PropObj extends Prop {
|
|
|
63
63
|
context.leave(this);
|
|
64
64
|
writer.append(' '.repeat(context.indent + context.stack.length)).append('}');
|
|
65
65
|
}
|
|
66
|
+
// writer.append('\n');
|
|
67
|
+
if (context.generateOptions.debugParentInSelection) {
|
|
68
|
+
writer.append(' # ').append(Naming.getRefName(this.parent.name));
|
|
69
|
+
}
|
|
66
70
|
writer.append('\n');
|
|
67
71
|
trace(context, '<- [prop-obj:select]', 'out ' + this.name + ', obj: ' + this.obj?.name);
|
|
68
72
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"propObj.js","sourceRoot":"","sources":["../../../src/oas/nodes/propObj.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,GAAG,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAGrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,MAAM,OAAO,OAAQ,SAAQ,IAAI;IAItB;IACA;IAJT,YACE,MAAa,EACb,IAAY,EACL,MAAoB,EACpB,GAAU;QAEjB,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QAHrB,WAAM,GAAN,MAAM,CAAc;QACpB,QAAG,GAAH,GAAG,CAAO;QAGjB,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,CAAC;QAED,+CAA+C;QAC/C,IAAI,GAAG,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;YACxB,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;QACpB,CAAC;IACH,CAAC;IAEM,SAAS,CAAC,QAAoB;QACnC,OAAO,CAAC,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I,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAC1F,CAAC;IAEO,aAAa,CAAC,KAAY;QAChC,OAAO,CAAC,KAAK,YAAY,GAAG,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,YAAY,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAClH,CAAC;CACF","sourcesContent":["import { IType, Obj, Union } from './internal.js';\nimport { Prop } from './internal.js';\nimport _ from 'lodash';\nimport { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { Composed } from './comp.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\n\nexport class PropObj extends Prop {\n constructor(\n parent: IType,\n name: string,\n public schema: SchemaObject,\n public obj: IType,\n ) {\n super(parent, name, schema);\n if (!obj) {\n throw new Error('obj parameter is required');\n }\n\n // TODO: check if re-parenting is necessary?!?!\n if (obj.parent !== this) {\n obj.parent = this;\n }\n }\n\n public forPrompt(_context: OasContext): string {\n return _.lowerFirst(this.name) + ': ' + Naming.getRefName(this.obj.name) + ' (Obj)';\n }\n\n get id(): string {\n return 'prop:obj:' + this.name;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n trace(context, '-> [prop-obj:visit]', 'in ' + this.name + ', obj: ' + this.obj.name);\n\n this.obj.visit(context);\n if (!this.children.includes(this.obj)) {\n this.add(this.obj);\n }\n this.visited = true;\n\n trace(context, '<- [prop-obj:visit]', 'out ' + this.name + ', obj: ' + this.obj.name);\n context.leave(this);\n }\n\n public getValue(context: OasContext): string {\n // we'll make an assumption here: that if the child obj has no properties,\n // then it's a free-form JSON payload. not sure if the right one, but it will\n // compose for now.\n if (_.isEmpty(this.obj?.props)) return 'JSON';\n\n return Naming.genTypeName(this.name);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, '-> [prop-obj:select]', 'in ' + this.name + ', obj: ' + this.obj.name);\n\n const fieldName = this.name;\n const sanitised = Naming.sanitiseFieldForSelect(fieldName);\n\n writer.append(' '.repeat(context.indent + context.stack.length)).append(sanitised);\n\n if (this.needsBrackets(this.obj!)) {\n writer.append(' {').append('\\n');\n context.enter(this);\n }\n\n for (const child of this.children) {\n child.select(context, writer, selection);\n }\n\n if (this.needsBrackets(this.obj!)) {\n context.leave(this);\n writer.append(' '.repeat(context.indent + context.stack.length)).append('}');\n }\n writer.append('\\n');\n\n trace(context, '<- [prop-obj:select]', 'out ' + this.name + ', obj: ' + this.obj?.name);\n }\n\n private needsBrackets(child: IType): boolean {\n return (child instanceof Obj || child instanceof Union || child instanceof Composed) && !_.isEmpty(child.props);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"propObj.js","sourceRoot":"","sources":["../../../src/oas/nodes/propObj.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,GAAG,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAGrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,MAAM,OAAO,OAAQ,SAAQ,IAAI;IAItB;IACA;IAJT,YACE,MAAa,EACb,IAAY,EACL,MAAoB,EACpB,GAAU;QAEjB,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QAHrB,WAAM,GAAN,MAAM,CAAc;QACpB,QAAG,GAAH,GAAG,CAAO;QAGjB,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,CAAC;QAED,+CAA+C;QAC/C,IAAI,GAAG,CAAC,MAAM,KAAK,IAAI,EA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AAO,CAAC,eAAe,CAAC,sBAAsB,EAAE,CAAC;YACnD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,MAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACpE,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEpB,KAAK,CAAC,OAAO,EAAE,sBAAsB,EAAE,MAAM,GAAG,IAAI,CAAC,IAAI,GAAG,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAC1F,CAAC;IAEO,aAAa,CAAC,KAAY;QAChC,OAAO,CAAC,KAAK,YAAY,GAAG,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,YAAY,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAClH,CAAC;CACF","sourcesContent":["import { IType, Obj, Union } from './internal.js';\nimport { Prop } from './internal.js';\nimport _ from 'lodash';\nimport { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { Composed } from './comp.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\n\nexport class PropObj extends Prop {\n constructor(\n parent: IType,\n name: string,\n public schema: SchemaObject,\n public obj: IType,\n ) {\n super(parent, name, schema);\n if (!obj) {\n throw new Error('obj parameter is required');\n }\n\n // TODO: check if re-parenting is necessary?!?!\n if (obj.parent !== this) {\n obj.parent = this;\n }\n }\n\n public forPrompt(_context: OasContext): string {\n return _.lowerFirst(this.name) + ': ' + Naming.getRefName(this.obj.name) + ' (Obj)';\n }\n\n get id(): string {\n return 'prop:obj:' + this.name;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n trace(context, '-> [prop-obj:visit]', 'in ' + this.name + ', obj: ' + this.obj.name);\n\n this.obj.visit(context);\n if (!this.children.includes(this.obj)) {\n this.add(this.obj);\n }\n this.visited = true;\n\n trace(context, '<- [prop-obj:visit]', 'out ' + this.name + ', obj: ' + this.obj.name);\n context.leave(this);\n }\n\n public getValue(context: OasContext): string {\n // we'll make an assumption here: that if the child obj has no properties,\n // then it's a free-form JSON payload. not sure if the right one, but it will\n // compose for now.\n if (_.isEmpty(this.obj?.props)) return 'JSON';\n\n return Naming.genTypeName(this.name);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, '-> [prop-obj:select]', 'in ' + this.name + ', obj: ' + this.obj.name);\n\n const fieldName = this.name;\n const sanitised = Naming.sanitiseFieldForSelect(fieldName);\n\n writer.append(' '.repeat(context.indent + context.stack.length)).append(sanitised);\n\n if (this.needsBrackets(this.obj!)) {\n writer.append(' {').append('\\n');\n context.enter(this);\n }\n\n for (const child of this.children) {\n child.select(context, writer, selection);\n }\n\n if (this.needsBrackets(this.obj!)) {\n context.leave(this);\n writer.append(' '.repeat(context.indent + context.stack.length)).append('}');\n }\n // writer.append('\\n');\n if (context.generateOptions.debugParentInSelection) {\n writer.append(' # ').append(Naming.getRefName(this.parent!.name));\n }\n\n writer.append('\\n');\n\n trace(context, '<- [prop-obj:select]', 'out ' + this.name + ', obj: ' + this.obj?.name);\n }\n\n private needsBrackets(child: IType): boolean {\n return (child instanceof Obj || child instanceof Union || child instanceof Composed) && !_.isEmpty(child.props);\n }\n}\n"]}
|
|
@@ -39,6 +39,9 @@ export class PropScalar extends Prop {
|
|
|
39
39
|
for (const child of this.children) {
|
|
40
40
|
child.select(context, writer, selection);
|
|
41
41
|
}
|
|
42
|
+
if (context.generateOptions.debugParentInSelection) {
|
|
43
|
+
writer.append(' # ').append(Naming.getRefName(this.parent.name));
|
|
44
|
+
}
|
|
42
45
|
writer.append('\n');
|
|
43
46
|
}
|
|
44
47
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"propScalar.js","sourceRoot":"","sources":["../../../src/oas/nodes/propScalar.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BAAkB,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9C,MAAM,SAAS,GAAG,MAAM,CAAC,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3D,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAEnF,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;QAC3C,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;CACF","sourcesContent":["import { Factory, IType } from './internal.js';\nimport { Prop } from './internal.js';\nimport { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\n\nexport class PropScalar extends Prop {\n private propType?: IType;\n\n constructor(\n parent: IType,\n name: string,\n public type: string,\n public schema: SchemaObject,\n ) {\n super(parent, name, schema);\n }\n\n get id(): string {\n return `prop:scalar:${this.name}`;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n if (!this.propType) {\n this.propType = Factory.fromSchema(this, this.schema);\n // this.propType.visit(context);\n this.visited = true;\n }\n context.leave(this);\n }\n\n public getValue(context: OasContext): string {\n return this.type;\n }\n\n public forPrompt(context: OasContext): string {\n return `${this.name}: ${this.type}`;\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, ' [prop:select]', this.name);\n const sanitised = Naming.sanitiseFieldForSelect(this.name);\n writer.append(' '.repeat(context.indent + context.stack.length)).append(sanitised);\n\n for (const child of this.children) {\n child.select(context, writer, selection);\n }\n\n writer.append('\\n');\n }\n}\n"]}
|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AAC,OAAmB,EAAE,MAAc,EAAE,SAAmB;QACpE,KAAK,CAAC,OAAO,EAAE,kBAAkB,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9C,MAAM,SAAS,GAAG,MAAM,CAAC,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3D,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAEnF,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;QAC3C,CAAC;QAED,IAAI,OAAO,CAAC,eAAe,CAAC,sBAAsB,EAAE,CAAC;YACnD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,MAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACpE,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;CACF","sourcesContent":["import { Factory, IType } from './internal.js';\nimport { Prop } from './internal.js';\nimport { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\n\nexport class PropScalar extends Prop {\n private propType?: IType;\n\n constructor(\n parent: IType,\n name: string,\n public type: string,\n public schema: SchemaObject,\n ) {\n super(parent, name, schema);\n }\n\n get id(): string {\n return `prop:scalar:${this.name}`;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n context.enter(this);\n if (!this.propType) {\n this.propType = Factory.fromSchema(this, this.schema);\n // this.propType.visit(context);\n this.visited = true;\n }\n context.leave(this);\n }\n\n public getValue(context: OasContext): string {\n return this.type;\n }\n\n public forPrompt(context: OasContext): string {\n return `${this.name}: ${this.type}`;\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]) {\n trace(context, ' [prop:select]', this.name);\n const sanitised = Naming.sanitiseFieldForSelect(this.name);\n writer.append(' '.repeat(context.indent + context.stack.length)).append(sanitised);\n\n for (const child of this.children) {\n child.select(context, writer, selection);\n }\n\n if (context.generateOptions.debugParentInSelection) {\n writer.append(' # ').append(Naming.getRefName(this.parent!.name));\n }\n\n writer.append('\\n');\n }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CircularRef, En, Obj, PropArray, PropScalar, Scalar } from './internal.js';
|
|
1
|
+
import { CircularRef, En, Obj, Prop, PropArray, PropScalar, Scalar } from './internal.js';
|
|
2
2
|
import _ from 'lodash';
|
|
3
3
|
export class T {
|
|
4
4
|
static isLeaf(type) {
|
|
@@ -30,5 +30,10 @@ export class T {
|
|
|
30
30
|
static isScalar(type) {
|
|
31
31
|
return type.id.startsWith('scalar:');
|
|
32
32
|
}
|
|
33
|
+
static containers(node) {
|
|
34
|
+
return Array.from(node.children.values())
|
|
35
|
+
.filter((child) => !(child instanceof Prop))
|
|
36
|
+
.map((child) => (child.id.startsWith('ref:') ? child.refType : child));
|
|
37
|
+
}
|
|
33
38
|
}
|
|
34
39
|
//# sourceMappingURL=typeUtils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeUtils.js","sourceRoot":"","sources":["../../../src/oas/nodes/typeUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"typeUtils.js","sourceRoot":"","sources":["../../../src/oas/nodes/typeUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAO,MAAM,EAAE,MAAM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CAAC,UAAU,CAAC,IAAW;QAClC,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;aACtC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,CAAC;aAC3C,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAE,KAAa,CAAC,OAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IACrF,CAAC;CACF","sourcesContent":["import { CircularRef, En, Obj, Prop, PropArray, PropScalar, Ref, Scalar } from './internal.js';\n\nimport { IType } from './internal.js';\nimport _ from 'lodash';\n\nexport class T {\n public static isLeaf(type: IType): boolean {\n return (\n type instanceof Scalar ||\n type instanceof PropScalar ||\n type instanceof En ||\n type instanceof CircularRef ||\n (type instanceof PropArray && type.items instanceof PropScalar) ||\n (type instanceof Obj && _.isEmpty(type.props))\n );\n }\n\n public static isPropScalar(type: IType): boolean {\n return type instanceof PropScalar;\n }\n\n public static traverse(node: IType, callback: (node: IType) => void): void {\n const traverseNode = (n: IType): void => {\n callback(n);\n\n for (const c of n.children) {\n traverseNode(c);\n }\n };\n\n traverseNode(node);\n }\n\n static isMutationType(type: IType): boolean {\n return (\n type.id.startsWith('post:') ||\n type.id.startsWith('put:') ||\n type.id.startsWith('patch:') ||\n type.id.startsWith('del:')\n );\n }\n\n static isScalar(type: IType): boolean {\n return type.id.startsWith('scalar:');\n }\n\n public static containers(node: IType) {\n return Array.from(node.children.values())\n .filter((child) => !(child instanceof Prop))\n .map((child) => (child.id.startsWith('ref:') ? (child as Ref).refType! : child));\n }\n}\n"]}
|
|
@@ -4,13 +4,16 @@ import { OasContext } from '../oasContext.js';
|
|
|
4
4
|
import { Writer } from '../io/writer.js';
|
|
5
5
|
export declare class Union extends Type {
|
|
6
6
|
consolidated: boolean;
|
|
7
|
-
get id(): string;
|
|
8
7
|
schemas: SchemaObject[];
|
|
8
|
+
discriminator?: string;
|
|
9
9
|
constructor(parent: IType, name: string, schemas: SchemaObject[], consolidated?: boolean);
|
|
10
|
+
get id(): string;
|
|
10
11
|
forPrompt(_context: OasContext): string;
|
|
11
12
|
visit(context: OasContext): void;
|
|
12
13
|
generate(context: OasContext, writer: Writer, selection: string[]): void;
|
|
13
14
|
select(context: OasContext, writer: Writer, selection: string[]): void;
|
|
14
15
|
consolidate(selection: string[]): Set<string>;
|
|
16
|
+
private updatePropName;
|
|
15
17
|
private visitProperties;
|
|
18
|
+
private updateName;
|
|
16
19
|
}
|
package/dist/oas/nodes/union.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import { Factory,
|
|
1
|
+
import { Factory, Ref, T, Type } from './internal.js';
|
|
2
2
|
import { trace } from '../log/trace.js';
|
|
3
3
|
import { Naming } from '../utils/naming.js';
|
|
4
|
+
import _ from 'lodash';
|
|
4
5
|
export class Union extends Type {
|
|
5
6
|
consolidated;
|
|
6
|
-
get id() {
|
|
7
|
-
return `union:${this.name}`;
|
|
8
|
-
}
|
|
9
7
|
schemas;
|
|
8
|
+
discriminator;
|
|
10
9
|
constructor(parent, name, schemas, consolidated = false) {
|
|
11
10
|
super(parent, name);
|
|
12
11
|
this.consolidated = consolidated;
|
|
13
12
|
this.schemas = schemas;
|
|
13
|
+
this.discriminator = _.get(parent.schema, 'discriminator')?.propertyName;
|
|
14
|
+
this.updateName();
|
|
15
|
+
}
|
|
16
|
+
get id() {
|
|
17
|
+
return `union:${this.name}`;
|
|
14
18
|
}
|
|
15
19
|
forPrompt(_context) {
|
|
16
20
|
return `${Naming.getRefName(this.name)} (Union)`;
|
|
@@ -51,81 +55,132 @@ export class Union extends Type {
|
|
|
51
55
|
child.generate(context, writer, selection);
|
|
52
56
|
}
|
|
53
57
|
}
|
|
58
|
+
// generate traditional union
|
|
54
59
|
else {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
60
|
+
const name = _.upperFirst(Naming.getRefName(this.name));
|
|
61
|
+
if (!context.generateOptions.consolidateUnion) {
|
|
62
|
+
this.children.forEach((child) => {
|
|
63
|
+
if (child instanceof Ref) {
|
|
64
|
+
child.refType?.generate(context, writer, selection);
|
|
65
|
+
}
|
|
66
|
+
else
|
|
67
|
+
child.generate(context, writer, selection);
|
|
68
|
+
});
|
|
69
|
+
// const selected = this.selectedProps(selection);
|
|
70
|
+
writer
|
|
71
|
+
.append('union ')
|
|
72
|
+
.append(name)
|
|
73
|
+
.append(this.nameSuffix())
|
|
74
|
+
.append(' = ')
|
|
75
|
+
.append(this.children.map((child) => Naming.getRefName(child.name)).join(' | '))
|
|
76
|
+
.append('\n\n');
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// When generating this union in GQL it might look like:
|
|
80
|
+
// union MyUnion = Type1 | Type2 | Type3
|
|
81
|
+
writer.append('#### NOT SUPPORTED YET BY CONNECTORS!!! union ').append(name).append(' = ');
|
|
82
|
+
const childrenNames = this.children.map((child) => Naming.getRefName(child.name)).join(' | ');
|
|
83
|
+
writer.append(childrenNames).append('\n\n');
|
|
84
|
+
trace(context, ' [union::generate]', `[union] -> object: ${this.name}`);
|
|
85
|
+
writer
|
|
86
|
+
.append(this.kind + ' ')
|
|
87
|
+
.append(name)
|
|
88
|
+
.append(this.nameSuffix())
|
|
89
|
+
.append(' { #### replacement for Union ')
|
|
90
|
+
.append(name)
|
|
91
|
+
.append('\n');
|
|
92
|
+
const selected = this.selectedProps(selection);
|
|
93
|
+
for (const prop of selected) {
|
|
94
|
+
trace(context, ' [union::generate]', `-> property: ${prop.name} (parent: ${prop.parent.name})`);
|
|
95
|
+
prop.generate(context, writer, selection);
|
|
96
|
+
}
|
|
97
|
+
writer.append('} \n### End replacement for ').append(this.name).append('\n\n');
|
|
75
98
|
}
|
|
76
|
-
writer.append('} \n### End replacement for ').append(this.name).append('\n\n');
|
|
77
99
|
}
|
|
78
100
|
trace(context, '<- [union::generate]', 'out: ' + schemas);
|
|
79
101
|
context.leave(this);
|
|
80
102
|
}
|
|
81
103
|
select(context, writer, selection) {
|
|
82
104
|
trace(context, '-> [union::select]', `-> in: ${this.name}`);
|
|
83
|
-
if (
|
|
84
|
-
this.
|
|
105
|
+
if (context.generateOptions.consolidateUnion) {
|
|
106
|
+
if (!this.consolidated) {
|
|
107
|
+
this.consolidate(selection);
|
|
108
|
+
}
|
|
109
|
+
const selected = this.selectedProps(selection);
|
|
110
|
+
for (const prop of selected) {
|
|
111
|
+
prop.select(context, writer, selection);
|
|
112
|
+
}
|
|
85
113
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
114
|
+
else {
|
|
115
|
+
// collect all property names from children and write them here
|
|
116
|
+
this.children.forEach((child) => {
|
|
117
|
+
child.select(context, writer, selection);
|
|
118
|
+
});
|
|
89
119
|
}
|
|
120
|
+
/* TODO:
|
|
121
|
+
dataPoints: dataFormat->match(
|
|
122
|
+
["raw", $.dataPoints],
|
|
123
|
+
["normal", $.dataPoints {
|
|
124
|
+
priceDateTime
|
|
125
|
+
# all other fields
|
|
126
|
+
}],
|
|
127
|
+
[@, $ { # optimized
|
|
128
|
+
priceDateTime
|
|
129
|
+
# all other fields
|
|
130
|
+
}
|
|
131
|
+
])
|
|
132
|
+
*/
|
|
90
133
|
trace(context, '<- [union::select]', `-> out: ${this.name}`);
|
|
91
134
|
}
|
|
92
135
|
consolidate(selection) {
|
|
93
136
|
const ids = new Set();
|
|
94
|
-
|
|
95
|
-
const
|
|
137
|
+
const props = [];
|
|
138
|
+
const discriminator = this.discriminator;
|
|
139
|
+
const queue = T.containers(this);
|
|
96
140
|
while (queue.length > 0) {
|
|
97
141
|
const node = queue.shift();
|
|
98
|
-
ids.add(node
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
142
|
+
ids.add(node.id);
|
|
143
|
+
// process each prop, renaming the ones that have a name clash with others. if a selection
|
|
144
|
+
// is passed, then only add those, otherwise add all.
|
|
145
|
+
for (const prop of _.isEmpty(selection)
|
|
146
|
+
? Array.from(node.props.values()) // include all
|
|
147
|
+
: Array.from(node.props.values())
|
|
148
|
+
.filter((i) => selection.find((s) => s.startsWith(i.path()))) // include selected only
|
|
149
|
+
.filter((i) => i.name !== discriminator)) {
|
|
150
|
+
// remove discriminator props
|
|
151
|
+
// rename every prop there is if there's a conflict, then add it to props
|
|
152
|
+
this.updatePropName(prop, props, selection);
|
|
153
|
+
props.push(prop);
|
|
108
154
|
}
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
const children = Array.from(node.children.values()).filter((child) => !(child instanceof Prop));
|
|
112
|
-
queue.push(...children);
|
|
155
|
+
// find the other containers and add them to the queue
|
|
156
|
+
queue.push(...T.containers(node));
|
|
113
157
|
}
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
158
|
+
// add the discriminator, if we have one
|
|
159
|
+
if (discriminator)
|
|
160
|
+
props.push(this.children[0].props.get(discriminator));
|
|
161
|
+
// and finally sort the props and copy them to our original
|
|
162
|
+
props.sort((a, b) => a.name.localeCompare(b.name)).forEach((prop) => this.props.set(prop.name, prop));
|
|
117
163
|
// and return the types.ts we've used
|
|
164
|
+
this.consolidated = true;
|
|
118
165
|
return ids;
|
|
119
166
|
}
|
|
167
|
+
updatePropName(prop, props, selection) {
|
|
168
|
+
let name = prop.name;
|
|
169
|
+
let counter = 0;
|
|
170
|
+
while (props.find((p) => p.name === name)) {
|
|
171
|
+
name = `${prop.name}${++counter}`;
|
|
172
|
+
}
|
|
173
|
+
if (name !== prop.name) {
|
|
174
|
+
prop.name = name;
|
|
175
|
+
// we need to extend the selection too, because a new property has been created
|
|
176
|
+
selection.push(prop.path());
|
|
177
|
+
}
|
|
178
|
+
}
|
|
120
179
|
visitProperties(_context) {
|
|
121
180
|
// do nothing?
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
//
|
|
126
|
-
// if (!this.children.includes(prop))
|
|
127
|
-
// this.add(prop);
|
|
128
|
-
// }
|
|
181
|
+
}
|
|
182
|
+
updateName() {
|
|
183
|
+
this.name = this.parent.name + `Union`;
|
|
129
184
|
}
|
|
130
185
|
}
|
|
131
186
|
//# sourceMappingURL=union.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../../src/oas/nodes/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAS,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,MAAM,OAAO,KAAM,SAAQ,IAAI;IAUpB;IATT,IAAI,EAAE;QACJ,OAAO,SAAS,IAAI,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IACM,OAAO,CAAiB;IAE/B,YACE,MAAa,EACb,IAAY,EACZ,OAAuB,EAChB,eAAwB,KAAK;QAEpC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAFb,iBAAY,GAAZ,YAAY,CAAiB;QAGpC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAEM,SAAS,CAAC,QAAoB;QACnC,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;IACnD,CAAC;IAEM,KAAK,CAAC,OAAmB;QAC9B,IAAI,IAAI,CA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KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;YAE7C,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC;YAEhG,KAAK,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC;QAC1B,CAAC;QAED,wDAAwD;QACxD,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;QAEzD,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QAEzB,qCAAqC;QACrC,OAAO,GAAG,CAAC;IACb,CAAC;IAEO,eAAe,CAAC,QAAoB;QAC1C,cAAc;QACd,iDAAiD;QACjD,mDAAmD;QACnD,qCAAqC;QACrC,EAAE;QACF,uCAAuC;QACvC,sBAAsB;QACtB,IAAI;IACN,CAAC;CACF","sourcesContent":["import { Factory, IType, Prop, Ref, Type } from './internal.js';\nimport { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\n\nexport class Union extends Type {\n get id(): string {\n return `union:${this.name}`;\n }\n public schemas: SchemaObject[];\n\n constructor(\n parent: IType,\n name: string,\n schemas: SchemaObject[],\n public consolidated: boolean = false,\n ) {\n super(parent, name);\n this.schemas = schemas;\n }\n\n public forPrompt(_context: OasContext): string {\n return `${Naming.getRefName(this.name)} (Union)`;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n const schemas = this.schemas.map((s) => s.type);\n\n context.enter(this);\n trace(context, '-> [union:visit]', 'in: ' + schemas);\n\n if (!context.inContextOf('Composed', this)) {\n trace(context, '[union]', 'In union: ' + this.parent?.name);\n }\n\n for (const refSchema of this.schemas) {\n const type = Factory.fromSchema(this, refSchema);\n trace(context, ' [union:visit]', 'of type: ' + type);\n\n type.visit(context);\n }\n\n if (!context.inContextOf('Param', this)) {\n this.visitProperties(context);\n }\n\n if (this.name != null) {\n context.store(this.name, this);\n }\n\n this.visited = true;\n trace(context, '<- [union:visit]', 'out: ' + schemas);\n context.leave(this);\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n context.enter(this);\n const schemas = this.schemas.map((s) => s.type);\n trace(context, '-> [union::generate]', 'in: ' + schemas);\n\n /* params with Unions are weird, but here's an example:\n * id: oneOf [string, Enum {me}] */\n if (context.inContextOf('Param', this)) {\n for (const child of this.children) {\n child.generate(context, writer, selection);\n }\n } else {\n // When generating this union in GQL it might look like:\n // union MyUnion = Type1 | Type2 | Type3\n writer\n .append('#### NOT SUPPORTED YET BY CONNECTORS!!! union ')\n .append(Naming.getRefName(this.name))\n .append(' = ');\n\n const childrenNames = this.children.map((child) => Naming.getRefName(child.name)).join('# | ');\n\n writer.append(childrenNames).append('#\\n\\n');\n\n trace(context, ' [union::generate]', `[union] -> object: ${this.name}`);\n\n writer\n .append(this.kind + ' ')\n .append(Naming.getRefName(this.name))\n .append(this.nameSuffix())\n .append(' { #### replacement for Union ')\n .append(this.name)\n .append('\\n');\n\n const selected = this.selectedProps(selection);\n for (const prop of selected) {\n trace(context, ' [union::generate]', `-> property: ${prop.name} (parent: ${prop.parent!.name})`);\n prop.generate(context, writer, selection);\n }\n\n writer.append('} \\n### End replacement for ').append(this.name).append('\\n\\n');\n }\n\n trace(context, '<- [union::generate]', 'out: ' + schemas);\n context.leave(this);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]): void {\n trace(context, '-> [union::select]', `-> in: ${this.name}`);\n if (!this.consolidated) {\n this.consolidate(selection);\n }\n\n const selected = this.selectedProps(selection);\n\n for (const prop of selected) {\n prop.select(context, writer, selection);\n }\n\n trace(context, '<- [union::select]', `-> out: ${this.name}`);\n }\n\n public consolidate(selection: string[]): Set<string> {\n const ids: Set<string> = new Set();\n let props: Map<string, Prop> = new Map();\n\n const queue: IType[] = Array.from(this.children.values()).filter((child) => !(child instanceof Prop));\n\n while (queue.length > 0) {\n const node = queue.shift()!;\n\n ids.add(node instanceof Ref ? (node as Ref).refType!.id : node.id);\n\n if (selection.length > 0) {\n node.props.forEach((prop) => {\n if (selection.find((s) => s.startsWith(prop.path()))) {\n props.set(prop.name, prop);\n }\n });\n } else {\n node.props.forEach((prop) => props.set(prop.name, prop));\n }\n\n // sort props\n props = new Map([...props.entries()].sort());\n\n const children = Array.from(node.children.values()).filter((child) => !(child instanceof Prop));\n\n queue.push(...children);\n }\n\n // copy all collected props from children into this node\n props.forEach((prop) => this.props.set(prop.name, prop));\n\n this.consolidated = true;\n\n // and return the types.ts we've used\n return ids;\n }\n\n private visitProperties(_context: OasContext): void {\n // do nothing?\n // for (const [_, prop] of collected.entries()) {\n // trace(context, ' [union]', 'prop: ' + prop);\n // this.props.set(prop.name, prop);\n //\n // if (!this.children.includes(prop))\n // this.add(prop);\n // }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../../src/oas/nodes/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAEnE,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB,MAAM,OAAO,KAAM,SAAQ,IAAI;IAQpB;IAPF,OAAO,CAAiB;IACxB,aAAa,CAAU;IAE9B,YACE,MAAa,EACb,IAAY,EACZ,OAAuB,EAChB,eAAwB,KAAK;QAEpC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAFb,iBA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AAI,CAAC,IAAI,GAAG,EAAE,OAAO,EAAE,CAAC;QACpC,CAAC;QAED,IAAI,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC;YACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,+EAA+E;YAC/E,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC;IAEO,eAAe,CAAC,QAAoB;QAC1C,cAAc;IAChB,CAAC;IAEO,UAAU;QAChB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAO,CAAC,IAAI,GAAG,OAAO,CAAC;IAC1C,CAAC;CACF","sourcesContent":["import { Factory, IType, Prop, Ref, T, Type } from './internal.js';\nimport { SchemaObject } from 'oas/types';\nimport { trace } from '../log/trace.js';\nimport { OasContext } from '../oasContext.js';\nimport { Writer } from '../io/writer.js';\nimport { Naming } from '../utils/naming.js';\nimport _ from 'lodash';\n\nexport class Union extends Type {\n public schemas: SchemaObject[];\n public discriminator?: string;\n\n constructor(\n parent: IType,\n name: string,\n schemas: SchemaObject[],\n public consolidated: boolean = false,\n ) {\n super(parent, name);\n this.schemas = schemas;\n this.discriminator = _.get((parent as Prop).schema, 'discriminator')?.propertyName;\n this.updateName();\n }\n\n get id(): string {\n return `union:${this.name}`;\n }\n\n public forPrompt(_context: OasContext): string {\n return `${Naming.getRefName(this.name)} (Union)`;\n }\n\n public visit(context: OasContext): void {\n if (this.visited) {\n return;\n }\n\n const schemas = this.schemas.map((s) => s.type);\n\n context.enter(this);\n trace(context, '-> [union:visit]', 'in: ' + schemas);\n\n if (!context.inContextOf('Composed', this)) {\n trace(context, '[union]', 'In union: ' + this.parent?.name);\n }\n\n for (const refSchema of this.schemas) {\n const type = Factory.fromSchema(this, refSchema);\n trace(context, ' [union:visit]', 'of type: ' + type);\n\n type.visit(context);\n }\n\n if (!context.inContextOf('Param', this)) {\n this.visitProperties(context);\n }\n\n if (this.name != null) {\n context.store(this.name, this);\n }\n\n this.visited = true;\n trace(context, '<- [union:visit]', 'out: ' + schemas);\n context.leave(this);\n }\n\n public generate(context: OasContext, writer: Writer, selection: string[]): void {\n context.enter(this);\n const schemas = this.schemas.map((s) => s.type);\n trace(context, '-> [union::generate]', 'in: ' + schemas);\n\n /* params with Unions are weird, but here's an example:\n * id: oneOf [string, Enum {me}] */\n if (context.inContextOf('Param', this)) {\n for (const child of this.children) {\n child.generate(context, writer, selection);\n }\n }\n // generate traditional union\n else {\n const name = _.upperFirst(Naming.getRefName(this.name));\n\n if (!context.generateOptions.consolidateUnion) {\n this.children.forEach((child) => {\n if (child instanceof Ref) {\n (child as Ref).refType?.generate(context, writer, selection);\n } else child.generate(context, writer, selection);\n });\n\n // const selected = this.selectedProps(selection);\n writer\n .append('union ')\n .append(name)\n .append(this.nameSuffix())\n .append(' = ')\n .append(this.children.map((child) => Naming.getRefName(child.name)).join(' | '))\n .append('\\n\\n');\n } else {\n // When generating this union in GQL it might look like:\n // union MyUnion = Type1 | Type2 | Type3\n writer.append('#### NOT SUPPORTED YET BY CONNECTORS!!! union ').append(name).append(' = ');\n\n const childrenNames = this.children.map((child) => Naming.getRefName(child.name)).join(' | ');\n\n writer.append(childrenNames).append('\\n\\n');\n\n trace(context, ' [union::generate]', `[union] -> object: ${this.name}`);\n\n writer\n .append(this.kind + ' ')\n .append(name)\n .append(this.nameSuffix())\n .append(' { #### replacement for Union ')\n .append(name)\n .append('\\n');\n\n const selected = this.selectedProps(selection);\n for (const prop of selected) {\n trace(context, ' [union::generate]', `-> property: ${prop.name} (parent: ${prop.parent!.name})`);\n prop.generate(context, writer, selection);\n }\n\n writer.append('} \\n### End replacement for ').append(this.name).append('\\n\\n');\n }\n }\n\n trace(context, '<- [union::generate]', 'out: ' + schemas);\n context.leave(this);\n }\n\n public select(context: OasContext, writer: Writer, selection: string[]): void {\n trace(context, '-> [union::select]', `-> in: ${this.name}`);\n\n if (context.generateOptions.consolidateUnion) {\n if (!this.consolidated) {\n this.consolidate(selection);\n }\n\n const selected = this.selectedProps(selection);\n for (const prop of selected) {\n prop.select(context, writer, selection);\n }\n } else {\n // collect all property names from children and write them here\n this.children.forEach((child) => {\n child.select(context, writer, selection);\n });\n }\n\n /* TODO:\n dataPoints: dataFormat->match(\n [\"raw\", $.dataPoints],\n [\"normal\", $.dataPoints {\n priceDateTime\n # all other fields\n }],\n [@, $ { # optimized\n priceDateTime\n # all other fields\n }\n ])\n */\n\n trace(context, '<- [union::select]', `-> out: ${this.name}`);\n }\n\n public consolidate(selection: string[]): Set<string> {\n const ids: Set<string> = new Set();\n const props: Prop[] = [];\n const discriminator = this.discriminator;\n\n const queue = T.containers(this);\n while (queue.length > 0) {\n const node = queue.shift()!;\n ids.add(node.id);\n\n // process each prop, renaming the ones that have a name clash with others. if a selection\n // is passed, then only add those, otherwise add all.\n for (const prop of _.isEmpty(selection)\n ? Array.from(node.props.values()) // include all\n : Array.from(node.props.values())\n .filter((i) => selection.find((s) => s.startsWith(i.path()))) // include selected only\n .filter((i) => i.name !== discriminator)) {\n // remove discriminator props\n // rename every prop there is if there's a conflict, then add it to props\n this.updatePropName(prop, props, selection);\n props.push(prop);\n }\n\n // find the other containers and add them to the queue\n queue.push(...T.containers(node));\n }\n\n // add the discriminator, if we have one\n if (discriminator) props.push(this.children[0].props.get(discriminator)!);\n\n // and finally sort the props and copy them to our original\n props.sort((a, b) => a.name.localeCompare(b.name)).forEach((prop) => this.props.set(prop.name, prop));\n\n // and return the types.ts we've used\n this.consolidated = true;\n return ids;\n }\n\n private updatePropName(prop: Prop, props: Prop[], selection: string[]) {\n let name = prop.name;\n\n let counter = 0;\n while (props.find((p) => p.name === name)) {\n name = `${prop.name}${++counter}`;\n }\n\n if (name !== prop.name) {\n prop.name = name;\n // we need to extend the selection too, because a new property has been created\n selection.push(prop.path());\n }\n }\n\n private visitProperties(_context: OasContext): void {\n // do nothing?\n }\n\n private updateName(): void {\n this.name = this.parent!.name + `Union`;\n }\n}\n"]}
|