Skip to content

Commit

Permalink
refactor(ns-asyncapi-2): replace toValue method with function (#3199)
Browse files Browse the repository at this point in the history
Refs #3198
  • Loading branch information
char0n authored Oct 2, 2023
1 parent ff10994 commit e311d44
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 21 deletions.
11 changes: 8 additions & 3 deletions packages/apidom-ns-asyncapi-2/src/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { createPredicate, isBooleanElement, isStringElement } from '@swagger-api/apidom-core';
import {
createPredicate,
isBooleanElement,
isStringElement,
toValue,
} from '@swagger-api/apidom-core';

import AsyncApi2Element from './elements/AsyncApi2';
import AsyncApiVersionElement from './elements/AsyncApiVersion';
Expand Down Expand Up @@ -70,7 +75,7 @@ export const isChannelItemElementExternal = (element: any): element is ChannelIt
return false;
}

const value = element.$ref.toValue();
const value = toValue(element.$ref);

return typeof value === 'string' && value.length > 0 && !value.startsWith('#');
};
Expand Down Expand Up @@ -180,7 +185,7 @@ export const isReferenceElementExternal = (element: any): element is ReferenceEl
return false;
}

const value = element.$ref.toValue();
const value = toValue(element.$ref);

return typeof value === 'string' && value.length > 0 && !value.startsWith('#');
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
includesClasses,
isArrayElement,
cloneDeep,
toValue,
} from '@swagger-api/apidom-core';

import mediaTypes from '../../media-types';
Expand Down Expand Up @@ -389,7 +390,7 @@ const schema = {
payload(...args: any[]) {
// @ts-ignore
const { context: messageElement } = this;
const schemaFormat = defaultTo(mediaTypes.latest(), messageElement.schemaFormat?.toValue());
const schemaFormat = defaultTo(mediaTypes.latest(), toValue(messageElement.schemaFormat));

if (mediaTypes.includes(schemaFormat)) {
return new SchemaElement(...args);
Expand Down Expand Up @@ -1026,7 +1027,7 @@ const schema = {

const findElementFactory = (ancestor: any, keyName: string) => {
const elementType = getNodeType(ancestor); // @ts-ignore
const keyMapping = schema[elementType] || schema[ancestor.classes.first?.toValue?.()];
const keyMapping = schema[elementType] || schema[toValue(ancestor.classes.first)];

return typeof keyMapping === 'undefined'
? undefined
Expand All @@ -1044,7 +1045,7 @@ const plugin = () => () => {

const [, , , ancestors] = rest;
const ancestor = ancestors[ancestors.length - 1]; // @ts-ignore
const elementFactory = findElementFactory(ancestor, element.key.toValue());
const elementFactory = findElementFactory(ancestor, toValue(element.key));

// no element factory found
if (typeof elementFactory === 'undefined') return undefined;
Expand Down
10 changes: 8 additions & 2 deletions packages/apidom-ns-asyncapi-2/src/refractor/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { MemberElement, isStringElement, isObjectElement, Element } from '@swagger-api/apidom-core';
import { startsWith } from 'ramda';
import {
MemberElement,
isStringElement,
isObjectElement,
Element,
toValue,
} from '@swagger-api/apidom-core';

export const isAsyncApi2LikeElement = <T extends Element>(element: T): boolean => {
return (
Expand Down Expand Up @@ -39,5 +45,5 @@ export const isServerLikeElement = <T extends Element>(element: T): boolean => {

export const isAsyncApiExtension = (element: MemberElement): boolean => {
// @ts-ignore
return isStringElement(element.key) && startsWith('x-', element.key.toValue());
return isStringElement(element.key) && startsWith('x-', toValue(element.key));
};
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 AsyncApiVersionElement from '../../../elements/AsyncApiVersion';
const AsyncApiVersionVisitor = stampit(SpecificationVisitor, FallbackVisitor, {
methods: {
StringElement(stringElement: StringElement) {
const asyncApiVersionElement = new AsyncApiVersionElement(stringElement.toValue());
const asyncApiVersionElement = new AsyncApiVersionElement(toValue(stringElement));

this.copyMetaAndAttributes(stringElement, asyncApiVersionElement);

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 DefaultContentTypeElement from '../../../elements/DefaultContentType';
const DefaultContentTypeVisitor = stampit(SpecificationVisitor, FallbackVisitor, {
methods: {
StringElement(stringElement: StringElement) {
const defaultContentTypeElement = new DefaultContentTypeElement(stringElement.toValue());
const defaultContentTypeElement = new DefaultContentTypeElement(toValue(stringElement));

this.copyMetaAndAttributes(stringElement, defaultContentTypeElement);

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 IdentifierElement from '../../../elements/Identifier';
const IdentifierVisitor = stampit(SpecificationVisitor, FallbackVisitor, {
methods: {
StringElement(stringElement: StringElement) {
const identifierElement = new IdentifierElement(stringElement.toValue());
const identifierElement = new IdentifierElement(toValue(stringElement));

this.copyMetaAndAttributes(stringElement, identifierElement);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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';

const VersionVisitor = stampit(FallbackVisitor, {
methods: {
StringElement(stringElement: StringElement) {
this.element = new StringElement(stringElement.toValue());
this.element = new StringElement(toValue(stringElement));

this.copyMetaAndAttributes(stringElement, this.element);
this.element.classes.push('api-version');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stampit from 'stampit';
import { always, defaultTo } from 'ramda';
import { ObjectElement, isObjectElement } from '@swagger-api/apidom-core';
import { ObjectElement, isObjectElement, toValue } from '@swagger-api/apidom-core';

import mediaTypes from '../../../../media-types';
import MessageElement from '../../../../elements/Message';
Expand Down Expand Up @@ -28,7 +28,7 @@ const MessageVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
const payload = this.element.get('payload');
const schemaFormat = defaultTo(
mediaTypes.latest(),
objectElement.get('schemaFormat')?.toValue(),
toValue(objectElement.get('schemaFormat')),
);

if (mediaTypes.includes(schemaFormat) && isReferenceLikeElement(payload)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const PatternedFieldsJsonObjectVisitor = stampit(SpecificationVisitor, {
);
this.element.content.push(extensionElement);
} else if (
!this.ignoredFields.includes(key.toValue()) &&
this.fieldPatternPredicate(key.toValue())
!this.ignoredFields.includes(toValue(key)) &&
this.fieldPatternPredicate(toValue(key))
) {
const specPath = this.specPath(value);
const patternedFieldElement = this.toRefractedElement(specPath, value);
Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-ns-asyncapi-2/test/refractor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe('refractor', function () {
});

// @ts-ignore
assert.strictEqual(asyncApiElement.asyncapi.meta.get('metaKey').toValue(), 'metaValue');
assert.strictEqual(toValue(asyncApiElement.asyncapi.meta.get('metaKey')), 'metaValue');
});
});
});
Expand Down

0 comments on commit e311d44

Please sign in to comment.