Enum.js
1.9 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
/**
* Constructs a new Enum.
* @exports ProtoBuf.Reflect.Enum
* @param {!ProtoBuf.Builder} builder Builder reference
* @param {!ProtoBuf.Reflect.T} parent Parent Reflect object
* @param {string} name Enum name
* @param {Object.<string,*>=} options Enum options
* @param {string?} syntax The syntax level (e.g., proto3)
* @constructor
* @extends ProtoBuf.Reflect.Namespace
*/
var Enum = function(builder, parent, name, options, syntax) {
Namespace.call(this, builder, parent, name, options, syntax);
/**
* @override
*/
this.className = "Enum";
/**
* Runtime enum object.
* @type {Object.<string,number>|null}
* @expose
*/
this.object = null;
};
/**
* Gets the string name of an enum value.
* @param {!ProtoBuf.Builder.Enum} enm Runtime enum
* @param {number} value Enum value
* @returns {?string} Name or `null` if not present
* @expose
*/
Enum.getName = function(enm, value) {
var keys = Object.keys(enm);
for (var i=0, key; i<keys.length; ++i)
if (enm[key = keys[i]] === value)
return key;
return null;
};
/**
* @alias ProtoBuf.Reflect.Enum.prototype
* @inner
*/
var EnumPrototype = Enum.prototype = Object.create(Namespace.prototype);
/**
* Builds this enum and returns the runtime counterpart.
* @param {boolean} rebuild Whether to rebuild or not, defaults to false
* @returns {!Object.<string,number>}
* @expose
*/
EnumPrototype.build = function(rebuild) {
if (this.object && !rebuild)
return this.object;
var enm = new ProtoBuf.Builder.Enum(),
values = this.getChildren(Enum.Value);
for (var i=0, k=values.length; i<k; ++i)
enm[values[i]['name']] = values[i]['id'];
if (Object.defineProperty)
Object.defineProperty(enm, '$options', {
"value": this.buildOpt(),
"enumerable": false
});
return this.object = enm;
};