-
Notifications
You must be signed in to change notification settings - Fork 4
/
data-value-resolver.js
156 lines (151 loc) · 7.26 KB
/
data-value-resolver.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
148
149
150
151
152
153
154
155
156
const {DataAttributeResolver} = require('./data-attribute-resolver');
const {isObjectDeep} = require('./is-object');
const {sprintf} = require('sprintf-js');
var {Expression} = require('@themost/query');
/**
* @class DataValueResolver
* @param {import('./index').DataQueryable} target
* @constructor
*/
function DataValueResolver(target) {
Object.defineProperty(this, 'target', { get: function() {
return target;
}, configurable:false, enumerable:false});
}
DataValueResolver.prototype.resolve = function(value) {
/**
* @type {DataQueryable}
*/
var target = this.target;
if (typeof value === 'string' && /^\$it\//.test(value)) {
var attr = value.replace(/^\$it\//,'');
if (DataAttributeResolver.prototype.testNestedAttribute(attr)) {
return DataAttributeResolver.prototype.resolveNestedAttribute.call(target, attr);
}
else {
attr = DataAttributeResolver.prototype.testAttribute(attr);
if (attr) {
return target.fieldOf(attr.name);
}
}
}
// if value is an instance of Expression e.g. an instance if MemberExpression
if (value instanceof Expression) {
// return the expression
return value;
}
if (isObjectDeep(value)) {
// try to get in-process left operand
// noinspection JSUnresolvedReference
var left = target.query.privates && target.query.privates.property;
if (typeof left === 'string' && /\./.test(left)) {
var members = left.split('.');
if (Array.isArray(members)) {
// try to find member mapping
/**
* @type {import('./data-model').DataModel}
*/
var model = target.model;
var mapping;
var attribute;
var index = 0;
var context = target.model.context;
// if the first segment contains the view adapter name
if (members[0] === target.model.viewAdapter) {
// move next
index++;
} else if (target.query.$expand != null) {
// try to find if the first segment is contained in the collection of joined entities
var joins = Array.isArray(target.query.$expand) ? target.query.$expand : [ target.query.$expand ];
if (joins.length) {
var found = joins.find(function(x) {
return x.$entity && x.$entity.$as === members[0];
});
if (found) {
var mapping1 = model.inferMapping(found.$entity.$as);
if (mapping1 && mapping1.associationType === 'junction') {
// get next segment of members
var nextMember = members[index + 1];
if (nextMember === mapping1.associationObjectField) {
// the next segment is the association object field
// e.g. groups/group
model = context.model(mapping1.parentModel);
members[index + 1] = mapping1.parentField;
} else if (nextMember === mapping1.associationValueField) {
// the next segment is the association value field
// e.g. groups/user
model = context.model(mapping1.childModel);
members[index + 1] = mapping1.childField;
} else if (model.name === mapping1.parentModel) {
model = context.model(mapping1.childModel);
} else {
model = context.model(mapping1.parentModel);
}
} else if (found.$entity.model != null) {
model = context.model(found.$entity.model);
} else {
throw new Error(sprintf('Expected a valid mapping for property "%s"', found.$entity.$as));
}
index++;
}
}
}
var mapValue = function(x) {
if (Object.hasOwnProperty.call(x, name)) {
return x[name];
}
throw new Error(sprintf('Invalid value for property "%s"', members[members.length - 1]));
}
while (index < members.length) {
mapping = model.inferMapping(members[index]);
if (mapping) {
if (mapping.associationType === 'association' && mapping.childModel === model.name) {
model = context.model(mapping.parentModel);
if (model) {
attribute = model.getAttribute(mapping.parentField);
}
} else if (mapping.associationType === 'association' && mapping.parentModel === model.name) {
model = context.model(mapping.childModel);
if (model) {
attribute = model.getAttribute(mapping.childField);
}
} else if (mapping.associationType === 'junction' && mapping.childModel === model.name) {
model = context.model(mapping.parentModel);
if (model) {
attribute = model.getAttribute(mapping.parentField);
}
} else if (mapping.associationType === 'junction' && mapping.parentModel === model.name) {
model = context.model(mapping.childModel);
if (model) {
attribute = model.getAttribute(mapping.childField);
}
}
} else {
// if mapping is not found, and we are in the last segment
// try to find if this last segment is a field of the current model
if (index === members.length - 1) {
attribute = model.getAttribute(members[index]);
break;
}
attribute = null;
model = null;
break;
}
index++;
}
if (attribute) {
var name = attribute.property || attribute.name;
if (Array.isArray(value)) {
return value.map(function(x) {
return mapValue(x);
});
} else {
return mapValue(value);
}
}
}
}
}
return value;
}
module.exports = { DataValueResolver };