Fix field helper function to be javascript
parent
8324e13670
commit
14f7b16f50
|
@ -1,5 +1,9 @@
|
|||
import _ from 'lodash'
|
||||
j
|
||||
|
||||
// fieldWalk traverses fields rescursively into args
|
||||
export const fieldWalk = (fields, fn) =>
|
||||
fields.each(f => _.concat(fn(f), fieldWalk(_.get(f, 'args', []), fn)))
|
||||
|
||||
// functions returns all top-level fields with type
|
||||
export const ofType = (fields, type) =>
|
||||
_.filter(fields, f => _.get(f, 'type') === type)
|
||||
|
@ -11,19 +15,16 @@ export const functions = fields => ofType(fields, 'func')
|
|||
export const numFunctions = fields => _.size(functions(fields))
|
||||
|
||||
// functionNames returns the names of all top-level functions
|
||||
export const functionNames = fields => _.map(functions(fields), f => f.name)
|
||||
|
||||
// firstFieldName returns the name of the first of type field
|
||||
export const firstFieldName = fields => _.head(fieldNamesDeep(fields))
|
||||
export const functionNames = fields => functions(fields).map(f => f.name)
|
||||
|
||||
// getFields returns all of the top-level fields of type field
|
||||
export const getFields = fields => ofType(fields, 'field')
|
||||
|
||||
export const fieldsDeep = fields =>
|
||||
_.uniqBy(walk(fields, f => getFields(f)), 'name')
|
||||
_.uniqBy(fieldWalk(fields, f => getFields(f)), 'name')
|
||||
|
||||
export const fieldNamesDeep = fields =>
|
||||
_.map(fieldsDeep(fields), f => _.get(f, 'name'))
|
||||
fieldsDeep(fields).map(f => _.get(f, 'name'))
|
||||
|
||||
export const walk = (fields, fn) =>
|
||||
_.foreach(fields, f => _.concat(fn(f), walk(_.get(f, 'args', []), fn)))
|
||||
// firstFieldName returns the name of the first of type field
|
||||
export const firstFieldName = fields => _.head(fieldNamesDeep(fields))
|
||||
|
|
|
@ -70,16 +70,18 @@ export function buildSelectStatement(config) {
|
|||
}
|
||||
|
||||
function _buildFields(fieldFuncs) {
|
||||
if (!!fieldFuncs) {
|
||||
if (fieldFuncs) {
|
||||
return fieldFuncs
|
||||
.map(f => {
|
||||
switch (f.type) {
|
||||
case 'field':
|
||||
case 'field': {
|
||||
return f.name === '*' ? '*' : `"${f.name}"`
|
||||
case 'func':
|
||||
}
|
||||
case 'func': {
|
||||
const args = _buildFields(f.args)
|
||||
const alias = f.alias ? ` AS "${f.alias}"` : ''
|
||||
return `${f.name}(${args})${alias}`
|
||||
}
|
||||
}
|
||||
})
|
||||
.join(', ')
|
||||
|
|
Loading…
Reference in New Issue