Skip to content

Commit

Permalink
feat: Support Schema type in the Launch form (#110)
Browse files Browse the repository at this point in the history
* feat: support schema as a launch input type

* test: add value test cases for schema
  • Loading branch information
schottra authored Oct 22, 2020
1 parent 191fe41 commit d5e92cd
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 216 deletions.
1 change: 0 additions & 1 deletion src/components/Launch/LaunchForm/LaunchFormInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function getComponentForInput(input: InputProps, showErrors: boolean) {
case InputType.Collection:
return <CollectionInput {...props} />;
case InputType.Map:
case InputType.Schema:
case InputType.Unknown:
case InputType.None:
return <UnsupportedInput {...props} />;
Expand Down
1 change: 1 addition & 0 deletions src/components/Launch/LaunchForm/SimpleInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const SimpleInput: React.FC<InputProps> = props => {
);
case InputType.Datetime:
return <DatetimeInput {...props} />;
case InputType.Schema:
case InputType.String:
case InputType.Integer:
case InputType.Float:
Expand Down
31 changes: 31 additions & 0 deletions src/components/Launch/LaunchForm/__mocks__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Core } from 'flyteidl';
import { BlobDimensionality } from 'models';
import { InputType, InputTypeDefinition } from '../types';

export function primitiveLiteral(primitive: Core.IPrimitive): Core.ILiteral {
return { scalar: { primitive } };
Expand All @@ -20,3 +21,33 @@ export function blobLiteral({
}
};
}

export function collectionInputTypeDefinition(
typeDefinition: InputTypeDefinition
): InputTypeDefinition {
return {
literalType: {
collectionType: typeDefinition.literalType
},
type: InputType.Collection,
subtype: typeDefinition
};
}

export function nestedCollectionInputTypeDefinition(
typeDefinition: InputTypeDefinition
): InputTypeDefinition {
return {
literalType: {
collectionType: {
collectionType: typeDefinition.literalType
}
},
type: InputType.Collection,
subtype: {
literalType: { collectionType: typeDefinition.literalType },
type: InputType.Collection,
subtype: typeDefinition
}
};
}
2 changes: 1 addition & 1 deletion src/components/Launch/LaunchForm/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const typeLabels: { [k in InputType]: string } = {
[InputType.Integer]: 'integer',
[InputType.Map]: '',
[InputType.None]: 'none',
[InputType.Schema]: 'schema',
[InputType.Schema]: 'schema - uri',
[InputType.String]: 'string',
[InputType.Struct]: 'struct',
[InputType.Unknown]: 'unknown'
Expand Down
1 change: 1 addition & 0 deletions src/components/Launch/LaunchForm/inputHelpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function literalNone(): Core.ILiteral {
export const allowedDateFormats = [ISO_8601, RFC_2822];

const primitivePath = 'scalar.primitive';
export const schemaUriPath = 'scalar.schema.uri';

/** Strings constants which can be used to perform a deep `get` on a scalar
* literal type using a primitive value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { durationHelper } from './duration';
import { floatHelper } from './float';
import { integerHelper } from './integer';
import { noneHelper } from './none';
import { schemaHelper } from './schema';
import { stringHelper } from './string';
import { InputHelper } from './types';

Expand All @@ -25,7 +26,7 @@ const inputHelpers: Record<InputType, InputHelper> = {
[InputType.Integer]: integerHelper,
[InputType.Map]: unsupportedHelper,
[InputType.None]: noneHelper,
[InputType.Schema]: unsupportedHelper,
[InputType.Schema]: schemaHelper,
[InputType.String]: stringHelper,
[InputType.Struct]: unsupportedHelper,
[InputType.Unknown]: unsupportedHelper
Expand Down
30 changes: 30 additions & 0 deletions src/components/Launch/LaunchForm/inputHelpers/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Core } from 'flyteidl';
import { InputValue } from '../types';
import { schemaUriPath } from './constants';
import { ConverterInput, InputHelper, InputValidatorParams } from './types';
import { extractLiteralWithCheck } from './utils';

function fromLiteral(literal: Core.ILiteral): InputValue {
return extractLiteralWithCheck<string>(literal, schemaUriPath);
}

function toLiteral({ typeDefinition, value }: ConverterInput): Core.ILiteral {
const uri = typeof value === 'string' ? value : value.toString();
// Note: schema type may be undefined if user is working with a generic schema.
const {
literalType: { schema: type }
} = typeDefinition;
return { scalar: { schema: { type, uri } } };
}

function validate({ value }: InputValidatorParams) {
if (typeof value !== 'string') {
throw new Error('Value is not a string');
}
}

export const schemaHelper: InputHelper = {
fromLiteral,
toLiteral,
validate
};
Loading

0 comments on commit d5e92cd

Please sign in to comment.