Skip to content

Commit

Permalink
refactor(ns-ads): replace toValue method with function (#3195)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Sep 30, 2023
1 parent 57c8602 commit 32aafd8
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toValue } from '@swagger-api/apidom-core';
import {
PathItemElement,
ParameterElement,
Expand All @@ -23,9 +24,9 @@ const plugin = () => () => {
['http', 'transaction'],
['http', 'request'],
['http', 'request', 'url'],
['http', 'request', 'url', parentPathItem.meta.get('path').toValue()],
['http', 'request', 'url', toValue(parentPathItem.meta.get('path'))],
['http', 'request', 'method'],
['http', 'request', 'method', element.meta.get('http-method').toValue().toLowerCase()],
['http', 'request', 'method', toValue(element.meta.get('http-method')).toLowerCase()],
);

// fold PathItem.parameters to Operation.parameters
Expand All @@ -34,13 +35,13 @@ const plugin = () => () => {
if (
isStringElement(parameter.in) &&
isStringElement(parameter.name) &&
parameter.in?.toValue() === 'header'
toValue(parameter.in) === 'header'
) {
operationIdentifiers.push(
['http', 'request', 'header'],
['http', 'request', 'header', parameter.name?.toValue()],
['http', 'request', 'header', toValue(parameter.name)],
['http', 'message', 'header'],
['http', 'message', 'header', parameter.name?.toValue()],
['http', 'message', 'header', toValue(parameter.name)],
);
}
});
Expand All @@ -54,13 +55,13 @@ const plugin = () => () => {
if (
isStringElement(element.in) &&
isStringElement(element.name) &&
element.in?.toValue() === 'header'
toValue(element.in) === 'header'
) {
operationIdentifiers.push(
['http', 'request', 'header'],
['http', 'request', 'header', element.name?.toValue()],
['http', 'request', 'header', toValue(element.name)],
['http', 'message', 'header'],
['http', 'message', 'header', element.name?.toValue()],
['http', 'message', 'header', toValue(element.name)],
);
}
},
Expand All @@ -85,7 +86,7 @@ const plugin = () => () => {
responseIdentifiers.push(['http', 'response']);

if (element.meta.hasKey('http-status-code')) {
const statusCode = String(element.meta.get('http-status-code').toValue());
const statusCode = String(toValue(element.meta.get('http-status-code')));
const statusCodeAlias = statusCode.startsWith('2')
? 'success'
: statusCode.startsWith('3')
Expand All @@ -105,7 +106,7 @@ const plugin = () => () => {

if (typeof element.headers !== 'undefined' && isObjectElement(element.headers)) {
element.headers.forEach((value, key) => {
const headerName = key.toValue();
const headerName = toValue(key);

responseIdentifiers.push(
['http', 'response', 'header'],
Expand All @@ -119,7 +120,7 @@ const plugin = () => () => {
responseIdentifiers.push(['http', 'response', 'body'], ['http', 'message', 'body']);

element.contentProp.forEach((value, key) => {
const headerName = key.toValue();
const headerName = toValue(key);

responseIdentifiers.push(
['http', 'response', 'header'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import stampit from 'stampit';
import { StringElement, BREAK } from '@swagger-api/apidom-core';
import { StringElement, BREAK, toValue } from '@swagger-api/apidom-core';

import FallbackVisitor from '../../FallbackVisitor';
import SpecificationVisitor from '../../SpecificationVisitor';
Expand All @@ -8,7 +8,7 @@ import RequirementLevelElement from '../../../../elements/RequirementLevel';
const RequirementLevelVisitor = stampit(SpecificationVisitor, FallbackVisitor, {
methods: {
StringElement(stringElement: StringElement) {
const requirementLevelElement = new RequirementLevelElement(stringElement.toValue());
const requirementLevelElement = new RequirementLevelElement(toValue(stringElement));

this.copyMetaAndAttributes(stringElement, requirementLevelElement);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Element, visit, ArrayElement } from '@swagger-api/apidom-core';
import { Element, visit, ArrayElement, toValue } from '@swagger-api/apidom-core';
import {
OperationElement,
ResponseElement,
Expand All @@ -18,7 +18,7 @@ const access = (
selected: OperationElement | ResponseElement,
standardIdentifier: StandardIdentifierElement,
): ArrayElement => {
const strStandardIdentifier = String(standardIdentifier.toValue());
const strStandardIdentifier = String(toValue(standardIdentifier));
const values = new ArrayElement();
const visitor = {
enter(element: Element) {
Expand All @@ -27,7 +27,7 @@ const access = (
element.meta
.get('ads-a-standard-identifier')
.content.filter((accessorMapping: any) => {
return String(accessorMapping.get('subject').toValue()) === strStandardIdentifier;
return String(toValue(accessorMapping.get('subject'))) === strStandardIdentifier;
})
.forEach((accessorMapping: any) => {
values.push(accessorMapping.get('value'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { includes } from 'ramda';
import { visit } from '@swagger-api/apidom-core';
import { visit, toValue } from '@swagger-api/apidom-core';
import {
OperationElement,
OpenApi3_1Element,
Expand All @@ -25,18 +25,18 @@ const select = (
OperationElement(element: OperationElement) {
if (!element.meta.hasKey('ads-s-standard-identifier')) return;

const standardIdentifiers = element.meta.get('ads-s-standard-identifier').toValue();
const standardIdentifiers = toValue(element.meta.get('ads-s-standard-identifier'));

if (includes(standardIdentifier.toValue(), standardIdentifiers)) {
if (includes(toValue(standardIdentifier), standardIdentifiers)) {
selected.push(element);
}
},
ResponseElement(element: ResponseElement) {
if (!element.meta.hasKey('ads-s-standard-identifier')) return;

const standardIdentifiers = element.meta.get('ads-s-standard-identifier').toValue();
const standardIdentifiers = toValue(element.meta.get('ads-s-standard-identifier'));

if (includes(standardIdentifier.toValue(), standardIdentifiers)) {
if (includes(toValue(standardIdentifier), standardIdentifiers)) {
selected.push(element);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ describe('refractor', function () {
});

// @ts-ignore
assert.strictEqual(mainElement.version.meta.get('metaKey').toValue(), 'metaValue');
assert.strictEqual(toValue(mainElement.version.meta.get('metaKey')), 'metaValue');
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { assert } from 'chai';
import { toValue } from '@swagger-api/apidom-core';
import { parse } from '@swagger-api/apidom-parser-adapter-json';
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';

Expand Down Expand Up @@ -38,7 +39,7 @@ describe('given OpenAPI 3.1 definition with Standard Identifier plugin applied',
const [operationElement] = select(openapiElement, selectorStandardIdentifier);
const value = access(operationElement, accessorStandardIdentifier);

assert.deepEqual(value.toValue(), ['get']);
assert.deepEqual(toValue(value), ['get']);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { assert } from 'chai';
import { AnnotationElement } from '@swagger-api/apidom-core';
import { AnnotationElement, toValue } from '@swagger-api/apidom-core';
import { parse as parseJSON } from '@swagger-api/apidom-parser-adapter-json';
import { parse as parseYAML } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('given API Design Systems and OpenAPI 3.1 definitions', function () {
const annotations = validateOpenAPI3_1(mainElement, openapiElement);
const statusCodeAnnotation = annotations.find((annotation: AnnotationElement) => {
return (
annotation.toValue() ===
toValue(annotation) ===
'"application/test" not allowed for subject ["http","request","header","Content-Type"]'
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { assert } from 'chai';
import { AnnotationElement } from '@swagger-api/apidom-core';
import { AnnotationElement, toValue } from '@swagger-api/apidom-core';
import { parse as parseJSON } from '@swagger-api/apidom-parser-adapter-json';
import { parse as parseYAML } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('given API Design Systems and OpenAPI 3.1 definitions', function () {
const annotations = validateOpenAPI3_1(mainElement, openapiElement);
const statusCodeAnnotation = annotations.find((annotation: AnnotationElement) => {
return (
annotation.toValue() ===
toValue(annotation) ===
'"X-Custom-Header" not allowed for subject ["http","request","header"]'
);
});
Expand All @@ -59,7 +59,7 @@ describe('given API Design Systems and OpenAPI 3.1 definitions', function () {
const annotations = validateOpenAPI3_1(mainElement, openapiElement);
const statusCodeAnnotation = annotations.find((annotation: AnnotationElement) => {
return (
annotation.toValue() ===
toValue(annotation) ===
'"X-Custom-Header-2" not allowed for subject ["http","request","header"]'
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { assert } from 'chai';
import { AnnotationElement } from '@swagger-api/apidom-core';
import { AnnotationElement, toValue } from '@swagger-api/apidom-core';
import { parse as parseJSON } from '@swagger-api/apidom-parser-adapter-json';
import { parse as parseYAML } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('given API Design Systems and OpenAPI 3.1 definitions', function () {
it('should produce annotation about trace method', function () {
const annotations = validateOpenAPI3_1(mainElement, openapiElement);
const traceAnnotation = annotations.find((annotation: AnnotationElement) => {
return annotation.toValue() === '"trace" not allowed for subject ["http","request","method"]';
return toValue(annotation) === '"trace" not allowed for subject ["http","request","method"]';
});

assert.isTrue(traceAnnotation instanceof AnnotationElement);
Expand All @@ -50,7 +50,7 @@ describe('given API Design Systems and OpenAPI 3.1 definitions', function () {
const annotations = validateOpenAPI3_1(mainElement, openapiElement);
const traceAnnotation = annotations.find((annotation: AnnotationElement) => {
return (
annotation.toValue() === '"options" not allowed for subject ["http","request","method"]'
toValue(annotation) === '"options" not allowed for subject ["http","request","method"]'
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { assert } from 'chai';
import { AnnotationElement } from '@swagger-api/apidom-core';
import { AnnotationElement, toValue } from '@swagger-api/apidom-core';
import { parse as parseJSON } from '@swagger-api/apidom-parser-adapter-json';
import { parse as parseYAML } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('given API Design Systems and OpenAPI 3.1 definitions', function () {
const annotations = validateOpenAPI3_1(mainElement, openapiElement);
const statusCodeAnnotation = annotations.find((annotation: AnnotationElement) => {
return (
annotation.toValue() === '"201" not allowed for subject ["http","response","status_code"]'
toValue(annotation) === '"201" not allowed for subject ["http","response","status_code"]'
);
});

Expand All @@ -58,7 +58,7 @@ describe('given API Design Systems and OpenAPI 3.1 definitions', function () {
const annotations = validateOpenAPI3_1(mainElement, openapiElement);
const statusCodeAnnotation = annotations.find((annotation: AnnotationElement) => {
return (
annotation.toValue() === '"305" not allowed for subject ["http","response","status_code"]'
toValue(annotation) === '"305" not allowed for subject ["http","response","status_code"]'
);
});

Expand Down

0 comments on commit 32aafd8

Please sign in to comment.