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 options for share json support #11

Merged
merged 32 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
246d227
add bin to .gitignore
nf-s Sep 9, 2020
93e7be1
add partial ConversionOption and add MembersResult type
nf-s Sep 9, 2020
f9f5876
add share-link conversion
nf-s Sep 11, 2020
514489d
add story support
nf-s Sep 13, 2020
b4e8c2c
add share tests + some member props
nf-s Sep 15, 2020
3e29e89
fix share test
nf-s Sep 15, 2020
5bda4be
add basic story test
nf-s Sep 15, 2020
9fc39eb
handle share id + knownContainerUniqueIds
nf-s Sep 16, 2020
66f61b4
simplify share user-added data conversion + add user added data test
nf-s Sep 17, 2020
cab586e
add test code
nf-s Sep 17, 2020
b9b4bb1
fix test
nf-s Sep 17, 2020
99a86de
user added data now works (single level only - no groups)
nf-s Sep 17, 2020
1c362bf
Lock rollup-plugin-dts to 1.4.10
soyarsauce Sep 23, 2020
84865ef
move "name" and "type" into catalogMemberPropsIgnore
nf-s Sep 24, 2020
d7f33ec
wms support
nf-s Sep 25, 2020
f6f847e
fix spelling
nf-s Sep 25, 2020
f1a5160
Merge remote-tracking branch 'origin/master' into wms
nf-s Sep 26, 2020
3fd6419
Merge branch 'wms' of https://github.com/TerriaJS/catalog-converter i…
nf-s Sep 26, 2020
7d75133
fix currentTime + copy over ids :o
nf-s Sep 26, 2020
27ca9ea
Merge pull request #13 from TerriaJS/wms
nf-s Sep 26, 2020
1495a85
share conversion - delete ids for user added data
nf-s Sep 26, 2020
fdf22ab
fix type
nf-s Sep 26, 2020
699b56f
fix share tests + remove undefined copyProps
nf-s Sep 26, 2020
91acd66
fix share workbenchIds
nf-s Sep 26, 2020
cf9b620
fix workbench order
nf-s Sep 26, 2020
7d193d3
bump version for tag
soyarsauce Sep 27, 2020
6dc6765
Add postinstall step
soyarsauce Sep 27, 2020
271f86c
fix opacity
nf-s Sep 27, 2020
c168eee
Merge branch 'share-support' of http://github.com/TerriaJS/catalog-co…
nf-s Sep 27, 2020
c213482
0.0.2-alpha.1
soyarsauce Sep 28, 2020
89778cd
0.0.2-alpha.2
soyarsauce Sep 28, 2020
56c4b33
fixes
nf-s Oct 2, 2020
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ compiled
.awcache
.rpt2_cache
docs

bin
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "catalog-converter",
"version": "0.0.2",
"version": "0.0.2-alpha.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd version number, to go from 0.0.2 to 0.0.2-alpha. Any reason? There's no good versioning policy for this repo yet so I'll take suggestions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @soyarsauce did this for terrace stuff

