Skip to content

Commit

Permalink
fix: define type aliases for some map keys (#455)
Browse files Browse the repository at this point in the history
Fixes #454
  • Loading branch information
chrispcampbell authored Mar 25, 2024
1 parent a9d2e34 commit 65560ef
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
10 changes: 5 additions & 5 deletions examples/sample-check-bundle/src/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2022 Climate Interactive / New Venture Fund

import type { InputId, InputVar, VarId } from '@sdeverywhere/check-core'
import type { InputAliasName, InputGroupName, InputId, InputVar, VarId } from '@sdeverywhere/check-core'

export interface Inputs {
inputVars: Map<VarId, InputVar>
inputGroups: Map<string, InputVar[]>
inputAliases: Map<string, VarId>
inputGroups: Map<InputGroupName, InputVar[]>
inputAliases: Map<InputAliasName, VarId>
}

/**
* Gather the list of input variables used in this version of the model.
*/
export function getInputs(modelVersion: number): Inputs {
const inputVars: Map<VarId, InputVar> = new Map()
const inputAliases: Map<string, VarId> = new Map()
const inputAliases: Map<InputAliasName, VarId> = new Map()

// TODO: Typically you would return the actual list of model inputs (for
// example, the list used to configure an SDEverywhere-generated model),
Expand Down Expand Up @@ -58,7 +58,7 @@ export function getInputs(modelVersion: number): Inputs {
}

// Configure input groups
const inputGroups: Map<string, InputVar[]> = new Map([
const inputGroups: Map<InputGroupName, InputVar[]> = new Map([
['All Inputs', [...inputVars.values()]],
// ['Input Group 1', [inputVars.get('_input_a'), inputVars.get('_input_b')]],
['Input Group 1', [inputVars.get('_input_a')]],
Expand Down
5 changes: 3 additions & 2 deletions examples/sample-check-bundle/src/outputs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) 2022 Climate Interactive / New Venture Fund

import type { DatasetGroupName } from '@sdeverywhere/check-core'
import type { DatasetKey, ImplVar, OutputVar, RelatedItem, VarId } from '@sdeverywhere/check-core'

export interface Outputs {
outputVars: Map<DatasetKey, OutputVar>
implVars: Map<DatasetKey, ImplVar>
datasetGroups: Map<string, DatasetKey[]>
datasetGroups: Map<DatasetGroupName, DatasetKey[]>
}

/**
Expand Down Expand Up @@ -99,7 +100,7 @@ export function getOutputs(modelVersion: number): Outputs {
const keysForVarsWithSource = (sourceName?: string) => {
return [...outputVars.entries()].filter(e => e[1].sourceName === sourceName).map(([k]) => k)
}
const datasetGroups: Map<string, DatasetKey[]> = new Map([
const datasetGroups: Map<DatasetGroupName, DatasetKey[]> = new Map([
['All Outputs', keysForVarsWithSource(undefined)],
['Basic Outputs', [keyForVarWithName('Output X'), keyForVarWithName('Output Y'), keyForVarWithName('Output Z')]],
['Static', keysForVarsWithSource('StaticData')]
Expand Down
16 changes: 13 additions & 3 deletions packages/check-core/src/bundle/bundle-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import type { DatasetKey, VarId } from '../_shared/types'

import type { ImplVar, InputVar, OutputVar } from './var-types'

/** The human-readable name for a group of inputs. */
export type InputGroupName = string

/** The alias name for an input. */
export type InputAliasName = string

/** The human-readable name for a group of dataset. */
export type DatasetGroupName = string

/**
* Includes the properties needed to display a legend item in the UI.
*/
Expand All @@ -28,6 +37,7 @@ export interface LinkItem {
content: string
}

/** The identifier for a bundle-specific graph. */
export type BundleGraphId = string

/**
Expand Down Expand Up @@ -93,11 +103,11 @@ export interface ModelSpec {
/** The map of all variables (both internal and exported) in this version of the model. */
implVars: Map<DatasetKey, ImplVar>
/** The custom input variable aliases defined for this model. */
inputAliases?: Map<string, VarId>
inputAliases?: Map<InputAliasName, VarId>
/** The custom input variable groups defined for this model. */
inputGroups?: Map<string, InputVar[]>
inputGroups?: Map<InputGroupName, InputVar[]>
/** The custom dataset (output variable) groups defined for this model. */
datasetGroups?: Map<string, DatasetKey[]>
datasetGroups?: Map<DatasetGroupName, DatasetKey[]>
/** The start time (year) for the model. */
startTime?: number
/** The end time (year) for the model. */
Expand Down
4 changes: 2 additions & 2 deletions packages/check-core/src/check/check-dataset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { describe, expect, it } from 'vitest'

import type { DatasetKey } from '../_shared/types'
import type { ModelSpec } from '../bundle/bundle-types'
import type { DatasetGroupName, ModelSpec } from '../bundle/bundle-types'
import type { ImplVar, OutputVar } from '../bundle/var-types'
import { expandDatasets } from './check-dataset'
import type { CheckDatasetSpec } from './check-spec'
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('expandDatasets', () => {
implVar('V6')
])

const datasetGroups: Map<string, DatasetKey[]> = new Map([
const datasetGroups: Map<DatasetGroupName, DatasetKey[]> = new Map([
['output group 0', []],
['output group 1', ['Model_unknown_dataset']],
['output group 2', ['Model_v1', 'Model_v6']],
Expand Down
4 changes: 2 additions & 2 deletions packages/check-core/src/check/check-scenario.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, it } from 'vitest'

import { inputSettingsSpec, positionSetting, valueSetting } from '../_shared/scenario-specs'
import type { VarId } from '../_shared/types'
import type { ModelSpec } from '../bundle/bundle-types'
import type { InputGroupName, ModelSpec } from '../bundle/bundle-types'
import type { InputVar } from '../bundle/var-types'
import { expandScenarios } from './check-scenario'
import type { CheckScenarioSpec } from './check-spec'
Expand All @@ -17,7 +17,7 @@ describe('expandScenarios', () => {
const i2 = inputVars.get('_i2')
const i3 = inputVars.get('_i3')

const inputGroups: Map<string, InputVar[]> = new Map([
const inputGroups: Map<InputGroupName, InputVar[]> = new Map([
['input group 1', [i1, i2]],
['Input Group 2', [i2, i3]],
['input group 3', []]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, it } from 'vitest'

import { inputAtPositionSpec as atPosSpec, inputAtValueSpec as atValSpec } from '../../../_shared/scenario-specs'
import type { VarId } from '../../../_shared/types'
import type { ModelSpec } from '../../../bundle/bundle-types'
import type { InputAliasName, ModelSpec } from '../../../bundle/bundle-types'
import { ModelInputs } from '../../../bundle/model-inputs'
import type { InputId, InputVar } from '../../../bundle/var-types'

Expand Down Expand Up @@ -76,7 +76,7 @@ function mockModelSpec(kind: 'L' | 'R'): ModelSpec {
}

// Add aliases by slider name
const inputAliases: Map<string, VarId> = new Map()
const inputAliases: Map<InputAliasName, VarId> = new Map()
inputAliases.set('S1', '_ivara')
inputAliases.set('S2', kind === 'L' ? '_ivarb' : '_ivarb_renamed')
inputAliases.set('S3', kind === 'L' ? '_ivarc' : '_ivard')
Expand Down
3 changes: 3 additions & 0 deletions packages/check-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export type {
BundleGraphSpec,
BundleGraphView,
BundleModel,
DatasetGroupName,
InputAliasName,
InputGroupName,
LegendItem,
LinkItem,
LoadedBundle,
Expand Down

0 comments on commit 65560ef

Please sign in to comment.