query_param_serializer.js
2.33 KB
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
var util = require('../util');
function QueryParamSerializer() {
}
QueryParamSerializer.prototype.serialize = function(params, shape, fn) {
serializeStructure('', params, shape, fn);
};
function ucfirst(shape) {
if (shape.isQueryName || shape.api.protocol !== 'ec2') {
return shape.name;
} else {
return shape.name[0].toUpperCase() + shape.name.substr(1);
}
}
function serializeStructure(prefix, struct, rules, fn) {
util.each(rules.members, function(name, member) {
var value = struct[name];
if (value === null || value === undefined) return;
var memberName = ucfirst(member);
memberName = prefix ? prefix + '.' + memberName : memberName;
serializeMember(memberName, value, member, fn);
});
}
function serializeMap(name, map, rules, fn) {
var i = 1;
util.each(map, function (key, value) {
var prefix = rules.flattened ? '.' : '.entry.';
var position = prefix + (i++) + '.';
var keyName = position + (rules.key.name || 'key');
var valueName = position + (rules.value.name || 'value');
serializeMember(name + keyName, key, rules.key, fn);
serializeMember(name + valueName, value, rules.value, fn);
});
}
function serializeList(name, list, rules, fn) {
var memberRules = rules.member || {};
if (list.length === 0) {
fn.call(this, name, null);
return;
}
util.arrayEach(list, function (v, n) {
var suffix = '.' + (n + 1);
if (rules.api.protocol === 'ec2') {
// Do nothing for EC2
suffix = suffix + ''; // make linter happy
} else if (rules.flattened) {
if (memberRules.name) {
var parts = name.split('.');
parts.pop();
parts.push(ucfirst(memberRules));
name = parts.join('.');
}
} else {
suffix = '.' + (memberRules.name ? memberRules.name : 'member') + suffix;
}
serializeMember(name + suffix, v, memberRules, fn);
});
}
function serializeMember(name, value, rules, fn) {
if (value === null || value === undefined) return;
if (rules.type === 'structure') {
serializeStructure(name, value, rules, fn);
} else if (rules.type === 'list') {
serializeList(name, value, rules, fn);
} else if (rules.type === 'map') {
serializeMap(name, value, rules, fn);
} else {
fn(name, rules.toWireFormat(value).toString());
}
}
/**
* @api private
*/
module.exports = QueryParamSerializer;