Skip to content

Commit

Permalink
fix: jmespathQueryLanguage not working with non-native JSON data type…
Browse files Browse the repository at this point in the history
…s like LosslessNumber
  • Loading branch information
josdejong committed Feb 22, 2023
1 parent 09f2575 commit e2c8e3d
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ interface QueryLanguage {
name: string
description: string
createQuery: (json: JSONValue, queryOptions: QueryLanguageOptions) => string
executeQuery: (json: JSONValue, query: string) => JSONValue
executeQuery: (json: JSONValue, query: string, parser: JSONParser) => JSONValue
}

interface QueryLanguageOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/controls/JSONPreview.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import type { JSONParser } from '$lib/types'
import type { JSONValue } from 'lossless-json'
import { truncate } from '$lib/utils/stringUtils.js'
import { getText } from '$lib/utils/jsonUtils.js'
import { MAX_CHARACTERS_TEXT_PREVIEW } from '$lib/constants.js'
import type { JSONValue } from 'immutable-json-patch'
export let text: string | undefined
export let json: JSONValue | undefined
Expand Down
15 changes: 10 additions & 5 deletions src/lib/components/modals/TransformModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
: getSelectedQueryLanguage(queryLanguageId).createQuery(json, state.queryOptions || {})
let isManual = state.isManual || false
let previewError = undefined
let previewError: string | undefined = undefined
let previewContent: Content = { text: '' }
function getSelectedQueryLanguage(queryLanguageId: string): QueryLanguage {
Expand All @@ -98,13 +98,17 @@
debug('previewTransform', {
query
})
const jsonTransformed = getSelectedQueryLanguage(queryLanguageId).executeQuery(json, query)
const jsonTransformed = getSelectedQueryLanguage(queryLanguageId).executeQuery(
json,
query,
parser
)
previewContent = { json: jsonTransformed }
previewError = undefined
} catch (err) {
previewContent = { text: '' }
previewError = err
previewError = err.toString()
}
}
Expand Down Expand Up @@ -132,7 +136,8 @@
debug('handleTransform', { query })
const jsonTransformed = getSelectedQueryLanguage(queryLanguageId).executeQuery(
selectedJson,
query
query,
parser
)
onTransform([
Expand Down Expand Up @@ -308,7 +313,7 @@
/>
{:else}
<div class="jse-preview jse-error">
{previewError.toString()}
{previewError}
</div>
{/if}
</div>
Expand Down
5 changes: 2 additions & 3 deletions src/lib/components/modes/tablemode/TableModeWelcome.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script lang="ts">
import type { JSONValue } from 'lossless-json'
import type { JSONPath, JSONValue } from 'immutable-json-patch'
import { getIn, isJSONArray, isJSONObject } from 'immutable-json-patch'
import type { JSONParser, OnChangeMode } from '$lib/types'
import { Mode } from '$lib/types'
import { valueType } from '$lib/utils/typeUtils'
import { findNestedArrays } from '$lib/logic/table'
import type { JSONPath } from 'immutable-json-patch'
import { getIn, isJSONArray, isJSONObject } from 'immutable-json-patch'
import { isEmpty } from 'lodash-es'
import { stringifyJSONPath, stripRootObject } from '$lib/utils/pathUtils.js'
Expand Down
4 changes: 1 addition & 3 deletions src/lib/components/modes/tablemode/tag/InlineValue.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script lang="ts">
import type { JSONArray, JSONObject } from 'lossless-json'
import { isJSONArray } from 'immutable-json-patch'
import type { JSONPath } from 'immutable-json-patch'
import type { isJSONArray, JSONArray, JSONObject, JSONPath } from 'immutable-json-patch'
import type { JSONParser } from '$lib/types'
import { truncate } from '$lib/utils/stringUtils.js'
import { MAX_INLINE_OBJECT_CHARS } from '$lib/constants.js'
Expand Down
2 changes: 1 addition & 1 deletion src/lib/logic/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type {
OnPatch,
OnSelect
} from '$lib/types'
import type { JSONValue } from 'lossless-json'
import { createDebug } from '$lib/utils/debug'
import {
getIn,
Expand All @@ -38,6 +37,7 @@ import {
isJSONPatchReplace,
type JSONArray,
type JSONPath,
type JSONValue,
parsePath
} from 'immutable-json-patch'
import { isObject, isObjectOrArray } from '$lib/utils/typeUtils'
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logic/selection.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, describe } from 'vitest'
import { describe, test } from 'vitest'
import assert from 'assert'
import {
createAfterSelection,
Expand All @@ -20,7 +20,7 @@ import {
} from './selection.js'
import { createDocumentState } from './documentState.js'
import { type DocumentState, type JSONSelection, SelectionType } from '../types.js'
import type { JSONValue } from 'lossless-json'
import type { JSONValue } from 'immutable-json-patch'

describe('selection', () => {
const json = {
Expand Down
49 changes: 34 additions & 15 deletions src/lib/plugins/query/javascriptQueryLanguage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { test, describe } from 'vitest'
import assert from 'assert'
import { javascriptQueryLanguage } from './javascriptQueryLanguage.js'
import { cloneDeep } from 'lodash-es'
import { LosslessNumber } from 'lossless-json'
import type { JSONValue } from 'immutable-json-patch'

const { createQuery, executeQuery } = javascriptQueryLanguage

Expand All @@ -16,7 +18,7 @@ describe('javascriptQueryLanguage', () => {
describe('createQuery and executeQuery', () => {
test('should create a and execute an empty query', () => {
const query = createQuery(users, {})
const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(query, 'function query (data) {\n return data\n}')
assert.deepStrictEqual(result, users)
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
Expand All @@ -38,7 +40,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, [user2])
assert.deepStrictEqual(users, originalUsers) // must not touch the original data
})
Expand All @@ -62,7 +64,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(data, query)
const result = executeQuery(data, query, JSON)
assert.deepStrictEqual(result, [{ 'user name!': 'Bob' }])
assert.deepStrictEqual(data, originalData) // must not touch the original data
})
Expand All @@ -85,7 +87,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(data, query)
const result = executeQuery(data, query, JSON)
assert.deepStrictEqual(result, [1])
assert.deepStrictEqual(data, originalData) // must not touch the original data
})
Expand All @@ -106,7 +108,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, [user1, user2])
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
})
Expand All @@ -127,7 +129,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, [user2])
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
})
Expand All @@ -148,7 +150,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, [user2])
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
})
Expand All @@ -173,7 +175,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, [user1, user2, user3])
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
})
Expand All @@ -198,7 +200,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, [user3, user2, user1])
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
})
Expand All @@ -218,7 +220,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, ['Stuart', 'Kevin', 'Bob'])
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
})
Expand All @@ -241,7 +243,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, [
{ name: 'Stuart', _id: '1' },
{ name: 'Kevin', _id: '3' },
Expand Down Expand Up @@ -281,7 +283,7 @@ describe('javascriptQueryLanguage', () => {
'}'
)

const result = executeQuery(users, query)
const result = executeQuery(users, query, JSON)
assert.deepStrictEqual(result, ['Bob', 'Stuart'])
assert.deepStrictEqual(users, originalUsers) // must not touch the original users
})
Expand All @@ -295,23 +297,40 @@ describe('javascriptQueryLanguage', () => {
' return square(data)\n' +
'};'
const data = 4
const result = executeQuery(data, query)
const result = executeQuery(data, query, JSON)
assert.deepStrictEqual(result, 16)
})

test('should work with alternative parsers and non-native JSON data types', () => {
const data = [new LosslessNumber('4'), new LosslessNumber('7'), new LosslessNumber('5')]
const query = createQuery(data as unknown as JSONValue, {
sort: {
path: [],
direction: 'asc'
}
})

const result = executeQuery(data as unknown as JSONValue, query, JSON)
assert.deepStrictEqual(result, [
new LosslessNumber('4'),
new LosslessNumber('5'),
new LosslessNumber('7')
])
})

test('should throw an exception when function query is not defined in the query', () => {
assert.throws(() => {
const query = 'function test (data) { return 42 }'
const data = {}

executeQuery(data, query)
executeQuery(data, query, JSON)
}, /Error: Cannot execute query: expecting a function named 'query' but is undefined/)
})

test('should return null when property is not found', () => {
const query = 'function query (data) {\n' + ' return data.foo\n' + '}'
const data = {}
const result = executeQuery(data, query)
const result = executeQuery(data, query, JSON)
assert.deepStrictEqual(result, null)
})
})
Expand Down
Loading

0 comments on commit e2c8e3d

Please sign in to comment.