Skip to content

Commit

Permalink
chore(core): allow empty nested flux template
Browse files Browse the repository at this point in the history
  • Loading branch information
sranka committed Sep 9, 2020
1 parent da64b54 commit 4de106c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
18 changes: 15 additions & 3 deletions packages/core/src/query/flux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class FluxParameter implements FluxParameterLike, ParameterizedQuery {
}
}

/**
* Checks if the supplied object is FluxParameterLike
* @param value - any value
* @returns true if it is
*/
function isFluxParameterLike(value: any): boolean {
return typeof value === 'object' && typeof value[FLUX_VALUE] === 'function'
}

/**
* Escapes content of the supplied string so it can be wrapped into double qoutes
* to become a [flux string literal](https://docs.influxdata.com/flux/v0.65/language/lexical-elements/#string-literals).
Expand Down Expand Up @@ -243,9 +252,12 @@ export function flux(
} else {
sanitized = toFluxValue(val)
if (sanitized === '') {
throw new Error(
`Unsupported parameter literal '${val}' at index: ${i}, type: ${typeof val}`
)
// do not allow to insert empty strings, unless it is FluxParameterLike
if (!isFluxParameterLike(val)) {
throw new Error(
`Unsupported parameter literal '${val}' at index: ${i}, type: ${typeof val}`
)
}
}
}
parts[partIndex++] = sanitized
Expand Down
28 changes: 25 additions & 3 deletions packages/core/test/unit/query/flux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ describe('Flux Tagged Template', () => {
'from(bucket:"my-bucket") |> range(start: 0) |> filter(fn: (r) => r._measurement == "temperature")'
)
})
it('interpolates a wrapped string', () => {
expect(flux`from(bucket:"${'my-bucket'}")`.toString()).equals(
'from(bucket:"my-bucket")'
)
})
it('fails on undefined', () => {
try {
flux`${undefined}`
Expand All @@ -156,6 +161,19 @@ describe('Flux Tagged Template', () => {
// ok expected, undefined is not supported
}
})
it('fails on empty toString', () => {
try {
const x = {
toString(): string {
return ''
},
}
flux`${x}`
expect.fail()
} catch (_e) {
// ok expected, undefined is not supported
}
})
it('fails on wrong usage of template', () => {
try {
flux((['1', '2'] as any) as TemplateStringsArray)
Expand All @@ -166,17 +184,21 @@ describe('Flux Tagged Template', () => {
})

it('processes a simple nested flux template', () => {
// nested flux templates
const flux1 = flux`from(bucket:"my-bucket")`
expect(flux`${flux1} |> range(start: ${0})")`.toString()).equals(
'from(bucket:"my-bucket") |> range(start: 0)")'
)
})
it('processes a simple nested flux template', () => {
// nested flux templates
it('processes a parameterized nested flux template', () => {
const flux1 = flux`from(bucket:${'my-bucket'})`
expect(flux`${flux1} |> range(start: ${0})")`.toString()).equals(
'from(bucket:"my-bucket") |> range(start: 0)")'
)
})
it('processes an empty nested flux template', () => {
const empty = flux``
expect(flux`from(bucket:"my-bucket")${empty}`.toString()).equals(
'from(bucket:"my-bucket")'
)
})
})

0 comments on commit 4de106c

Please sign in to comment.