Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(575)

Side by Side Diff: mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl

Issue 2380303003: [Mojo] Make javascript enums extensible. (Closed)
Patch Set: rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 {%- macro enum_def(enum_name, enum) -%} 1 {%- macro enum_def(enum_name, enum) -%}
2 {{enum_name}} = {}; 2 {{enum_name}} = {};
3 {%- set prev_enum = 0 %} 3 {%- set prev_enum = 0 %}
4 {%- for field in enum.fields %} 4 {%- for field in enum.fields %}
5 {%- if field.value %} 5 {%- if field.value %}
6 {{enum_name}}.{{field.name}} = {{field.value|expression_to_text}}; 6 {{enum_name}}.{{field.name}} = {{field.value|expression_to_text}};
7 {%- elif loop.first %} 7 {%- elif loop.first %}
8 {{enum_name}}.{{field.name}} = 0; 8 {{enum_name}}.{{field.name}} = 0;
9 {%- else %} 9 {%- else %}
10 {{enum_name}}.{{field.name}} = {{enum_name}}.{{enum.fields[loop.index0 - 1].na me}} + 1; 10 {{enum_name}}.{{field.name}} = {{enum_name}}.{{enum.fields[loop.index0 - 1].na me}} + 1;
11 {%- endif %}
12 {%- endfor %}
13
14 {{enum_name}}.isKnownEnumValue = function(value) {
15 {%- if enum.fields %}
16 switch (value) {
17 {%- for enum_field in enum.fields|groupby('numeric_value') %}
18 case {{enum_field[0]}}:
19 {%- endfor %}
20 return true;
21 }
11 {%- endif %} 22 {%- endif %}
12 {%- endfor %} 23 return false;
24 };
25
26 {{enum_name}}.validate = function(enumValue) {
27 var isExtensible = {% if enum.extensible %}true{% else %}false{% endif %};
28 if (isExtensible || this.isKnownEnumValue(enumValue))
29 return validator.validationError.NONE;
30
31 return validator.validationError.UNKNOWN_ENUM_VALUE;
32 };
13 {%- endmacro %} 33 {%- endmacro %}
OLDNEW
« no previous file with comments | « mojo/public/js/validator.js ('k') | mojo/public/tools/bindings/generators/js_templates/validation_macros.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698