Skip to content

Commit a7a354a

Browse files
correct type flattening
1 parent 55ee1d5 commit a7a354a

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

src/cloud/components/Automations/actions/ActionConfigurationInput.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useMemo } from 'react'
22
import FormSelect from '../../../../design/components/molecules/Form/atoms/FormSelect'
33
import FormRowItem from '../../../../design/components/molecules/Form/templates/FormRowItem'
44
import { pickBy } from 'ramda'
5-
import { BoostAST, BoostPrimitives } from '../../../lib/automations'
5+
import { BoostAST, BoostPrimitives, BoostType } from '../../../lib/automations'
66
import { LiteralNode, RefNode } from '../../../lib/automations/ast'
77
import { StdPrimitives } from '../../../lib/automations/types'
88

@@ -18,7 +18,7 @@ interface ActionConfigurationInputProps {
1818
onChange: ActionConfigurationInputProps['onChange'],
1919
value: Extract<BoostAST, { type: 'literal' }> | null
2020
) => React.ReactNode
21-
eventDataOptions: Record<string, string>
21+
eventDataOptions: Record<string, BoostType>
2222
type: BoostPrimitives | StdPrimitives
2323
defaultValue: any
2424
}
@@ -48,7 +48,10 @@ const ActionConfigurationInput = ({
4848

4949
const options = useMemo(() => {
5050
return Object.keys(
51-
pickBy((val) => dataType == null || val === dataType, eventDataOptions)
51+
pickBy(
52+
(val) => val.type === 'primitive' && val.def === dataType,
53+
eventDataOptions
54+
)
5255
).map((key) => ({ label: key, value: key }))
5356
}, [eventDataOptions, dataType])
5457

src/cloud/components/Automations/actions/CreateDocActionConfigurator.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
RecordNode,
1111
StructNode,
1212
} from '../../../lib/automations/ast'
13-
import { flattenObj } from '../../../lib/utils/object'
13+
import { flattenType } from '../../../lib/automations/types'
1414
import { ActionConfiguratorProps } from './'
1515
import ActionConfigurationInput from './ActionConfigurationInput'
1616
import FolderSelect from './FolderSelect'
@@ -22,7 +22,12 @@ const CreateDocActionConfigurator = ({
2222
eventType,
2323
}: ActionConfiguratorProps) => {
2424
const eventDataOptions = useMemo(() => {
25-
return flattenObj(eventType as any)
25+
return Object.fromEntries(
26+
Array.from(flattenType(eventType)).map(([path, type]) => [
27+
path.join('.'),
28+
type,
29+
])
30+
)
2631
}, [eventType])
2732

2833
const constructorTree = useMemo(() => {

src/cloud/components/Automations/actions/PropertySelect.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const PropertySelect = ({
9696
data,
9797
}
9898
)
99-
) as SupportedType['val']
99+
)
100100
)
101101
}
102102
}}

src/cloud/components/Automations/actions/UpdateDocActionConfigurator.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
RecordNode,
1212
StructNode,
1313
} from '../../../lib/automations/ast'
14-
import { flattenObj } from '../../../lib/utils/object'
14+
import { flattenType } from '../../../lib/automations/types'
1515
import { ActionConfiguratorProps } from './'
1616
import ActionConfigurationInput from './ActionConfigurationInput'
1717
import PropertySelect, {
@@ -25,7 +25,12 @@ const UpdateDocActionConfigurator = ({
2525
eventType,
2626
}: ActionConfiguratorProps) => {
2727
const eventDataOptions = useMemo(() => {
28-
return flattenObj(eventType as any)
28+
return Object.fromEntries(
29+
Array.from(flattenType(eventType)).map(([path, type]) => [
30+
path.join('.'),
31+
type,
32+
])
33+
)
2934
}, [eventType])
3035

3136
const [propQueryNodes, contentNodes] = useMemo(() => {

src/cloud/lib/automations/types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,38 @@ export function Optional<T extends TypeDef<any, any>>(def: T) {
4848
export function Reference<U extends string>(def: U) {
4949
return { type: 'reference' as const, def: def as U }
5050
}
51+
52+
export function* flattenType<P extends string, U extends string | never>(
53+
typeDef: TypeDef<P, U>,
54+
internalRepr = false
55+
): Generator<[string[], TypeDef<P, U>]> {
56+
yield [[], typeDef]
57+
58+
const additionalKey = internalRepr ? ['def'] : []
59+
switch (typeDef.type) {
60+
case 'struct': {
61+
for (const [key, val] of Object.entries(typeDef.def)) {
62+
for (const [path, nestedType] of flattenType(val, internalRepr)) {
63+
yield [additionalKey.concat([key]).concat(path), nestedType]
64+
}
65+
}
66+
break
67+
}
68+
case 'record':
69+
case 'array': {
70+
for (const [path, nestedType] of flattenType(typeDef.def, internalRepr)) {
71+
yield [(internalRepr ? additionalKey : ['0']).concat(path), nestedType]
72+
}
73+
break
74+
}
75+
case 'optional': {
76+
for (const [path, nestedType] of flattenType(typeDef.def, internalRepr)) {
77+
yield [
78+
additionalKey.concat(path),
79+
nestedType.type === 'optional' ? nestedType : Optional(nestedType),
80+
]
81+
}
82+
break
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)