Skip to content

Commit

Permalink
Add util for identifying ValueTree type (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon authored Oct 18, 2022
1 parent c7c181b commit dc97706
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
5 changes: 5 additions & 0 deletions extension/src/cli/dvc/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export interface ValueTreeNode {

export type ValueTree = ValueTreeRoot | ValueTreeNode

export const isValueTree = (
value: Value | ValueTree
): value is NonNullable<ValueTree> =>
!!(value && !Array.isArray(value) && typeof value === 'object')

export enum ExperimentStatus {
FAILED = 'Failed',
QUEUED = 'Queued',
Expand Down
11 changes: 4 additions & 7 deletions extension/src/experiments/columns/collect/metricsAndParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
import { ColumnType } from '../../webview/contract'
import {
ExperimentFields,
isValueTree,
Value,
ValueTree,
ValueTreeNode,
ValueTreeOrError,
ValueTreeRoot
} from '../../../cli/dvc/contract'
Expand Down Expand Up @@ -57,17 +57,14 @@ const collectMetricOrParam = (
)
}

const notLeaf = (value: ValueTreeNode): boolean =>
value && !Array.isArray(value) && typeof value === 'object'

const walkValueTree = (
acc: ColumnAccumulator,
type: ColumnType,
tree: ValueTree,
ancestors: string[] = []
) => {
for (const [label, value] of Object.entries(tree)) {
if (notLeaf(value)) {
if (isValueTree(value)) {
walkValueTree(acc, type, value, [...ancestors, label])
} else {
collectMetricOrParam(acc, type, ancestors, label, value)
Expand Down Expand Up @@ -110,8 +107,8 @@ const collectChange = (
commitData: ExperimentFields,
ancestors: string[] = []
) => {
if (value && !Array.isArray(value) && typeof value === 'object') {
for (const [childKey, childValue] of Object.entries(value as ValueTree)) {
if (isValueTree(value)) {
for (const [childKey, childValue] of Object.entries(value)) {
collectChange(changes, type, file, childKey, childValue, commitData, [
...ancestors,
key
Expand Down
6 changes: 3 additions & 3 deletions extension/src/experiments/model/modify/collect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Value, ValueTree } from '../../../cli/dvc/contract'
import { isValueTree, Value, ValueTree } from '../../../cli/dvc/contract'
import { appendColumnToPath } from '../../columns/paths'
import { MetricOrParamColumns } from '../../webview/contract'

Expand All @@ -15,8 +15,8 @@ const collectFromParamsFile = (
) => {
const pathArray = [...ancestors, key].filter(Boolean) as string[]

if (!Array.isArray(value) && typeof value === 'object') {
for (const [childKey, childValue] of Object.entries(value as ValueTree)) {
if (isValueTree(value)) {
for (const [childKey, childValue] of Object.entries(value)) {
collectFromParamsFile(acc, childKey, childValue, pathArray)
}
return
Expand Down
5 changes: 3 additions & 2 deletions extension/src/plots/model/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ExperimentsBranchOutput,
ExperimentsOutput,
ExperimentStatus,
isValueTree,
PlotsOutput,
Value,
ValueTree
Expand Down Expand Up @@ -58,8 +59,8 @@ const collectFromMetricsFile = (
) => {
const pathArray = [...ancestors, key].filter(Boolean) as string[]

if (typeof value === 'object') {
for (const [childKey, childValue] of Object.entries(value as ValueTree)) {
if (isValueTree(value)) {
for (const [childKey, childValue] of Object.entries(value)) {
collectFromMetricsFile(
acc,
name,
Expand Down

0 comments on commit dc97706

Please sign in to comment.