mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 12:11:56 +00:00
ui: better display for UniqueBy, hashset for enums
This commit is contained in:
committed by
Keagan McClelland
parent
37c772ff8c
commit
95a0bfdd1e
@@ -1,5 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
ValueSpec, ConfigSpec, UniqueBy, ValueSpecOf, ValueType, ValueSpecObject
|
ValueSpec, ConfigSpec, UniqueBy, ValueSpecOf, ValueType, ValueSpecObject, ValueSpecUnion
|
||||||
} from './config-types'
|
} from './config-types'
|
||||||
import * as pointer from 'json-pointer'
|
import * as pointer from 'json-pointer'
|
||||||
import * as handlebars from 'handlebars'
|
import * as handlebars from 'handlebars'
|
||||||
@@ -207,7 +207,8 @@ export class ConfigCursor<T extends ValueType> {
|
|||||||
}
|
}
|
||||||
case 'enum':
|
case 'enum':
|
||||||
if (typeof cfg === 'string') {
|
if (typeof cfg === 'string') {
|
||||||
return spec.values.includes(cfg) ? null : `${cfg} is not a valid selection.`
|
spec.valuesSet = spec.valuesSet || new Set(spec.values)
|
||||||
|
return spec.valuesSet.has(cfg) ? null : `${cfg} is not a valid selection.`
|
||||||
} else {
|
} else {
|
||||||
throw new TypeError(`${this.ptr}: expected string, got ${Array.isArray(cfg) ? 'array' : typeof cfg}`)
|
throw new TypeError(`${this.ptr}: expected string, got ${Array.isArray(cfg) ? 'array' : typeof cfg}`)
|
||||||
}
|
}
|
||||||
@@ -232,7 +233,13 @@ export class ConfigCursor<T extends ValueType> {
|
|||||||
if (spec.subtype === 'enum') continue
|
if (spec.subtype === 'enum') continue
|
||||||
for (let idx2 = idx + 1; idx2 < cfg.length; idx2++) {
|
for (let idx2 = idx + 1; idx2 < cfg.length; idx2++) {
|
||||||
if (cursor.equals(this.seekNext(idx2))) {
|
if (cursor.equals(this.seekNext(idx2))) {
|
||||||
return `Item #${idx + 1} is not unique. ${JSON.stringify((cursor.cachedSpec as ValueSpecObject).uniqueBy)} must be unique.`
|
return `Item #${idx + 1} is not unique.` + ('uniqueBy' in cursor.spec()) ? `${
|
||||||
|
displayUniqueBy(
|
||||||
|
(cursor.spec() as ValueSpecObject | ValueSpecUnion).uniqueBy,
|
||||||
|
(cursor.spec() as ValueSpecObject | ValueSpecUnion),
|
||||||
|
cursor.config()
|
||||||
|
)
|
||||||
|
} must be unique.` : ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -445,4 +452,34 @@ function isEqual (uniqueBy: UniqueBy, lhs: ConfigCursor<'object'>, rhs: ConfigCu
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function displayUniqueBy(uniqueBy: UniqueBy, spec: ValueSpecObject | ValueSpecUnion, value: object): string {
|
||||||
|
if (typeof uniqueBy === 'string') {
|
||||||
|
if (spec.type === 'object') {
|
||||||
|
return spec.spec[uniqueBy].name
|
||||||
|
} else if (spec.type === 'union') {
|
||||||
|
if (uniqueBy === spec.tag.id) {
|
||||||
|
return spec.tag.name
|
||||||
|
} else {
|
||||||
|
return spec.variants[value[spec.tag.id]][uniqueBy].name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if ('any' in uniqueBy) {
|
||||||
|
return uniqueBy.any.map(uq => {
|
||||||
|
if (typeof uq === 'object' && 'all' in uq) {
|
||||||
|
return `(${displayUniqueBy(uq, spec, value)})`
|
||||||
|
} else {
|
||||||
|
return displayUniqueBy(uq, spec, value)
|
||||||
|
}
|
||||||
|
}).join(' and ')
|
||||||
|
} else if ('all' in uniqueBy) {
|
||||||
|
return uniqueBy.all.map(uq => {
|
||||||
|
if (typeof uq === 'object' && 'any' in uq) {
|
||||||
|
return `(${displayUniqueBy(uq, spec, value)})`
|
||||||
|
} else {
|
||||||
|
return displayUniqueBy(uq, spec, value)
|
||||||
|
}
|
||||||
|
}).join(' or ')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -102,6 +102,7 @@ export interface ListValueSpecNumber {
|
|||||||
|
|
||||||
export interface ListValueSpecEnum {
|
export interface ListValueSpecEnum {
|
||||||
values: string[]
|
values: string[]
|
||||||
|
valuesSet?: Set<string>
|
||||||
valueNames: { [value: string]: string }
|
valueNames: { [value: string]: string }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user