Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util for identifying ValueTree type #2619

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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