orm id and type > @id, @type

feat/orm-diffs
Laurin Weger 1 day ago
parent 1282b9e006
commit d45ebc340f
No known key found for this signature in database
GPG Key ID: 9B372BB0B792770F
  1. 2
      engine/verifier/src/orm/handle_backend_update.rs
  2. 43
      sdk/js/shex-orm/src/schema-converter/util/annotateReadablePredicates.ts
  3. 4
      sdk/rust/src/tests/mod.rs
  4. 10
      sdk/rust/src/tests/orm_create_patches.rs
  5. 34
      sdk/rust/src/tests/orm_creation.rs

@ -267,7 +267,7 @@ impl Verifier {
patches.push(OrmDiffOp { patches.push(OrmDiffOp {
op: OrmDiffOpType::add, op: OrmDiffOpType::add,
valType: None, valType: None,
path: format!("{}/id", json_pointer), path: format!("{}/@id", json_pointer),
value: Some(json!(iri)), value: Some(json!(iri)),
}); });
} }

@ -33,25 +33,40 @@ export default function annotateReadablePredicates(schema: Schema): void {
if (tcs.length > 0) { if (tcs.length > 0) {
// Group by local token (last segment of IRI) and set a base readablePredicate for all // Group by local token (last segment of IRI) and set a base readablePredicate for all
const groups = new Map<string, TCwReadable[]>(); const readableNameToPredicatesMap = new Map<
for (const tc of tcs) { string,
const tokens = splitIriTokens(tc.predicate); TCwReadable[]
const local = tokens.length >();
? tokens[tokens.length - 1] for (const tripleConstraint of tcs) {
: tc.predicate; // Use the name based on the IRI ending.
let readableName: string;
// Special case rdfs:type => @type
if (
tripleConstraint.predicate ===
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
) {
readableName = "@type";
} else {
const tokens = splitIriTokens(tripleConstraint.predicate);
readableName = tokens.length
? tokens[tokens.length - 1]
: tripleConstraint.predicate;
}
// default base name for non-colliders // default base name for non-colliders
tc.readablePredicate = local; tripleConstraint.readablePredicate = readableName;
const arr = groups.get(local) ?? []; const groupMembers =
arr.push(tc); readableNameToPredicatesMap.get(readableName) ?? [];
groups.set(local, arr); groupMembers.push(tripleConstraint);
readableNameToPredicatesMap.set(readableName, groupMembers);
} }
// Resolve each group (rename all in collisions) // Resolve each group (rename all in collisions)
for (const [, arr] of groups) { for (const [, groupMembers] of readableNameToPredicatesMap) {
if (arr.length <= 1) continue; if (groupMembers.length <= 1) continue;
const used = new Set<string>(); const used = new Set<string>();
const local = const local =
splitIriTokens(arr[0].predicate).slice(-1)[0] ?? ""; splitIriTokens(groupMembers[0].predicate).slice(-1)[0] ??
for (const tc of arr) { "";
for (const tc of groupMembers) {
const tokens = splitIriTokens(tc.predicate); const tokens = splitIriTokens(tc.predicate);
let localIdx = tokens.lastIndexOf(local); let localIdx = tokens.lastIndexOf(local);
if (localIdx === -1) if (localIdx === -1)

@ -86,14 +86,14 @@ fn sort_arrays(value: &mut Value) {
} }
} }
/// Helper to recursively remove nested "id" fields from nested objects, /// Helper to recursively remove nested "@id" fields from nested objects,
/// but only if they are not at the root level. /// but only if they are not at the root level.
fn remove_id_fields(value: &mut Value) { fn remove_id_fields(value: &mut Value) {
fn remove_id_fields_inner(value: &mut Value, is_root: bool) { fn remove_id_fields_inner(value: &mut Value, is_root: bool) {
match value { match value {
Value::Object(map) => { Value::Object(map) => {
if !is_root { if !is_root {
map.remove("id"); map.remove("@id");
} }
for v in map.values_mut() { for v in map.values_mut() {
remove_id_fields_inner(v, false); remove_id_fields_inner(v, false);

@ -209,7 +209,7 @@ INSERT DATA {
{ {
"op": "add", "op": "add",
"value": "urn:test:numArrayObj4", "value": "urn:test:numArrayObj4",
"path": "/urn:test:numArrayObj4/id", "path": "/urn:test:numArrayObj4/@id",
"valType": Value::Null, "valType": Value::Null,
}, },
{ {
@ -575,7 +575,7 @@ INSERT DATA {
"op": "add", "op": "add",
// "valType": None, // "valType": None,
"value": "urn:test:multiNested4", "value": "urn:test:multiNested4",
"path": "/urn:test:oj1/multiNest/urn:test:multiNested4/id", "path": "/urn:test:oj1/multiNest/urn:test:multiNested4/@id",
}, },
{ {
"op": "add", "op": "add",
@ -882,7 +882,7 @@ INSERT DATA {
{ {
"op": "add", "op": "add",
"value": "urn:test:cat2", "value": "urn:test:cat2",
"path": "/urn:test:house1/inhabitants/urn:test:person2/cat/id", "path": "/urn:test:house1/inhabitants/urn:test:person2/cat/@id",
}, },
{ {
"op": "add", "op": "add",
@ -903,7 +903,7 @@ INSERT DATA {
{ {
"op": "add", "op": "add",
"value": "urn:test:person3", "value": "urn:test:person3",
"path": "/urn:test:house1/inhabitants/urn:test:person3/id", "path": "/urn:test:house1/inhabitants/urn:test:person3/@id",
}, },
{ {
"op": "add", "op": "add",
@ -923,7 +923,7 @@ INSERT DATA {
{ {
"op": "add", "op": "add",
"value": "urn:test:cat3", "value": "urn:test:cat3",
"path": "/urn:test:house1/inhabitants/urn:test:person3/cat/id", "path": "/urn:test:house1/inhabitants/urn:test:person3/cat/@id",
}, },
{ {
"op": "add", "op": "add",

@ -448,7 +448,7 @@ INSERT DATA {
let mut expected = json!([{ let mut expected = json!([{
"type":"http://example.org/TestObject", "type":"http://example.org/TestObject",
"id":"urn:test:obj1", "@id":"urn:test:obj1",
"anotherObject":{ "anotherObject":{
"urn:test:id1":{ "urn:test:id1":{
"prop1":"one", "prop1":"one",
@ -465,7 +465,7 @@ INSERT DATA {
"numOrStr":"either", "numOrStr":"either",
"numValue":42.0, "numValue":42.0,
"objectValue":{ "objectValue":{
"id":"urn:test:id3", "@id":"urn:test:id3",
"nestedArray":[5.0,6.0], "nestedArray":[5.0,6.0],
"nestedNum":7.0, "nestedNum":7.0,
"nestedString":"nested" "nestedString":"nested"
@ -473,7 +473,7 @@ INSERT DATA {
"stringValue": "hello world", "stringValue": "hello world",
}, },
{ {
"id":"urn:test:obj2", "@id":"urn:test:obj2",
"type":"http://example.org/TestObject", "type":"http://example.org/TestObject",
"anotherObject": { "anotherObject": {
"urn:test:id4":{ "urn:test:id4":{
@ -491,7 +491,7 @@ INSERT DATA {
"numOrStr":4.0, "numOrStr":4.0,
"numValue":422.0, "numValue":422.0,
"objectValue":{ "objectValue":{
"id":"urn:test:id6", "@id":"urn:test:id6",
"nestedArray": [7.0,8.0,9.0], "nestedArray": [7.0,8.0,9.0],
"nestedNum":72.0, "nestedNum":72.0,
"nestedString":"nested2" "nestedString":"nested2"
@ -602,17 +602,17 @@ INSERT DATA {
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:numArrayObj1", "@id": "urn:test:numArrayObj1",
"type": "http://example.org/TestObject", "type": "http://example.org/TestObject",
"numArray": [1.0, 2.0, 3.0] "numArray": [1.0, 2.0, 3.0]
}, },
{ {
"id": "urn:test:numArrayObj2", "@id": "urn:test:numArrayObj2",
"type": "http://example.org/TestObject", "type": "http://example.org/TestObject",
"numArray": [] "numArray": []
}, },
{ {
"id": "urn:test:numArrayObj3", "@id": "urn:test:numArrayObj3",
"type": "http://example.org/TestObject", "type": "http://example.org/TestObject",
"numArray": [1.0, 2.0] "numArray": [1.0, 2.0]
} }
@ -689,7 +689,7 @@ INSERT DATA {
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:oj1", "@id": "urn:test:oj1",
"opt": true "opt": true
} }
]); ]);
@ -786,12 +786,12 @@ INSERT DATA {
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:oj1", "@id": "urn:test:oj1",
"lit1": ["lit 1"], "lit1": ["lit 1"],
"lit2": "lit 2" "lit2": "lit 2"
}, },
{ {
"id": "urn:test:obj2", "@id": "urn:test:obj2",
"lit1": ["lit 1", "lit 1 extra"], "lit1": ["lit 1", "lit 1 extra"],
"lit2": "lit 2" "lit2": "lit 2"
} }
@ -878,7 +878,7 @@ INSERT DATA {
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:oj1", "@id": "urn:test:oj1",
"strOrNum": ["a string", "another string", 2.0] "strOrNum": ["a string", "another string", 2.0]
} }
]); ]);
@ -1079,7 +1079,7 @@ INSERT DATA {
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:oj1", "@id": "urn:test:oj1",
"str": "obj1 str", "str": "obj1 str",
"nestedWithExtra": { "nestedWithExtra": {
"nestedStr": "obj1 nested with extra valid", "nestedStr": "obj1 nested with extra valid",
@ -1199,7 +1199,7 @@ INSERT DATA {
// claire2 is invalid (missing name), so alice2's knows chain is incomplete // claire2 is invalid (missing name), so alice2's knows chain is incomplete
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:alice", "@id": "urn:test:alice",
"name": "Alice", "name": "Alice",
"knows": { "knows": {
"urn:test:bob": { "urn:test:bob": {
@ -1218,7 +1218,7 @@ INSERT DATA {
} }
}, },
{ {
"id": "urn:test:bob", "@id": "urn:test:bob",
"name": "Bob", "name": "Bob",
"knows": { "knows": {
"urn:test:claire": { "urn:test:claire": {
@ -1228,7 +1228,7 @@ INSERT DATA {
} }
}, },
{ {
"id": "urn:test:claire", "@id": "urn:test:claire",
"name": "Claire", "name": "Claire",
"knows": {} "knows": {}
} }
@ -1411,7 +1411,7 @@ INSERT DATA {
// alice2 is incomplete because claire2 has wrong type // alice2 is incomplete because claire2 has wrong type
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:alice", "@id": "urn:test:alice",
"type": "http://example.org/Alice", "type": "http://example.org/Alice",
"knows": { "knows": {
"urn:test:bob": { "urn:test:bob": {
@ -1538,7 +1538,7 @@ INSERT DATA {
let mut expected = json!([ let mut expected = json!([
{ {
"id": "urn:test:alice", "@id": "urn:test:alice",
"type": "http://example.org/Person", "type": "http://example.org/Person",
"cats": { "cats": {
"urn:test:kitten1": { "urn:test:kitten1": {

Loading…
Cancel
Save