1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
4 | exports.__esModule = true;
|
---|
5 | exports.default = void 0;
|
---|
6 | var _ramda = require("ramda");
|
---|
7 | var _ramdaAdjunct = require("ramda-adjunct");
|
---|
8 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
9 | var _Visitor = _interopRequireDefault(require("./Visitor.cjs"));
|
---|
10 | var _FallbackVisitor = _interopRequireDefault(require("./FallbackVisitor.cjs"));
|
---|
11 | /**
|
---|
12 | * This is a base Type for every visitor that does
|
---|
13 | * internal look-ups to retrieve other child visitors.
|
---|
14 | */
|
---|
15 |
|
---|
16 | class SpecificationVisitor extends _Visitor.default {
|
---|
17 | specObj;
|
---|
18 | passingOptionsNames = ['specObj', 'openApiGenericElement', 'openApiSemanticElement'];
|
---|
19 | openApiGenericElement;
|
---|
20 | openApiSemanticElement;
|
---|
21 | constructor({
|
---|
22 | specObj,
|
---|
23 | passingOptionsNames,
|
---|
24 | openApiGenericElement,
|
---|
25 | openApiSemanticElement,
|
---|
26 | ...rest
|
---|
27 | }) {
|
---|
28 | super({
|
---|
29 | ...rest
|
---|
30 | });
|
---|
31 | this.specObj = specObj;
|
---|
32 | this.openApiGenericElement = openApiGenericElement;
|
---|
33 | this.openApiSemanticElement = openApiSemanticElement;
|
---|
34 | if (Array.isArray(passingOptionsNames)) {
|
---|
35 | this.passingOptionsNames = passingOptionsNames;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | retrievePassingOptions() {
|
---|
39 | return (0, _ramda.pick)(this.passingOptionsNames, this);
|
---|
40 | }
|
---|
41 | retrieveFixedFields(specPath) {
|
---|
42 | const fixedFields = (0, _ramda.path)(['visitors', ...specPath, 'fixedFields'], this.specObj);
|
---|
43 | if (typeof fixedFields === 'object' && fixedFields !== null) {
|
---|
44 | return Object.keys(fixedFields);
|
---|
45 | }
|
---|
46 | return [];
|
---|
47 | }
|
---|
48 | retrieveVisitor(specPath) {
|
---|
49 | if ((0, _ramda.pathSatisfies)(_ramdaAdjunct.isFunction, ['visitors', ...specPath], this.specObj)) {
|
---|
50 | return (0, _ramda.path)(['visitors', ...specPath], this.specObj);
|
---|
51 | }
|
---|
52 | return (0, _ramda.path)(['visitors', ...specPath, '$visitor'], this.specObj);
|
---|
53 | }
|
---|
54 | retrieveVisitorInstance(specPath, options = {}) {
|
---|
55 | const passingOpts = this.retrievePassingOptions();
|
---|
56 | const VisitorClz = this.retrieveVisitor(specPath);
|
---|
57 | const visitorOpts = {
|
---|
58 | ...passingOpts,
|
---|
59 | ...options
|
---|
60 | };
|
---|
61 | return new VisitorClz(visitorOpts);
|
---|
62 | }
|
---|
63 | toRefractedElement(specPath, element, options = {}) {
|
---|
64 | /**
|
---|
65 | * This is `Visitor shortcut`: mechanism for short-circuiting the traversal and replacing
|
---|
66 | * it by basic node cloning.
|
---|
67 | *
|
---|
68 | * Visiting the element is equivalent to cloning it if the prototype of a visitor
|
---|
69 | * is the same as the prototype of FallbackVisitor. If that's the case, we can avoid
|
---|
70 | * bootstrapping the traversal cycle for fields that don't require any special visiting.
|
---|
71 | */
|
---|
72 | const visitor = this.retrieveVisitorInstance(specPath, options);
|
---|
73 | if (visitor instanceof _FallbackVisitor.default && (visitor == null ? void 0 : visitor.constructor) === _FallbackVisitor.default) {
|
---|
74 | return (0, _apidomCore.cloneDeep)(element);
|
---|
75 | }
|
---|
76 | (0, _apidomCore.visit)(element, visitor, options);
|
---|
77 | return visitor.element;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | var _default = exports.default = SpecificationVisitor; |
---|