forked from google/earthengine-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.js
295 lines (260 loc) · 8.51 KB
/
function.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* @fileoverview A base class for EE Functions.
*/
goog.provide('ee.Function');
goog.provide('ee.Function.Signature');
goog.require('ee.ComputedObject');
goog.require('ee.Encodable');
goog.require('ee.Serializer');
goog.require('ee.Types');
goog.require('goog.array');
goog.require('goog.functions');
goog.require('goog.object');
/**
* An abstract base class for functions callable by the EE API. Subclasses
* must implement encode() and getSignature().
*
* TODO(user): Implement Function.bind() while supporting both positional
* and named args and avoiding dependendency on CustomFunction.
*
* @constructor
* @extends {ee.Encodable}
*/
ee.Function = function() {
if (!(this instanceof ee.Function)) {
return new ee.Function();
}
};
goog.inherits(ee.Function, ee.Encodable);
// Exporting manually to avoid marking the class public in the docs.
goog.exportSymbol('ee.Function', ee.Function);
/**
* A function used to type-coerce arguments and return values.
* @type {function(*, string): *}
* @private
*/
ee.Function.promoter_ = goog.functions.identity;
/**
* Register a function used to type-coerce arguments and return values.
* @param {function(*, string): *} promoter A function used to type-coerce
* arguments and return values. Passed a value as the first parameter
* and a type name as the second. Can be used, for example, promote
* numbers or strings to Images. Should return the input promoted if
* the type is recognized, otherwise the original input.
*/
ee.Function.registerPromoter = function(promoter) {
ee.Function.promoter_ = promoter;
};
/**
* Returns a description of the interface provided by this function.
*
* @return {ee.Function.Signature}
*/
ee.Function.prototype.getSignature = goog.abstractMethod;
/**
* Call the function with the given positional arguments.
*
* @param {...*} var_args Positional arguments to pass to the function.
* @return {ee.ComputedObject} An object representing the called function.
* If the signature specifies a recognized return type, the returned
* value will be cast to that type.
* @export
*/
ee.Function.prototype.call = function(var_args) {
return this.apply(this.nameArgs(Array.prototype.slice.call(arguments, 0)));
};
/**
* Call the function with a dictionary of named arguments.
*
* @param {Object} namedArgs A dictionary of arguments to the function.
* @return {ee.ComputedObject} An object representing the lazy result of
* the called function. If the signature specifies a recognized return
* type, the returned value will be cast to that type.
* @export
*/
ee.Function.prototype.apply = function(namedArgs) {
var result = new ee.ComputedObject(this, this.promoteArgs(namedArgs));
return /** @type {ee.ComputedObject} */(
ee.Function.promoter_(result, this.getReturnType()));
};
/**
* Call the function automatically deciding whether to interpret the arguments
* as positional ones or as a dictionary of named ones.
*
* @param {*|undefined} thisValue The "this" value on which the function was
* called. If defined, interpreted as the first argument.
* @param {Array<*>} args A list containing either positional args or a
* keyword arg dictionary.
* @return {ee.ComputedObject} An object representing the called function.
* If the signature specifies a recognized return type, the returned
* value will be cast to that type.
* @package
*/
ee.Function.prototype.callOrApply = function(thisValue, args) {
var isInstance = goog.isDef(thisValue);
var signature = this.getSignature();
// Assume keyword arguments if we get a single dictionary.
var useKeywordArgs = false;
if (args.length == 1 && ee.Types.isRegularObject(args[0])) {
// Decide whether the algorithm expects a dictionary as an only arg.
var params = signature['args'];
if (isInstance) {
params = params.slice(1);
}
if (params.length) {
var requiresOneArg = (params.length == 1 || params[1]['optional']);
var aSingleDictionaryIsValid =
(requiresOneArg && params[0]['type'] == 'Dictionary');
useKeywordArgs = !aSingleDictionaryIsValid;
}
}
// Convert positional to named args.
var namedArgs;
if (useKeywordArgs) {
namedArgs = goog.object.clone(/** @type {Object} */ (args[0]));
if (isInstance) {
var firstArgName = signature['args'][0]['name'];
if (firstArgName in namedArgs) {
throw Error('Named args for ' + signature['name'] +
' can\'t contain keyword ' + firstArgName);
}
namedArgs[firstArgName] = thisValue;
}
} else {
namedArgs = this.nameArgs(isInstance ? [thisValue].concat(args) : args);
}
return this.apply(namedArgs);
};
/**
* Promotes arguments to their types based on the function's signature and
* verifies that all required arguments are provided and no unknown arguments
* are present.
*
* @param {Object} args Keyword arguments to the function.
* @return {Object} The promoted arguments.
* @protected
*/
ee.Function.prototype.promoteArgs = function(args) {
var specs = this.getSignature()['args'];
// Promote all recognized args.
var promotedArgs = {};
var known = {};
for (var i = 0; i < specs.length; i++) {
var name = specs[i]['name'];
if (name in args && goog.isDef(args[name])) {
promotedArgs[name] = ee.Function.promoter_(args[name], specs[i]['type']);
} else if (!specs[i]['optional']) {
throw Error('Required argument (' + name + ') missing to function: ' +
this);
}
known[name] = true;
}
// Check for unknown arguments.
var unknown = [];
for (var argName in args) {
if (!known[argName]) {
unknown.push(argName);
}
}
if (unknown.length > 0) {
throw Error('Unrecognized arguments (' + unknown + ') to function: ' +
this);
}
return promotedArgs;
};
/**
* Converts a list of positional arguments to a map of keyword arguments
* using the function's signature. Note that this does not check whether
* the list contains enough arguments to satisfy the call.
*
* @param {Array} args Positional arguments to the function.
* @return {Object} Keyword arguments to the function.
* @protected
*/
ee.Function.prototype.nameArgs = function(args) {
var specs = this.getSignature()['args'];
if (specs.length < args.length) {
throw Error('Too many (' + args.length + ') arguments to function: ' +
this);
}
var namedArgs = {};
for (var i = 0; i < args.length; i++) {
namedArgs[specs[i]['name']] = args[i];
}
return namedArgs;
};
/**
* @return {string} The name of the return type of the function.
* @protected
*/
ee.Function.prototype.getReturnType = function() {
return this.getSignature()['returns'];
};
/**
* @param {string=} opt_name An optional override of the function name.
* @param {boolean=} opt_isInstance If true, the first argument is documented
* to be "this".
* @return {string} A user-friendly formatted description of the function based
* on its signature.
* @override
*/
ee.Function.prototype.toString = function(opt_name, opt_isInstance) {
var signature = this.getSignature();
var buffer = [];
buffer.push(opt_name || signature['name']);
buffer.push('(');
buffer.push(goog.array.map(signature['args'].slice(opt_isInstance ? 1 : 0),
function(elem) {
return elem['name'];
}).join(', '));
buffer.push(')\n');
buffer.push('\n');
if (signature['description']) {
buffer.push(signature['description']);
} else {
buffer.push('Undocumented.');
}
buffer.push('\n');
if (signature['args'].length) {
buffer.push('\nArgs:\n');
for (var i = 0; i < signature['args'].length; i++) {
if (opt_isInstance && i == 0) {
buffer.push(' this:');
} else {
buffer.push('\n ');
}
var arg = signature['args'][i];
buffer.push(arg['name']);
buffer.push(' (');
buffer.push(arg['type']);
if (arg['optional']) {
buffer.push(', optional');
}
buffer.push('): ');
if (arg['description']) {
buffer.push(arg['description']);
} else {
buffer.push('Undocumented.');
}
}
}
return buffer.join('');
};
/**
* @return {string} The serialized representation of this object.
*/
ee.Function.prototype.serialize = function() {
return ee.Serializer.toJSON(this);
};
/**
* The signature of a function.
*
* @typedef {{
* name: string,
* args: !Array.<ee.data.AlgorithmArgument>,
* returns: string,
* description: (string|undefined),
* deprecated: (string|undefined)
* }}
*/
ee.Function.Signature;