Replace get with getdeep to add types and refactor for clarity

pull/10616/head
ebb-tide 2018-06-01 12:19:41 -07:00
parent fc3e2bc091
commit 189ff6ae0a
3 changed files with 28 additions and 19 deletions

View File

@ -1,4 +1,5 @@
import React, {PureComponent} from 'react' import React, {PureComponent} from 'react'
import uuid from 'uuid'
import _ from 'lodash' import _ from 'lodash'
import {Func} from 'src/types/ifql' import {Func} from 'src/types/ifql'
@ -6,7 +7,7 @@ import {funcNames} from 'src/ifql/constants'
import Filter from 'src/ifql/components/Filter' import Filter from 'src/ifql/components/Filter'
import FilterPreview from 'src/ifql/components/FilterPreview' import FilterPreview from 'src/ifql/components/FilterPreview'
import uuid from 'uuid' import {getDeep} from 'src/utils/wrappers'
interface Props { interface Props {
func: Func func: Func
@ -26,7 +27,7 @@ export default class FuncArgsPreview extends PureComponent<Props> {
} }
if (func.name === funcNames.FILTER) { if (func.name === funcNames.FILTER) {
const value = _.get(args, '0.value', '') const value = getDeep<string>(args, '0.value', '')
if (!value) { if (!value) {
return this.colorizedArguments return this.colorizedArguments
} }
@ -52,10 +53,9 @@ export default class FuncArgsPreview extends PureComponent<Props> {
const separator = i === 0 ? null : ', ' const separator = i === 0 ? null : ', '
let argValue let argValue
if (arg.type === 'object') { if (arg.type === 'object') {
const valueMap = _.map(arg.value, (value, key) => `${key}:${value}`) const valueMap = _.map(arg.value, (value, key) => `${key}:${value}`)
argValue = '{' + valueMap.join(', ') + '}' argValue = `{${valueMap.join(', ')}}`
} else { } else {
argValue = `${arg.value}` argValue = `${arg.value}`
} }

View File

@ -4,8 +4,9 @@ import _ from 'lodash'
import Dropdown from 'src/shared/components/Dropdown' import Dropdown from 'src/shared/components/Dropdown'
import FuncArgInput from 'src/ifql/components/FuncArgInput' import FuncArgInput from 'src/ifql/components/FuncArgInput'
import FuncArgTextArea from 'src/ifql/components/FuncArgTextArea' import FuncArgTextArea from 'src/ifql/components/FuncArgTextArea'
import {getDeep} from 'src/utils/wrappers'
import {OnChangeArg, Func} from 'src/types/ifql' import {OnChangeArg, Func, Arg} from 'src/types/ifql'
import {argTypes} from 'src/ifql/constants' import {argTypes} from 'src/ifql/constants'
interface Props { interface Props {
@ -22,7 +23,7 @@ interface DropdownItem {
} }
class Join extends PureComponent<Props> { class Join extends PureComponent<Props> {
constructor(props) { constructor(props: Props) {
super(props) super(props)
} }
@ -37,7 +38,7 @@ class Join extends PureComponent<Props> {
return ( return (
<> <>
<div className="func-arg"> <div className="func-arg">
<label className="func-arg--label">{'tables'}</label> <label className="func-arg--label">tables</label>
<Dropdown <Dropdown
selected={this.table1Value} selected={this.table1Value}
className="from--dropdown dropdown-100 func-arg--value" className="from--dropdown dropdown-100 func-arg--value"
@ -91,7 +92,7 @@ class Join extends PureComponent<Props> {
this.handleChooseTables(this.table1Value, item.text) this.handleChooseTables(this.table1Value, item.text)
} }
private handleChooseTables = (table1, table2): void => { private handleChooseTables = (table1: string, table2: string): void => {
const { const {
onChangeArg, onChangeArg,
bodyID, bodyID,
@ -115,30 +116,36 @@ class Join extends PureComponent<Props> {
return this.props.declarationsFromBody.map(d => ({text: d})) return this.props.declarationsFromBody.map(d => ({text: d}))
} }
private get argsArray(): Arg[] {
const {func} = this.props
return getDeep<Arg[]>(func, 'args', [])
}
private get onValue(): string { private get onValue(): string {
const onObject = this.props.func.args.find(a => a.key === 'on') const onObject = this.argsArray.find(a => a.key === 'on')
return onObject.value.toString() return onObject.value.toString()
} }
private get fnValue(): string { private get fnValue(): string {
const fnObject = this.props.func.args.find(a => a.key === 'fn') const fnObject = this.argsArray.find(a => a.key === 'fn')
return fnObject.value.toString() return fnObject.value.toString()
} }
private get table1Value(): string { private get table1Value(): string {
const tables = this.props.func.args.find(a => a.key === 'tables') const tables = this.argsArray.find(a => a.key === 'tables')
if (tables) { if (tables) {
const keys = _.keys(tables.value) const keys = _.keys(tables.value)
return _.get(keys, '0', '') return getDeep<string>(keys, '0', '')
} }
return '' return ''
} }
private get table2Value(): string { private get table2Value(): string {
const tables = this.props.func.args.find(a => a.key === 'tables') const tables = this.argsArray.find(a => a.key === 'tables')
if (tables) { if (tables) {
const keys = _.keys(tables.value) const keys = _.keys(tables.value)
return _.get(keys, '1', _.get(keys, '0', '')) return getDeep<string>(keys, '1', getDeep<string>(keys, '0', ''))
} }
return '' return ''
} }

View File

@ -1,15 +1,16 @@
import uuid from 'uuid' import uuid from 'uuid'
import _ from 'lodash'
import Walker from 'src/ifql/ast/walker' import Walker from 'src/ifql/ast/walker'
import {funcNames} from 'src/ifql/constants' import {funcNames} from 'src/ifql/constants'
import {getDeep} from 'src/utils/wrappers'
import {FlatBody, Func} from 'src/types/ifql' import {FlatBody, Func, Suggestion} from 'src/types/ifql'
interface Body extends FlatBody { interface Body extends FlatBody {
id: string id: string
} }
export const bodyNodes = (ast, suggestions): Body[] => { export const bodyNodes = (ast, suggestions: Suggestion[]): Body[] => {
if (!ast) { if (!ast) {
return [] return []
} }
@ -63,7 +64,7 @@ export const bodyNodes = (ast, suggestions): Body[] => {
return body return body
} }
const functions = (funcs, suggestions): Func[] => { const functions = (funcs: Func[], suggestions: Suggestion[]): Func[] => {
const funcList = funcs.map(func => { const funcList = funcs.map(func => {
const suggestion = suggestions.find(f => f.name === func.name) const suggestion = suggestions.find(f => f.name === func.name)
if (!suggestion) { if (!suggestion) {
@ -77,7 +78,8 @@ const functions = (funcs, suggestions): Func[] => {
const {params, name} = suggestion const {params, name} = suggestion
const args = Object.entries(params).map(([key, type]) => { const args = Object.entries(params).map(([key, type]) => {
const value = _.get(func.args.find(arg => arg.key === key), 'value', '') const argWithKey = func.args.find(arg => arg.key === key)
const value = getDeep<string>(argWithKey, 'value', '')
return { return {
key, key,