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

make project schema properties optional #133

Merged
merged 5 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
4 changes: 2 additions & 2 deletions proto/project/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ message Project_1 {
repeated bytes relation = 5;
};

DefaultPresets defaultPresets = 2;
optional DefaultPresets defaultPresets = 2;

string name = 5;
optional string name = 5;
}
4 changes: 2 additions & 2 deletions proto/project/v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ message Project_2 {
repeated bytes relation = 5;
};

DefaultPresets defaultPresets = 2;
optional DefaultPresets defaultPresets = 2;

string name = 5;
optional string name = 5;
}
18 changes: 10 additions & 8 deletions schema/project/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
"description": "name of the project",
"type": "string"
},
"defaultPresets":{
"defaultPresets": {
"type": "object",
"properties": {
"point": { "type": "array", "items":{"type":"string"}},
"area": { "type": "array", "items":{"type":"string"}},
"vertex": { "type": "array", "items":{"type":"string"}},
"line": { "type": "array", "items":{"type":"string"}},
"relation": { "type": "array", "items":{"type":"string"}}
}
"point": { "type": "array", "items": { "type": "string" } },
"area": { "type": "array", "items": { "type": "string" } },
"vertex": { "type": "array", "items": { "type": "string" } },
"line": { "type": "array", "items": { "type": "string" } },
"relation": { "type": "array", "items": { "type": "string" } }
},
"required": ["point", "area", "vertex", "line", "relation"],
"additionalProperties": false
}
},
"required": ["schemaName", "name", "defaultPresets"],
"required": ["schemaName"],
"additionalProperties": false
}
16 changes: 9 additions & 7 deletions schema/project/v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
"defaultPresets": {
"type": "object",
"properties": {
"point": { "type": "array", "items":{"type":"string"}},
"area": { "type": "array", "items":{"type":"string"}},
"vertex": { "type": "array", "items":{"type":"string"}},
"line": { "type": "array", "items":{"type":"string"}},
"relation": { "type": "array", "items":{"type":"string"}}
}
"point": { "type": "array", "items": { "type": "string" } },
"area": { "type": "array", "items": { "type": "string" } },
"vertex": { "type": "array", "items": { "type": "string" } },
"line": { "type": "array", "items": { "type": "string" } },
"relation": { "type": "array", "items": { "type": "string" } }
},
"required": ["point", "area", "vertex", "line", "relation"],
"additionalProperties": false
}
},
"required": ["schemaName", "name", "defaultPresets"],
"required": ["schemaName"],
"additionalProperties": false
}
27 changes: 19 additions & 8 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ export const convertProject: ConvertFunction<'project'> = (
message,
versionObj
) => {
const { common, schemaVersion, ...rest } = message
const { common, schemaVersion, defaultPresets, ...rest } = message
const jsonSchemaCommon = convertCommon(common, versionObj)
return {
...jsonSchemaCommon,
...rest,
defaultPresets: {
point: message.defaultPresets?.point.map((p) => p.toString('hex')),
area: message.defaultPresets?.area.map((a) => a.toString('hex')),
vertex: message.defaultPresets?.vertex.map((v) => v.toString('hex')),
line: message.defaultPresets?.line.map((l) => l.toString('hex')),
relation: message.defaultPresets?.relation.map((r) => r.toString('hex')),
},
defaultPresets: defaultPresets
? {
point: defaultPresets.point.map((p) => p.toString('hex')),
area: defaultPresets.area.map((a) => a.toString('hex')),
vertex: defaultPresets.vertex.map((v) => v.toString('hex')),
line: defaultPresets.line.map((l) => l.toString('hex')),
relation: defaultPresets.relation.map((r) => r.toString('hex')),
}
: undefined,
}
}

Expand Down Expand Up @@ -235,3 +237,12 @@ function convertCommon(
updatedAt: common.updatedAt,
}
}

/**
* Unsafe type for Object.entries - you must be sure that the object does not
have additional properties that are not defined in the type, e.g. when using
a const value
*/
export type Entries<T> = {
[K in keyof T]: [K, T[K]]
}[keyof T][]
36 changes: 19 additions & 17 deletions src/lib/encode-converstions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ export const convertProject: ConvertFunction<'project'> = (mapeoDoc) => {
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
...mapeoDoc,
...mapeoDoc,
name: mapeoDoc.name || '',

I think that since name is now optional, we need to take that into account here

Copy link
Member

Choose a reason for hiding this comment

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

I changed the protobuf definition to allow for optional values

Copy link
Contributor

Choose a reason for hiding this comment

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

right! forgot about that!

defaultPresets: {
point: (mapeoDoc.defaultPresets.point || []).map((p) =>
Buffer.from(p, 'hex')
),
area: (mapeoDoc.defaultPresets.area || []).map((a) =>
Buffer.from(a, 'hex')
),
vertex: (mapeoDoc.defaultPresets.vertex || []).map((v) =>
Buffer.from(v, 'hex')
),
line: (mapeoDoc.defaultPresets.line || []).map((l) =>
Buffer.from(l, 'hex')
),
relation: (mapeoDoc.defaultPresets.relation || []).map((r) =>
Buffer.from(r, 'hex')
),
},
defaultPresets: mapeoDoc.defaultPresets
? {
point: (mapeoDoc.defaultPresets.point || []).map((p) =>
Buffer.from(p, 'hex')
),
area: (mapeoDoc.defaultPresets.area || []).map((a) =>
Buffer.from(a, 'hex')
),
vertex: (mapeoDoc.defaultPresets.vertex || []).map((v) =>
Buffer.from(v, 'hex')
),
line: (mapeoDoc.defaultPresets.line || []).map((l) =>
Buffer.from(l, 'hex')
),
relation: (mapeoDoc.defaultPresets.relation || []).map((r) =>
Buffer.from(r, 'hex')
),
}
: undefined,
}
}

Expand Down
12 changes: 1 addition & 11 deletions test/fixtures/good-docs-minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,8 @@ export const goodDocsMinimal = [
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
defaultPresets: {},
name: 'myProject',
},
expected: {
defaultPresets: {
point: [],
area: [],
vertex: [],
line: [],
relation: [],
},
},
expected: {},
},
{
doc: {
Expand Down