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

[Chore] Add more types for schema #2162

Merged
merged 3 commits into from
Mar 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/actions/src/vis-state-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
Feature,
FeatureSelectionContext,
InteractionConfig,
Filter
Filter,
ParsedConfig
} from '@kepler.gl/types';
// TODO - import LoaderObject type from @loaders.gl/core when supported
// TODO - import LoadOptions type from @loaders.gl/core when supported
Expand Down Expand Up @@ -628,8 +629,8 @@ export type AddDataToMapUpdaterOptions = {
export type UpdateVisDataUpdaterAction = {
datasets: AddDataToMapPayload['datasets'];
options: AddDataToMapPayload['options'];
config?: AddDataToMapPayload['config'];
} & AddDataToMapPayload;
config?: ParsedConfig;
};
// * @param dataset.info -info of a dataset
// * @param dataset.info.id - id of this dataset. If config is defined, `id` should matches the `dataId` in config.
// * @param dataset.info.label - A display name of this dataset
Expand All @@ -654,7 +655,7 @@ export type UpdateVisDataUpdaterAction = {
export function updateVisData(
datasets: AddDataToMapPayload['datasets'],
options: AddDataToMapPayload['options'],
config?: AddDataToMapPayload['config']
config?: ParsedConfig
): Merge<UpdateVisDataUpdaterAction, {type: typeof ActionTypes.UPDATE_VIS_DATA}> {
return {
type: ActionTypes.UPDATE_VIS_DATA,
Expand Down
14 changes: 7 additions & 7 deletions src/reducers/src/vis-state-merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ import {

import {LayerColumns, LayerColumn, Layer} from '@kepler.gl/layers';
import {LAYER_BLENDINGS, OVERLAY_BLENDINGS} from '@kepler.gl/constants';
import {CURRENT_VERSION, VisState, VisStateMergers, visStateSchema} from '@kepler.gl/schemas';

import {
CURRENT_VERSION,
VisState,
VisStateMergers,
visStateSchema,
ParsedLayer,
SavedInteractionConfig,
TooltipInfo,
SavedEditor,
ParsedConfig
} from '@kepler.gl/schemas';

import {ParsedLayer, SavedInteractionConfig, TooltipInfo, SavedEditor} from '@kepler.gl/types';
} from '@kepler.gl/types';
import {KeplerTable, Datasets, assignGpuChannels, resetFilterGpuMode} from '@kepler.gl/table';

/**
Expand Down
6 changes: 4 additions & 2 deletions src/reducers/src/vis-state-updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ export const DEFAULT_ANIMATION_CONFIG: AnimationConfig = {
isAnimating: false,
timeFormat: null,
timezone: null,
defaultTimeFormat: null
defaultTimeFormat: null,
hideControl: false,
duration: null
};

export const DEFAULT_EDITOR: Editor = {
Expand Down Expand Up @@ -1389,7 +1391,7 @@ export const resetMapConfigUpdater = (state: VisState): VisState => ({
export const receiveMapConfigUpdater = (
state: VisState,
{
payload: {config = {version: ''}, options = {}}
payload: {config = {}, options = {}}
}: {
type?: typeof ActionTypes.RECEIVE_MAP_CONFIG;
payload: ReceiveMapConfigPayload;
Expand Down
1 change: 0 additions & 1 deletion src/schemas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export {

// eslint-disable-next-line prettier/prettier
export type {
ParsedConfig,
SavedConfigV1,
SavedMap,
LoadedMap,
Expand Down
19 changes: 1 addition & 18 deletions src/schemas/src/schema-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {visStateSchema} from './vis-state-schema';
import {CURRENT_VERSION, VERSIONS} from './versions';
import {isPlainObject} from '@kepler.gl/utils';

import {MapInfo, ParsedVisState, RGBColor, SavedVisState} from '@kepler.gl/types';
import {MapInfo, SavedVisState, SavedMapStyle, ParsedConfig} from '@kepler.gl/types';

export type SavedMapState = {
bearing: number;
Expand All @@ -57,14 +57,6 @@ export type SavedCustomMapStyle = {
};
};

export type SavedMapStyle = {
styleType: string;
topLayerGroups: SavedLayerGroups;
visibleLayerGroups: SavedLayerGroups;
threeDBuildingColor: RGBColor;
mapStyles: SavedCustomMapStyle;
};

/** Schema for v1 saved configuration */
export type SavedConfigV1 = {
version: 'v1';
Expand All @@ -75,14 +67,6 @@ export type SavedConfigV1 = {
};
};

/** Schema for a parsed configuration ("normalized" across versions) */
export type ParsedConfig = {
version: string;
visState?: ParsedVisState;
mapState?: Partial<SavedMapState>;
mapStyle?: Partial<SavedMapStyle>;
};

export type SavedMap = {
datasets: SavedDatasetV1[];
config: SavedConfigV1;
Expand Down Expand Up @@ -269,7 +253,6 @@ export class KeplerGLSchema {
return null;
}

// @ts-expect-error
return Object.keys(config).reduce(
(accu, key) => ({
...accu,
Expand Down
8 changes: 6 additions & 2 deletions src/schemas/src/vis-state-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ import {
ParsedLayer,
ParsedVisState,
SavedFilter,
MinSavedFilter,
SavedInteractionConfig,
SavedLayer,
MinSavedLayer,
SavedVisState,
SplitMap
} from '@kepler.gl/types';
Expand Down Expand Up @@ -570,7 +572,7 @@ export class LayerSchemaV0 extends Schema {
}

load(
layers: SavedLayer[] | undefined
layers: SavedLayer[] | MinSavedLayer[] | undefined
): {
layers: ParsedLayer[] | undefined;
} {
Expand All @@ -591,7 +593,9 @@ export class FilterSchemaV0 extends Schema {
.map(filter => this.savePropertiesOrApplySchema(filter).filters)
};
}
load(filters: undefined | SavedFilter[]): {filters: undefined | ParsedFilter[]} {
load(
filters: SavedFilter[] | MinSavedFilter[] | undefined
): {filters: ParsedFilter[] | undefined} {
return {
filters: filters
?.map(filter => this.loadPropertiesOrApplySchema(filter).filters)
Expand Down
4 changes: 2 additions & 2 deletions src/types/actions.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SavedMap, ParsedConfig} from './schemas';
import {SavedMap, ParsedConfig, SavedConfigV1, MinSavedConfigV1} from './schemas';

/** EXPORT_FILE_TO_CLOUD */
export type MapData = {
Expand Down Expand Up @@ -67,6 +67,6 @@ export type AddDataToMapPayload = {
// as the field is called `datasets`
datasets: ProtoDataset[] | ProtoDataset;
options?: AddDataToMapOptions;
config?: ParsedConfig;
config?: ParsedConfig | SavedConfigV1 | MinSavedConfigV1;
info?: Partial<MapInfo>;
};
1 change: 1 addition & 0 deletions src/types/reducers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export type AnimationConfig = {
domain: number[] | null;
currentTime: number | null;
speed: number;
duration?: number | null;
isAnimating?: boolean;
timeSteps?: null | number[];
// auto generated based on time domain
Expand Down
56 changes: 44 additions & 12 deletions src/types/schemas.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {RGBColor, Merge, RGBAColor} from './types';
import {RGBColor, Merge, RGBAColor, RequireFrom} from './types';

import {
Filter,
Expand Down Expand Up @@ -28,8 +28,8 @@ export type SavedFilter = {
speed: Filter['speed'];
layerId: Filter['layerId'];
};

export type ParsedFilter = Partial<SavedFilter>;
export type MinSavedFilter = RequireFrom<SavedFilter, 'dataId' | 'id' | 'name' | 'type' | 'value'>;
export type ParsedFilter = SavedFilter | MinSavedFilter;

export type SavedInteractionConfig = {
tooltip: TooltipInfo['config'] & {
Expand Down Expand Up @@ -63,18 +63,20 @@ export type SavedLayer = {
[key: string]: string;
};
isVisible: boolean;
visConfig: object;
visConfig: Record<string, any>;
hidden: boolean;
textLabel: Merge<LayerTextLabel, {field: {name: string; type: string} | null}>;
};
visualChannels: SavedVisualChannels;
};

export type ParsedLayer = {
id?: string;
type?: string;
config?: Partial<SavedLayer['config']>;
export type MinSavedLayerConfig = RequireFrom<SavedLayer['config'], 'dataId' | 'columns'>;
export type MinSavedLayer = {
id: string;
type: string;
config: MinSavedLayerConfig;
visualChannels?: SavedVisualChannels;
};
export type ParsedLayer = SavedLayer | MinSavedLayer;

export type SavedAnimationConfig = {
currentTime: AnimationConfig['currentTime'];
Expand All @@ -97,6 +99,18 @@ export type SavedVisState = {
editor?: SavedEditor;
};

// Min saved config can be passed to addDataToMap
export type MinSavedVisStateV1 = {
filters?: MinSavedFilter[];
layers?: MinSavedLayer[];
interactionConfig?: Partial<SavedInteractionConfig>;
layerBlending?: string;
overlayBlending?: string;
splitMaps?: SplitMap[];
animationConfig?: SavedAnimationConfig;
editor?: SavedEditor;
};

export type ParsedVisState = {
layers?: ParsedLayer[];
filters?: ParsedFilter[];
Expand All @@ -117,6 +131,10 @@ export type SavedMapState = {
isSplit: boolean;
};

export type SavedMapStateV1 = SavedMapState;
export type MinSavedMapStateV1 = Partial<SavedMapState>;
export type ParsedMapState = Partial<SavedMapState>;

export type SavedLayerGroups = {
[key: string]: boolean;
};
Expand All @@ -140,6 +158,11 @@ export type SavedMapStyle = {
mapStyles: SavedCustomMapStyle;
};

export type SavedMapStyleV1 = SavedMapStyle;
export type MinSavedMapStyleV1 = Partial<SavedMapStyleV1>;

export type ParsedMapStyle = Partial<SavedMapStyleV1>;

/** Schema for v1 saved configuration */
export type SavedConfigV1 = {
version: 'v1';
Expand All @@ -150,12 +173,21 @@ export type SavedConfigV1 = {
};
};

// supported by addDataToMap
export type MinSavedConfigV1 = {
version: 'v1';
config: {
visState?: MinSavedVisStateV1;
mapState?: MinSavedMapStateV1;
mapStyle?: MinSavedMapStyleV1;
};
};

/** Schema for a parsed configuration ("normalized" across versions) */
export type ParsedConfig = {
version: string;
visState?: ParsedVisState;
mapState?: Partial<SavedMapState>;
mapStyle?: Partial<SavedMapStyle>;
mapState?: ParsedMapState;
mapStyle?: ParsedMapStyle;
};

export type SavedField = {
Expand Down
2 changes: 2 additions & 0 deletions src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type Merge<A, B> = {[K in keyof A]: K extends keyof B ? B[K] : A[K]} & B
? {[K in keyof O]: O[K]}
: never;

export type RequireFrom<T, K extends keyof T> = Merge<Required<Pick<T, K>>, Partial<Omit<T, K>>>;

export type Entries<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T][];
Expand Down
1 change: 0 additions & 1 deletion src/utils/src/filter-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ export function validateFilter<K extends KeplerTableModel<K, L>, L>(
}

const initializeFilter: Filter = {
// @ts-expect-error
...getDefaultFilter(filter.dataId),
...filter,
dataId: filterDataId,
Expand Down
7 changes: 2 additions & 5 deletions test/node/reducers/vis-state-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,10 @@ test('#visStateReducer -> LAYER_CONFIG_CHANGE -> isVisible -> animationConfig',
t.deepEqual(
nextState.animationConfig,
{
...DEFAULT_ANIMATION_CONFIG,
domain: null,
currentTime: 1565577261000,
speed: 1,
isAnimating: false,
defaultTimeFormat: null,
timeFormat: null,
timezone: null
isAnimating: false
},
'should set animationConfig to default'
);
Expand Down