Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/private/json-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export class JsonPathToken implements IResolvable {
Object.defineProperty(this, JSON_PATH_TOKEN_SYMBOL, { value: true });
}

public resolve(_ctx: IResolveContext): any {
public resolve(_ctx: IResolveContext): string {
return this.path;
}

public toString() {
public toString(): string {
return Token.asString(this, { displayHint: this.displayHint });
}

public toJSON() {
public toJSON(): string {
return `<path:${this.path}>`;
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ export function jsonPathString(x: string): string | undefined {
return undefined;
}

export function jsonPathFromAny(x: any) {
export function jsonPathFromAny(x: any): string | undefined {
if (!x) { return undefined; }
if (typeof x === 'string') { return jsonPathString(x); }
return pathFromToken(Tokenization.reverse(x));
Expand All @@ -314,7 +314,7 @@ function jsonPathNumber(x: number): string | undefined {
return pathFromToken(Tokenization.reverseNumber(x));
}

function pathFromToken(token: IResolvable | undefined) {
function pathFromToken(token: IResolvable | undefined): string | undefined {
return token && (JsonPathToken.isJsonPathToken(token) ? token.path : undefined);
}

Expand All @@ -329,15 +329,15 @@ function pathFromToken(token: IResolvable | undefined) {
* Call this function whenever you're building compound JSONPath expressions, in
* order to avoid having tokens-in-tokens-in-tokens which become very hard to parse.
*/
export function renderInExpression(x: any) {
export function renderInExpression(x: any): string {
const path = jsonPathFromAny(x);
if (path) return path;
if (typeof x === 'number') return x.toString(10);
if (typeof x === 'string') return singleQuotestring(x);
throw new UnscopedValidationError('Unxexpected value.');
}

function singleQuotestring(x: string) {
function singleQuotestring(x: string): string {
const ret = new Array<string>();
ret.push("'");
for (let i = 0; i < x.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,94 @@
import { Token, Tokenization, UnscopedValidationError } from '../../../core';
import { JsonPath } from '../../lib';
import { renderInExpression } from '../../lib/private/json-path';
import { jsonPathFromAny, jsonPathString, JsonPathToken, renderInExpression } from '../../lib/private/json-path';

describe('JsonPathToken', () => {
describe('constructor', () => {
test('should create instance with path', () => {
const token = new JsonPathToken('$.foo.bar');
expect(token.path).toBe('$.foo.bar');
});
test('should set displayHint correctly', () => {
const token = new JsonPathToken('$.foo.bar');
expect(token.displayHint).toBe('foo.bar');
});
test('should remove leading non-alphabetic characters', () => {
const token = new JsonPathToken('$.123foo.bar');
expect(token.displayHint).toBe('foo.bar');
});
test('should handle path with only special characters', () => {
const token = new JsonPathToken('$.');
expect(token.displayHint).toBe('');
});
});

describe('isJsonPathToken', () => {
test('should return true for JsonPathToken instances', () => {
const token = new JsonPathToken('$.foo.bar');
expect(JsonPathToken.isJsonPathToken(token)).toBe(true);
});

test('should return false for non-JsonPathToken objects', () => {
const notToken = {} as IResolvable;
expect(JsonPathToken.isJsonPathToken(notToken)).toBe(false);
});
});

describe('resolve', () => {
test('should return the path', () => {
const token = new JsonPathToken('$.foo.bar');
const mockContext = {} as IResolveContext;
expect(token.resolve(mockContext)).toBe('$.foo.bar');
});
});

describe('toString', () => {
test('should return the path', () => {
const token = new JsonPathToken('$.foo.bar');
expect(token.toString()).toMatch(/^\${Token\[foo\.bar\.\d+\]}$/);
});
});

describe('toJSON', () => {
test('should return formatted string', () => {
const token = new JsonPathToken('$.foo.bar');
expect(token.toJSON()).toBe('<path:$.foo.bar>');
});
});
});

describe('jsonPathString', () => {
test('should return path for jsonPathToken instances', () => {
const token = new JsonPathToken('$.foo.bar');
expect(jsonPathString(token.toString())).toBe('$.foo.bar');
});
test('sholud return undefined for non-jsonPathToken objects', () => {
const notToken = '$.foo.bar';
expect(jsonPathString(notToken)).toBeUndefined();
});
test('should return path for for concatenated jsonPathToken tokens', () => {
const token = `${Token.asString(new JsonPathToken('$.foo'))}-${new JsonPathToken('$.bar')}`;
expect(() => jsonPathString(token)).toThrow(UnscopedValidationError);
});
});

describe('jsonPathFromAny', () => {
test('should return path for jsonPathToken', () => {
const token = new JsonPathToken('$.foo.bar');
expect(jsonPathFromAny(token)).toBe('$.foo.bar');
});
test('should return undefined for non-jsonPathToken objects', () => {
const notToken = {};
expect(jsonPathFromAny(notToken)).toBeUndefined();
});
test('should return path for falsy objects', () => {
expect(jsonPathFromAny('')).toBeUndefined();
});
test('should return path for jsonPathToken instances', () => {
const token = new JsonPathToken('$.foo.bar');
expect(jsonPathFromAny(token.toString())).toBe('$.foo.bar');
});
});

describe('RenderInExpression', () => {
test('simple number', () => {
Expand Down
Loading