"description": "",
"keywords": [],
"main": "dist/catalog-converter.umd.js",
Expand Down Expand Up @@ -32,7 +32,8 @@
"commit": "git-cz",
"semantic-release": "semantic-release",
"semantic-release-prepare": "ts-node tools/semantic-release-prepare",
"travis-deploy-once": "travis-deploy-once"
"travis-deploy-once": "travis-deploy-once",
"prepare": "npm run prebuild && npm run build"
},
"config": {
"commitizen": {
Expand Down Expand Up @@ -101,9 +102,8 @@
"replace-in-file": "^3.4.2",
"rimraf": "^2.6.2",
"rollup": "^2.7.3",
"rollup-plugin-dts": "^1.4.10",
"rollup-plugin-dts": "1.4.10",
"rollup-plugin-sourcemaps": "^0.5.0",
"rollup-plugin-typescript-2": "0.8.1",
"semantic-release": "^15.9.16",
"shelljs": "^0.8.3",
"travis-deploy-once": "^5.0.9",
Expand Down
1 change: 1 addition & 0 deletions src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum Severity {
}

export enum ModelType {
Share = "Share",
Member = "CatalogMember",
Group = "CatalogGroup",
WmsItem = "WebMapServiceCatalogItem",
Expand Down
215 changes: 213 additions & 2 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Unfortunately types in `is` require @types/node to be pinned to v12
// See https://github.com/sindresorhus/is/issues/108
import is from "@sindresorhus/is";
import { merge } from "lodash";
import { csvCatalogItem } from "./converters/CsvItem";
import {
convertMembersArrayWithConvertMember,
Expand All @@ -21,8 +22,8 @@ import {
geoJsonCatalogItem,
groupFromConvertMembersArray,
sosCatalogItem,
wmsCatalogItem,
} from "./converters/other";
import { wmsCatalogItem } from "./converters/WmsCatalogItem";
import {
inputNotPlainObject,
Message,
Expand Down Expand Up @@ -53,6 +54,7 @@ function defaultOptions(options: ConversionOptions | undefined) {
return Object.assign(
{
copyUnknownProperties: false,
partial: false,
},
options || {}
);
Expand All @@ -63,7 +65,7 @@ export function convertMember(
options?: ConversionOptions
): MemberResult {
options = defaultOptions(options);
if (isCatalogMember(member)) {
if (isCatalogMember(member, options.partial)) {
const converterForType = converters.get(member.type);
if (!converterForType) {
return {
Expand Down Expand Up @@ -134,3 +136,212 @@ export function convertCatalog(
}
return result;
}

export type Share = { version: string; initSources: any[] };
export type ShareResult = {
result: Share | null;
messages?: Message[];
converted?: boolean;
};
export type Story = {
title: string;
text: string;
id: string;
shareData: Share;
};
export function convertShare(json: unknown): ShareResult {
if (!is.plainObject(json)) {
return {
result: null,
messages: [inputNotPlainObject()],
};
}

// If version 8 return
if (
"version" in json &&
typeof json.version === "string" &&
json.version.startsWith("8")
) {
return { result: json as Share, converted: false };
}

if (!Array.isArray(json.initSources)) {
return {
result: null,
messages: [
missingRequiredProp(
ModelType.Share,
"initSources",
undefined,
"Init sources"
),
],
};
}

const messages: Message[] = [];

const initializationUrls: string[] = [];

// Crunch v7 initSources together + extract initializationUrls
const v7InitSource = json.initSources.reduce<any>((sources, current) => {
if (typeof current === "string") {
initializationUrls.push(current);
return sources;
}

return merge(sources, current);
nf-s marked this conversation as resolved.
Show resolved Hide resolved
}, {});

const v8InitSource: any = { stratum: "user" };

const workbenchIds: string[] = [];

const convertMembers = (members: any, convertUserAdded = false) =>
Object.entries(members).reduce<any>((convertedMembers, [id, v7Member]) => {
if (is.plainObject(v7Member)) {
// Get `knownContainerUniqueIds` from v7 `parents` property
let knownContainerUniqueIds = ["/"];
if (Array.isArray(v7Member.parents) && v7Member.parents.length > 0) {
knownContainerUniqueIds = v7Member.parents
.map((parent: string) => {
// Convert v7 user added data group id
if (parent === "Root Group/User-Added Data") {
return "__User-Added_Data__";
}
// Convert v7 root group id
if (parent === "Root Group") {
return "/";
}

// Replace v7 Root Group with slash (v8 auto-ids start with //$catalogName)
return parent.replace("Root Group", "/");
})
.filter((parent) => typeof parent !== "undefined") as string[];
}

// Firstly, if model has explicit `id`
// Otherwise, try to guess id based on this:
// v7 Id has format /Root Group/$someContainerId/$someLowerContainerId/$catalogName
// v8 Id has format //$someContainerId/$someLowerContainerId/$catalogName
// So replace "Root Group" with "/"
let newId =
typeof v7Member.id === "string" && v7Member.id !== ""
? v7Member.id
: id.replace("Root Group", "/");

// Replace User Added Data group id
if (id === "Root Group/User-Added Data") {
newId = "__User-Added_Data__";
}

// For some reason user added data doesn't have the __User-Added_Data__ group in ids (in v8)
newId = newId.replace("//User-Added Data", "");

// Only add to workbenchIds if NOT converting User Added Data
if (v7Member.isEnabled && !convertUserAdded) {
workbenchIds.push(newId);
}

// Only convert user added data if convertUserAdded
if (
convertUserAdded ||
(id !== "Root Group/User-Added Data" &&
!knownContainerUniqueIds.includes("__User-Added_Data__"))
) {
const result = convertMember(v7Member, { partial: true });
messages.push(...result.messages);
convertedMembers[newId] = {
...result.member,
knownContainerUniqueIds,
};
}
return convertedMembers;
}
}, {});

// Shared catalog members
if ("sharedCatalogMembers" in v7InitSource) {
v8InitSource.models = convertMembers(v7InitSource.sharedCatalogMembers);
} else {
v8InitSource.models = {};
}

// User added data
if ("catalog" in v7InitSource && Array.isArray(v7InitSource.catalog)) {
// Only add "Root Group/User-Added Data" Catalog Group
const userAddedData = v7InitSource.catalog.find(
(item: any) => item.id === "Root Group/User-Added Data"
);

if (
typeof userAddedData !== "undefined" &&
Array.isArray(userAddedData.items) &&
userAddedData.items.length > 0
) {
// Delete all ids
delete userAddedData.id;
const deleteIds = (items: any[]) =>
items.forEach((item) => {
if (typeof item.id === "string") delete item.id;
if (Array.isArray(item.items)) deleteIds(item.items);
});

deleteIds(userAddedData.items);

const userAddedDataV8 = convertMembers(
{
"Root Group/User-Added Data": userAddedData,
},
true
);

// Add IDs to user added models - so they show up in the workbenck (from sharedCatalogMembers)
userAddedDataV8?.["__User-Added_Data__"]?.members.forEach(
(member: any) =>
member !== null && (member.id = member.id ?? `/${member.name}`)
);
v8InitSource.models = { ...userAddedDataV8, ...v8InitSource.models };
}
}

if ("stories" in v7InitSource && Array.isArray(v7InitSource.stories)) {
v8InitSource.stories = v7InitSource.stories.map((story: Story) => {
const result = convertShare(story.shareData);
// Add story details to message paths
messages.push(
...(result.messages?.map((message) => {
return { ...message, path: ["Story", story.title, ...message.path] };
}) || [])
);
return { ...story, shareData: result.result };
});
}

v8InitSource.workbench = workbenchIds.reverse();

// Copy over common properties
[
"initialCamera",
"homeCamera",
"baseMapName",
"viewerMode",
"currentTime",
"showSplitter",
"splitPosition",
"previewedItemId",
// Not currently used:
//"timeline" - v7 timeline is different from v8
//"locationMarker" - not in v8
//"sharedFromExplorerPanel" - not in v8
//"pickedFeatures" - not in v8 share links
].forEach((prop) => (v8InitSource[prop] = v7InitSource[prop]));

const v8json: { version: string; initSources: any[] } = {
version: "8.0.0",
initSources: [...initializationUrls, v8InitSource],
};

return { result: v8json, messages, converted: true };
}
6 changes: 3 additions & 3 deletions src/converters/CsvItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getUnknownProps,
nullResult,
propsToWarnings,
catalogMemberPropsIgnore,
} from "./helpers";

function tableStyle(tableStyle: PlainObject) {
Expand Down Expand Up @@ -39,7 +40,7 @@ export function csvCatalogItem(
item: CatalogMember,
options: ConversionOptions
): MemberResult {
if (!is.string(item.url) && !is.string(item.data)) {
if (!options.partial && !is.string(item.url) && !is.string(item.data)) {
return nullResult(
missingRequiredProp(
ModelType.CsvItem,
Expand All @@ -50,9 +51,8 @@ export function csvCatalogItem(
);
}
const unknownProps = getUnknownProps(item, [
"name",
"type",
...catalogMemberProps,
...catalogMemberPropsIgnore,
"url",
"data",
"opacity",
Expand Down
Loading