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

Unified Diff: mojo/public/bindings/generators/js_templates/struct_definition.tmpl

Issue 139613005: Support default values for complex objects in mojom. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 11 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/bindings/generators/js_templates/struct_definition.tmpl
diff --git a/mojo/public/bindings/generators/js_templates/struct_definition.tmpl b/mojo/public/bindings/generators/js_templates/struct_definition.tmpl
index 1ab7b94f18605c892b0ac577ef9463b612dea601..622f4a382bf3d3edfe39522db519fdbfa53eac38 100644
--- a/mojo/public/bindings/generators/js_templates/struct_definition.tmpl
+++ b/mojo/public/bindings/generators/js_templates/struct_definition.tmpl
@@ -1,6 +1,56 @@
+{%- macro set_default(kind, value, depth) -%}
+{#--- Strings ---#}
+{%- if kind|is_string_kind -%}
+{{caller(value)}}
+{#--- Arrays ---#}
+{%- elif kind|is_array_kind %}
+{%- set _ = value|verify_token_type("ARRAY") %}
+{
+ var tmp{{depth}} = [];
+{%- for element in value[1] %}
+{%- filter indent(2) %}
+{%- call(result) set_default(kind.kind, element, depth+1) %}
+tmp{{depth}}[{{loop.index0}}] = {{result}};
+{%- endcall %}
+{%- endfilter %}
+{%- endfor -%}
+ {{caller("tmp" ~ depth)|indent(2)}}
+}
+{#--- Objects ---#}
+{%- elif kind|is_object_kind %}
+{%- set _ = value|verify_token_type("OBJECT") %}
+{
+ var tmp{{depth}} = new {{kind.name}}();
+{%- set struct = structs|struct_by_name(kind.name) %}
+{%- for element in value[1] %}
+{#- Use struct.packed_fields to order struct values by ordinal number #}
+{%- set subfield = struct.fields[loop.index0] %}
+{%- filter indent(2) %}
+{%- call(result) set_default(subfield.kind, element, depth+1) %}
+tmp{{depth}}.{{subfield.name}} = {{result}};
+{%- endcall %}
+{%- endfilter %}
+{%- endfor -%}
+ {{caller("tmp" ~ depth)|indent(2)}}
+}
+{#--- POD types ---#}
+{%- else -%}
+{{caller(value)}}
+{%- endif %}
+{%- endmacro %}
+
+
function {{struct.name}}() {
{%- for packed_field in struct.packed.packed_fields %}
+{%- if packed_field.field.default %}
+{%- filter indent(4) %}
+{%- call(result) set_default(packed_field.field.kind, packed_field.field.default, 0) %}
+this.{{packed_field.field.name}} = {{result}};
+{%- endcall %}
+{%- endfilter %}
+{%- else %}
this.{{packed_field.field.name}} = {{packed_field.field|default_value}};
+{%- endif %}
{%- endfor %}
}

Powered by Google App Engine
This is Rietveld 408576698