Skip to content

Commit 3e1e8fe

Browse files
🔨 add empty arrays to the default grapher config
1 parent 27ae90b commit 3e1e8fe

File tree

7 files changed

+41
-24
lines changed

7 files changed

+41
-24
lines changed

adminSiteClient/AbstractChartEditor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export abstract class AbstractChartEditor<
106106
/** patch config merged with parent config */
107107
@computed get fullConfig(): GrapherInterface {
108108
if (!this.activeParentConfig) return this.liveConfig
109-
return mergeGrapherConfigs(this.activeParentConfig, this.liveConfig)
109+
return mergeGrapherConfigs(this.activeParentConfig, this.patchConfig)
110110
}
111111

112112
/** parent config currently applied to grapher */

devTools/schema/generate-default-object-from-schema.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import parseArgs from "minimist"
44
import fs from "fs-extra"
55
import { range } from "lodash"
66

7+
const TEMPLATE_FILENAME = "./devTools/schema/template"
8+
79
const schemaVersionRegex =
810
/https:\/\/files\.ourworldindata\.org\/schemas\/grapher-schema\.(?<version>\d{3})\.json/m
911
const getSchemaVersion = (config: Record<string, any>): string =>
@@ -58,23 +60,23 @@ async function main(parsedArgs: parseArgs.ParsedArgs) {
5860

5961
// save as ts file if requested
6062
if (parsedArgs["save-ts"]) {
63+
const template = fs.readFileSync(TEMPLATE_FILENAME, "utf8")
64+
6165
const latestVersion = getSchemaVersion(defaultConfig)
6266
const outdatedVersionsAsInts = range(1, parseInt(latestVersion))
6367
const outdatedVersions = outdatedVersionsAsInts.map((versionNumber) =>
6468
versionNumber.toString().padStart(3, "0")
6569
)
6670

6771
const out = parsedArgs["save-ts"]
68-
const content = `// THIS IS A GENERATED FILE, DO NOT EDIT DIRECTLY
69-
70-
// GENERATED BY devTools/schema/generate-default-object-from-schema.ts
71-
72-
import { GrapherInterface } from "@ourworldindata/types"
73-
74-
export const latestSchemaVersion = "${latestVersion}" as const
75-
export const outdatedSchemaVersions = ${toArrayString(outdatedVersions)} as const
72+
const content = template
73+
.replace("{{LATEST_SCHEMA_VERSION}}", latestVersion.toString())
74+
.replace(
75+
"{{OUTDATED_SCHEMA_VERSIONS}}",
76+
toArrayString(outdatedVersions)
77+
)
78+
.replace("{{DEFAULT_GRAPHER_CONFIG}}", defaultConfigJSON)
7679

77-
export const defaultGrapherConfig = ${defaultConfigJSON} as GrapherInterface`
7880
fs.outputFileSync(out, content)
7981
}
8082

devTools/schema/template

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// THIS IS A GENERATED FILE, DO NOT EDIT DIRECTLY
2+
3+
// GENERATED BY devTools/schema/generate-default-object-from-schema.ts
4+
5+
import { GrapherInterface } from "@ourworldindata/types"
6+
7+
export const latestSchemaVersion = "{{LATEST_SCHEMA_VERSION}}" as const
8+
export const outdatedSchemaVersions = {{OUTDATED_SCHEMA_VERSIONS}} as const
9+
10+
export const defaultGrapherConfig = {{DEFAULT_GRAPHER_CONFIG}} as GrapherInterface

packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts

-12
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ it("can get dimension slots", () => {
7979
it("an empty Grapher serializes to an object that includes only the schema", () => {
8080
expect(new Grapher().toObject()).toEqual({
8181
$schema: latestGrapherConfigSchema,
82-
83-
// TODO: ideally, these are not serialised for an empty object
84-
selectedEntityNames: [],
85-
focusedSeriesNames: [],
8682
})
8783
})
8884

@@ -93,20 +89,12 @@ it("a bad chart type does not crash grapher", () => {
9389
expect(new Grapher(input).toObject()).toEqual({
9490
...input,
9591
$schema: latestGrapherConfigSchema,
96-
97-
// TODO: ideally, these are not serialised for an empty object
98-
selectedEntityNames: [],
99-
focusedSeriesNames: [],
10092
})
10193
})
10294

10395
it("does not preserve defaults in the object (except for the schema)", () => {
10496
expect(new Grapher({ tab: GRAPHER_TAB_OPTIONS.chart }).toObject()).toEqual({
10597
$schema: latestGrapherConfigSchema,
106-
107-
// TODO: ideally, these are not serialised for an empty object
108-
selectedEntityNames: [],
109-
focusedSeriesNames: [],
11098
})
11199
})
112100

packages/@ourworldindata/grapher/src/core/Grapher.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -438,17 +438,22 @@ export class Grapher
438438
} = {}
439439

440440
// Initializing arrays with `undefined` ensures that empty arrays get serialised
441-
@observable selectedEntityNames?: EntityName[] = undefined
442441
@observable excludedEntities?: number[] = undefined
443442
/** IncludedEntities are usually empty which means use all available entities. When
444443
includedEntities is set it means "only use these entities". excludedEntities
445444
are evaluated afterwards and can still remove entities even if they were included before.
446445
*/
447446
@observable includedEntities?: number[] = undefined
448-
@observable focusedSeriesNames?: SeriesName[] = undefined
449447
@observable comparisonLines?: ComparisonLineConfig[] = undefined // todo: Persistables?
450448
@observable relatedQuestions?: RelatedQuestionsConfig[] = undefined // todo: Persistables?
451449

450+
// Since selected entity names and focused series names are managed internally
451+
// by separate stores (selection and focusArray, resp.), they're always
452+
// serialised. To ensure that empty arrays are not serialised, the default is
453+
// set to just that since default values are omitted from serialisation.
454+
@observable selectedEntityNames: EntityName[] = []
455+
@observable focusedSeriesNames: SeriesName[] = []
456+
452457
/**
453458
* Used to highlight an entity at a particular time in a line chart.
454459
* The sparkline in map tooltips makes use of this.

packages/@ourworldindata/grapher/src/schema/defaultGrapherConfig.ts

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const defaultGrapherConfig = {
3030
time: "latest",
3131
},
3232
maxTime: "latest",
33+
selectedEntityNames: [],
34+
focusedSeriesNames: [],
3335
yAxis: {
3436
removePointsOutsideDomain: false,
3537
scaleType: "linear",
@@ -54,11 +56,13 @@ export const defaultGrapherConfig = {
5456
selectedFacetStrategy: "none",
5557
invertColorScheme: false,
5658
hideRelativeToggle: true,
59+
comparisonLines: [],
5760
logo: "owid",
5861
entityType: "country or region",
5962
facettingLabelByYVariables: "metric",
6063
addCountryMode: "add-country",
6164
compareEndPointsOnly: false,
65+
relatedQuestions: [],
6266
chartTypes: ["LineChart"],
6367
hasMapTab: false,
6468
stackMode: "absolute",
@@ -68,6 +72,7 @@ export const defaultGrapherConfig = {
6872
time: false,
6973
changeInPrefix: false,
7074
},
75+
excludedEntities: [],
7176
xAxis: {
7277
removePointsOutsideDomain: false,
7378
scaleType: "linear",
@@ -85,6 +90,7 @@ export const defaultGrapherConfig = {
8590
sortBy: "total",
8691
sortOrder: "desc",
8792
hideFacetControl: true,
93+
includedEntities: [],
8894
entityTypePlural: "countries and regions",
8995
missingDataStrategy: "auto",
9096
} as GrapherInterface

packages/@ourworldindata/grapher/src/schema/grapher-schema.006.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ properties:
101101
selectedEntityNames:
102102
type: array
103103
description: The initial selection of entities
104+
default: []
104105
items:
105106
type:
106107
- string
@@ -109,6 +110,7 @@ properties:
109110
description: |
110111
The initially focused chart elements. Is either a list of entity or variable names.
111112
Only works for line and slope charts for now.
113+
default: []
112114
items:
113115
type:
114116
- string
@@ -195,6 +197,7 @@ properties:
195197
comparisonLines:
196198
description: List of vertical comparison lines to draw
197199
type: array
200+
default: []
198201
items:
199202
type: object
200203
properties:
@@ -361,6 +364,7 @@ properties:
361364
relatedQuestions:
362365
type: array
363366
description: Links to related questions
367+
default: []
364368
items:
365369
type: object
366370
properties:
@@ -428,6 +432,7 @@ properties:
428432
excludedEntities:
429433
type: array
430434
description: Entities that should be excluded from the chart
435+
default: []
431436
items:
432437
type: integer
433438
minimum: 0
@@ -493,6 +498,7 @@ properties:
493498
includedEntities:
494499
type: array
495500
description: Entities to include. Opposite of excludedEntities. If this is set then all entities not specified here are excluded.
501+
default: []
496502
items:
497503
type: number
498504
entityTypePlural:

0 commit comments

Comments
 (0)