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

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

Issue 2394873003: [Mojo] Make javascript enums extensible. (Closed)
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl
diff --git a/mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl b/mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl
index 4ae0a9b5cdb4d3b72e8aab6d64cfc9173211421d..019b1b638363e9f8bebec89a5f876536fcbc02bd 100644
--- a/mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl
@@ -1,13 +1,33 @@
{%- macro enum_def(enum_name, enum) -%}
{{enum_name}} = {};
-{%- set prev_enum = 0 %}
-{%- for field in enum.fields %}
-{%- if field.value %}
+{%- set prev_enum = 0 %}
+{%- for field in enum.fields %}
+{%- if field.value %}
{{enum_name}}.{{field.name}} = {{field.value|expression_to_text}};
-{%- elif loop.first %}
+{%- elif loop.first %}
{{enum_name}}.{{field.name}} = 0;
-{%- else %}
+{%- else %}
{{enum_name}}.{{field.name}} = {{enum_name}}.{{enum.fields[loop.index0 - 1].name}} + 1;
+{%- endif %}
+{%- endfor %}
+
+ {{enum_name}}.isKnownEnumValue = function(value) {
+{%- if enum.fields %}
+ switch (value) {
+{%- for enum_field in enum.fields|groupby('numeric_value') %}
+ case {{enum_field[0]}}:
+{%- endfor %}
+ return true;
+ }
{%- endif %}
-{%- endfor %}
+ return false;
+ };
+
+ {{enum_name}}.validate = function(enumValue) {
+ var isExtensible = {% if enum.extensible %}true{% else %}false{% endif %};
+ if (isExtensible || this.isKnownEnumValue(enumValue))
+ return validator.validationError.NONE;
+
+ return validator.validationError.UNKNOWN_ENUM_VALUE;
+ };
{%- endmacro %}
« 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