-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathutils.ts
88 lines (83 loc) · 2.63 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Core from '@clients/common/flyteidl/core';
import get from 'lodash/get';
import has from 'lodash/has';
import { assertNever } from '../../../../common/utils';
import { BlobDimensionality } from '../../../../models/Common/types';
import { InputType, InputTypeDefinition } from '../types';
/** Performs a deep get of `path` on the given `Core.ILiteral`. Will throw
* if the given property doesn't exist.
*/
export function extractLiteralWithCheck<T>(literal: Core.ILiteral, path: string): T {
if (!has(literal, path)) {
throw new Error(`Failed to extract literal value with path ${path}`);
}
return get(literal, path) as T;
}
export function formatParameterValues(type: InputType, value: any) {
if (value === undefined) {
return '';
}
if (type === InputType.Struct) {
return JSON.stringify(value, null, 2);
}
return JSON.stringify(value, null, 0).split(',').join(', ');
}
/** Determines if a given input type, including all levels of nested types, is
* supported for use in the Launch form.
*/
export function typeIsSupported(typeDefinition: InputTypeDefinition): boolean {
const { type, subtype, listOfSubTypes } = typeDefinition;
switch (type) {
case InputType.Binary:
case InputType.Error:
case InputType.Unknown:
return false;
case InputType.None:
case InputType.Boolean:
case InputType.Blob:
case InputType.Datetime:
case InputType.Duration:
case InputType.Enum:
case InputType.Float:
case InputType.Integer:
case InputType.Schema:
case InputType.String:
case InputType.Struct:
case InputType.StructuredDataset:
return true;
case InputType.Union:
if (listOfSubTypes?.length) {
let isSupported = true;
listOfSubTypes.forEach((subtype) => {
if (!typeIsSupported(subtype)) {
isSupported = false;
}
});
return isSupported;
}
return false;
case InputType.Map:
if (!subtype) {
return false;
}
return typeIsSupported(subtype);
case InputType.Collection: {
if (!subtype) {
console.error('Unexpected missing subtype for collection input', typeDefinition);
return false;
}
return typeIsSupported(subtype);
}
default:
// This will cause a compiler error if new types are added and there is
// no case for them listed above.
assertNever(type, { noThrow: true });
// @ts-ignore
return false;
}
}
export function isKeyOfBlobDimensionality(
value: string | number | symbol,
): value is keyof typeof BlobDimensionality {
return Object.keys(BlobDimensionality).indexOf(value as any) >= 0;
}