Skip to content

Commit

Permalink
refactor(Schema): handle strange types
Browse files Browse the repository at this point in the history
make adjustments to the way we handle unknown types and 'any'
  • Loading branch information
ramfox committed Jan 15, 2020
1 parent 870a479 commit 2c048a1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
15 changes: 12 additions & 3 deletions app/components/Structure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,23 @@ const Structure: React.FunctionComponent<StructureProps> = ({ peername, name, st

const handleWriteSchema = (row: number) => {
return (field: string) => {
return (value: string) => {
return (value: DataTypes) => {
let val = value
if (Array.isArray(value) && value.length === 1) {
val = value[0]
}
// if the value is 'any', we don't want to make
// any type assertions, let's remove 'type' from this
// row and field
if (val === 'any') {
val = undefined
}
const s = { ...structure.schema }
try {
s.items.items[row][field] = value
s.items.items[row][field] = val
} catch (e) {
throw e
}
console.log('writing')
write(peername, name, {
structure: {
...structure,
Expand Down
16 changes: 15 additions & 1 deletion app/components/structure/Schema.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as React from 'react'
import { Schema as ISchema } from '../../models/dataset'
import SchemaItem from '../item/SchemaItem'
import { typesAndDescriptions } from './TypePicker'
import { DataTypes } from '../item/DataType'

interface SchemaProps {
schema: ISchema | undefined
Expand All @@ -23,6 +25,18 @@ const Schema: React.FunctionComponent<SchemaProps> = ({

const items = schema.items.items

const typeList = typesAndDescriptions.map((el) => el.type)

const handleTypes = (types: DataTypes): DataTypes => {
// if there is an unknown type, return 'any'
if (!types ||
(typeof types === 'string' && !typeList.includes(types)) ||
(Array.isArray(types) && (types.length === 0 || types.some((el) => !typeList.includes(el))))) {
return 'any'
}
return types
}

return (
<div className='schema-wrap'>
<div className='schema-header'>
Expand All @@ -38,7 +52,7 @@ const Schema: React.FunctionComponent<SchemaProps> = ({
row={i}
onAccept={onAccept ? onAccept(i) : undefined}
title={item.title || ''}
type={item.type || 'any'}
type={handleTypes(item.type)}
description={item.description || ''}
validation={item.validation || ''}
editable={editable}
Expand Down
7 changes: 6 additions & 1 deletion app/components/structure/TypePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ const TypePicker: React.FunctionComponent<TypePickerProps> = ({
}
if (activeTab === 'multi') {
let pickedTypeList
if (pickedType.includes(picked)) {
// if they picked 'any', only 'any' should be picked
// since 'any' emcompasses all types
if (picked === 'any') {
pickedTypeList = ['any']
} else if (pickedType.includes(picked)) {
pickedTypeList = pickedType.filter((type) => type !== picked)
} else {
pickedTypeList = typesAndDescriptions.map((el: any): DataTypes => {
return el.type
}).filter((type: DataTypes) => {
if (type === 'any') return false
return type === picked || pickedType.includes(type)
})
}
Expand Down
11 changes: 10 additions & 1 deletion stories/3-Structure.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const sampleSchema: ISchema = {
{
'title': 'Spanish',
'description': 'The numerical value, in spanish',
'type': 'string'
'type': 'not a type'
},
{
'title': 'Misc',
'description': 'Can be anything'
}
],
type: 'array'
Expand Down Expand Up @@ -65,6 +69,11 @@ export const schema = () => {
)
}

schema.story = {
name: 'Schema',
parameters: { note: 'The initial schema given has 1) multiple types, 2) a single type, 3) an unknown type, and 4) no type' }
}

export const nonEditableSchema = () => {
return (
<div style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', margin: 20, width: 700 }}>
Expand Down

0 comments on commit 2c048a1

Please sign in to comment.