Skip to content

Commit 78e9699

Browse files
committed
Improve core plugin
1 parent 1d59c3f commit 78e9699

File tree

2 files changed

+91
-9
lines changed

2 files changed

+91
-9
lines changed

packages/data-plugins/lib/collections/core.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Plugin, SchemaFieldObject } from '@stac-manager/data-core';
2+
import { emptyString2Null, fieldIf, null2EmptyString } from '../utils';
23

34
export class PluginCore extends Plugin {
45
name = 'CollectionsCore';
56

6-
// async init(data) {
7-
// await new Promise((resolve) => setTimeout(resolve, 100));
8-
// }
7+
isNew: boolean = false;
8+
9+
async init(data: any) {
10+
this.isNew = !data?.id;
11+
}
912

1013
editSchema(): SchemaFieldObject {
1114
return {
@@ -19,10 +22,10 @@ export class PluginCore extends Plugin {
1922
// 'links'
2023
// ],
2124
properties: {
22-
id: {
25+
...fieldIf(this.isNew, 'id', {
2326
label: 'Collection ID',
2427
type: 'string'
25-
},
28+
}),
2629
title: {
2730
label: 'Title',
2831
type: 'string'
@@ -136,7 +139,36 @@ export class PluginCore extends Plugin {
136139
},
137140
type: {
138141
label: 'Type',
139-
type: 'string'
142+
type: 'string',
143+
'ui:widget': 'select',
144+
enum: [
145+
['application/geo+json', 'geo+json'],
146+
['application/geopackage+sqlite3', 'geopackage+sqlite3'],
147+
['application/json', 'json'],
148+
['application/schema+json', 'schema+json'],
149+
[
150+
'application/vnd.google-earth.kml+xml',
151+
'vnd.google-earth.kml+xml'
152+
],
153+
['application/vnd.google-earth.kmz', 'vnd.google-earth.kmz'],
154+
[
155+
'application/vnd.oai.openapi+json;version=3.0',
156+
'vnd.oai.openapi+json;version=3.0'
157+
],
158+
['application/x-hdf', 'x-hdf'],
159+
['application/x-hdf5', 'x-hdf5'],
160+
['application/xml', 'xml'],
161+
['image/jp2', 'jp2'],
162+
['image/jpeg', 'jpeg'],
163+
['image/png', 'png'],
164+
[
165+
'image/tiff; application=geotiff; profile=cloud-optimized',
166+
'COG'
167+
],
168+
['image/tiff; application=geotiff', 'Geotiff'],
169+
['text/html', 'HTML'],
170+
['text/plain', 'Text']
171+
]
140172
},
141173
title: {
142174
label: 'Title',
@@ -201,7 +233,7 @@ export class PluginCore extends Plugin {
201233
providers: data?.providers || [],
202234
stac_extensions: data?.stac_extensions,
203235
spatial: data?.extent?.spatial.bbox || [],
204-
temporal: data?.extent?.temporal.bbox || [],
236+
temporal: data?.extent?.temporal.interval.map(null2EmptyString) || [],
205237
links: data?.links || [],
206238
assets: Object.entries<Record<string, any>>(data?.assets || {}).map(
207239
([key, value]) => ({
@@ -227,10 +259,10 @@ export class PluginCore extends Plugin {
227259

228260
extent: {
229261
spatial: {
230-
bbox: [data.spatial?.map(({ value }: any) => Number(value))]
262+
bbox: data.spatial
231263
},
232264
temporal: {
233-
interval: [data.temporal?.map(({ value }: any) => value)]
265+
interval: data.temporal.map(emptyString2Null)
234266
}
235267
},
236268

packages/data-plugins/lib/utils.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { SchemaField } from '@stac-manager/data-core';
2+
3+
/**
4+
*
5+
* @param condition The condition to evaluate
6+
* @param id The id for the field
7+
* @param ifTrue Field schema to associate to id if condition is true
8+
* @param ifFalse Field schema to associate to id if condition is false
9+
* @returns
10+
*/
11+
export function fieldIf<I extends string>(
12+
condition: boolean,
13+
id: I,
14+
ifTrue: SchemaField,
15+
ifFalse?: SchemaField
16+
) {
17+
return (
18+
condition
19+
? {
20+
[id]: ifTrue
21+
}
22+
: ifFalse
23+
? {
24+
[id]: ifTrue
25+
}
26+
: {}
27+
) as Record<I, SchemaField>;
28+
}
29+
30+
/**
31+
* Recursively converts empty strings in arrays to null.
32+
*/
33+
export function emptyString2Null(v: any): any {
34+
if (Array.isArray(v)) {
35+
return v.map(emptyString2Null);
36+
}
37+
38+
return v === '' ? null : v;
39+
}
40+
41+
/**
42+
* Recursively converts nulls in arrays to empty strings.
43+
*/
44+
export function null2EmptyString(v: any): any {
45+
if (Array.isArray(v)) {
46+
return v.map(null2EmptyString);
47+
}
48+
49+
return v === null ? '' : v;
50+
}

0 commit comments

Comments
 (0)