Skip to content

Commit

Permalink
[form-builder] Fix issue with recursive string field lookup for refer…
Browse files Browse the repository at this point in the history
…ence search
  • Loading branch information
bjoerge committed Oct 25, 2017
1 parent dcc39fd commit cc359d8
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,42 @@ function wrapIn(chars = '') {

const wrapInParens = wrapIn('()')

const stringFields = Symbol('__cachedStringFields')
const stringFieldsSymbol = Symbol('__cachedStringFields')

function getCachedStringFieldPaths(type, maxDepth) {
if (!type[stringFields]) {
type[stringFields] = getStringFieldPaths(type, maxDepth)
if (!type[stringFieldsSymbol]) {
type[stringFieldsSymbol] = getStringFieldPaths(type, maxDepth)
}
return type[stringFields]
return type[stringFieldsSymbol]
}

function getStringFieldPaths(type, maxDepth = 2) {
function reduceType(type, reducer, accumulator, path = [], maxDepth = 10) {
if (maxDepth < 0) {
return []
return accumulator
}
return (type.fields || [])
.map(field => {
if (field.type.jsonType === 'string') {
return [[field.name]]
}
if (field.type.jsonType === 'object') {
return getCachedStringFieldPaths(field.type, maxDepth - 1).map(path => [field.name, ...flatten(path)])
}
return null
})
.filter(Boolean)
if (Array.isArray(type.fields)) {
return type.fields.reduce(
(acc, field, index) => reduceType(field.type, reducer, acc, path.concat(field.name), maxDepth - 1),
reducer(accumulator, type, path)
)
}
return reducer(accumulator, type, path)
}

function getStringFieldPaths(type, maxDepth) {
const reducer = (accumulator, childType, path) =>
(childType.jsonType === 'string'
? [...accumulator, path]
: accumulator
)

return reduceType(type, reducer, [], [], maxDepth)
}

function buildConstraintFromType(type, terms) {
const typeConstraint = `_type == '${type.name}'`

const stringFieldPaths = flatten(getCachedStringFieldPaths(type, 2))
const stringFieldPaths = getCachedStringFieldPaths(type, 4)
if (stringFieldPaths.length === 0) {
return typeConstraint
}
Expand Down

0 comments on commit cc359d8

Please sign in to comment.