-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
147 lines (133 loc) · 4.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
*/
const { defaultLookup, defaultClasses } = require("./src/definitions");
const { sharedDefaults, sharedDataSchema, sharedOneObject } = require("./src/shared");
const { objEquality } = require("./src/util");
/**
* Extends a given TD with the default values of fields that aren't filled.
* @param {object} td The TD to extend with default values
* @returns {void}
*/
function addDefaults(td) {
sharedDefaults(td, extendOneObject, extendDataSchema);
}
/**
* Remove explicitly given default values from a TD.
* @param {object} td The TD to remove default values from
* @returns {void}
*/
function removeDefaults(td) {
sharedDefaults(td, reduceOneObject, reduceDataSchema);
}
/**
* Recursively extends nested data Schemas
* @param {object} dataSchema A TD dataSchema object
*/
function extendDataSchema(dataSchema) {
sharedDataSchema(dataSchema, extendOneObject);
}
/**
* Recursively reduces nested data Schemas
* @param {object} dataSchema A TD dataSchema object
*/
function reduceDataSchema(dataSchema) {
sharedDataSchema(dataSchema, reduceOneObject);
}
/**
* Adds default values to one given object,
* using the defaultLookup table
* @param {{}} target the object to extend
* @param {string} type The type according to the defaultLookup table
* @param {{}} parentInteraction Only for read/writeOnly special case
*/
function extendOneObject(target, type, parentInteraction) {
const callback = (cTarget, cType, cParent) => {
// treat special case, that readOnly / writeOnly should be respected
if (cType === "Form-PropertyAffordance" && (cParent.readOnly || cParent.writeOnly)) {
if (cTarget.op === undefined) {
if (cParent.readOnly && cParent.writeOnly) {
console.warn("readOnly and writeOnly are both true for: ", cParent);
// do not set op in this case
} else if (cParent.readOnly) {
cTarget.op = "readproperty";
} else {
cTarget.op = "writeproperty";
}
}
}
// default behavior
else {
const defaultSource = defaultLookup[cType];
if (cType === "AdditionalExpectedResponse" && cParent.contentType) {
defaultSource.contentType = cParent.contentType;
}
Object.keys(defaultSource).forEach((key) => {
if (cTarget[key] === undefined) {
cTarget[key] = defaultSource[key];
}
});
}
};
sharedOneObject(target, type, callback, parentInteraction);
}
/**
* Remove properties from one object if they
* equal default values, using the defaultLookup table
* @param {object} target the object to extend
* @param {string} type The type according to the defaultLookup table
* @param {{}} parentInteraction Only for read/writeOnly special case
*/
function reduceOneObject(target, type, parentInteraction) {
const callback = (cTarget, cType, cParent) => {
// treat special case, that readOnly / writeOnly should be respected
if (
cType === "Form-PropertyAffordance" &&
(cParent.readOnly || cParent.writeOnly) &&
typeof cTarget.op === "string"
) {
if (cParent.readOnly && cParent.writeOnly) {
console.warn("readOnly and writeOnly are both true for: ", cParent);
// do not set op in this case
} else if (cParent.readOnly && cTarget.op === "readproperty") {
delete cTarget.op;
} else if (cParent.writeOnly && cTarget.op === "writeproperty") {
delete cTarget.op;
} else {
// do nothing
}
} else {
const defaultSource = defaultLookup[cType];
if (cType === "AdditionalExpectedResponse" && cParent.contentType) {
defaultSource.contentType = cParent.contentType;
}
Object.keys(defaultSource).forEach((key) => {
if (objEquality(cTarget[key], defaultSource[key])) {
delete cTarget[key];
}
});
}
};
sharedOneObject(target, type, callback, parentInteraction);
}
module.exports = {
addDefaults,
removeDefaults,
extendDataSchema,
extendOneObject,
reduceDataSchema,
reduceOneObject,
defaultLookup,
defaultClasses,
};