Chromium Code Reviews| Index: mojo/public/tools/bindings/generators/go_templates/mojom_type_macros.tmpl |
| diff --git a/mojo/public/tools/bindings/generators/go_templates/mojom_type_macros.tmpl b/mojo/public/tools/bindings/generators/go_templates/mojom_type_macros.tmpl |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..44ed74f3b9fb5259f87f280d9d57d02d4d84e6ac |
| --- /dev/null |
| +++ b/mojo/public/tools/bindings/generators/go_templates/mojom_type_macros.tmpl |
| @@ -0,0 +1,219 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +{%- macro writeMojomType(typepkg, t, pkg, exported=true, topLevel=true) -%} |
|
azani
2015/10/19 18:27:02
Maybe split this into several macros just to impro
alexfandrianto
2015/10/19 21:36:30
Done.
|
| +{%- if t|identifier(pkg)|identifier_check('WriteMojomType') -%} {#- Already printed out -#} |
| +{{writeTypeTypeReference(typepkg, t, pkg)}} |
| +{%- else -%} |
| + |
| +{%- if t|mojom_type(typepkg) != "" -%}{#- simple kind case -#} |
| + &{{t|mojom_type(typepkg)}} |
| +{%- elif t|is_array -%} |
| + &{{typepkg}}TypeArrayType{ |
| + Value: {{typepkg}}ArrayType{ |
| + {%- if t|is_nullable -%} |
| + Nullable: true, |
| + {%- endif -%} |
| + {%- if t.length is not none -%} |
| + FixedLength: {{t.length}}, |
| + {%- endif -%} |
| + ElementType: {{writeMojomType(typepkg, t.kind, pkg, exported, false)}}, |
| + }, |
| + } |
| +{%- elif t|is_map -%} |
| + &{{typepkg}}TypeMapType{ |
| + Value: {{typepkg}}MapType{ |
| + {%- if t|is_nullable -%} |
| + Nullable: true, |
| + {%- endif -%} |
| + KeyType: {{writeMojomType(typepkg, t.key_kind, pkg, exported, false)}}, |
| + ValueType: {{writeMojomType(typepkg, t.value_kind, pkg, exported, false)}}, |
| + }, |
| + } |
| +{%- elif t|is_enum -%} |
| + {%- if topLevel -%} |
| + {{- t|identifier(pkg)|identifier_store('WriteMojomType') -}} |
| + {{typepkg}}MojomEnum{ |
| + DeclData: &{{typepkg}}DeclarationData{ |
| + ShortName: &enumName_{{t|name}}, |
| + }, |
| + Values: []{{typepkg}}EnumValue{ |
| + {%- for field in t.fields -%} |
| + {{typepkg}}EnumValue{ |
| + Value: {{typepkg}}ConstantOccurrence{ |
| + Value: &{{typepkg}}ConstantValueEnumValue{ |
| + Value: {{typepkg}}EnumConstantValue{ |
| + EnumType: {{writeTypeReference(typepkg, t, pkg)}}, |
| + EnumValueName: &enumFieldName_{{t|name}}_{{field|name}}, |
| + IntValue: int32({{field.numeric_value}}), |
| + }, |
| + }, |
| + }, |
| + }, |
| + {%- endfor -%} |
| + }, |
| + } |
| + {%- else -%} |
| + {{writeTypeTypeReference(typepkg, t, pkg)}} |
| + {%- endif -%} |
| +{%- elif t|is_struct -%} |
| + {%- if topLevel -%} |
| + {{- t|identifier(pkg)|identifier_store('WriteMojomType') -}} |
| + {{typepkg}}MojomStruct{ |
| + DeclData: &{{typepkg}}DeclarationData{ |
| + ShortName: &structName_{{t|name}}, |
| + }, |
| + {%- if t|is_nullable -%} |
| + Nullable: true, |
| + {%- endif -%} |
| + Fields: []{{typepkg}}StructField{ |
| + {%- for field in t.fields -%} |
| + {{typepkg}}StructField{ |
| + DeclData: &{{typepkg}}DeclarationData{ |
| + ShortName: &structFieldName_{{t|name}}_{{field|name}}, |
| + }, |
| + FieldType: {{writeMojomType(typepkg, field.kind, pkg, exported, false)}}, |
| + }, |
| + {%- endfor -%} |
| + }, |
| + } |
| + {%- else -%} |
| + {{writeTypeTypeReference(typepkg, t, pkg)}} |
| + {%- endif -%} |
| +{%- elif t|is_union -%} |
| + {%- if topLevel -%} |
| + {{- t|identifier(pkg)|identifier_store('WriteMojomType') -}} |
| + {{typepkg}}MojomUnion{ |
| + DeclData: &{{typepkg}}DeclarationData{ |
| + ShortName: &unionName_{{t|name}}, |
| + }, |
| + {%- if t|is_nullable -%} |
| + Nullable: true, |
| + {%- endif -%} |
| + Fields: []{{typepkg}}UnionField{ |
| + {%- for field in t.fields -%} |
| + {{typepkg}}UnionField{ |
| + DeclData: &{{typepkg}}DeclarationData{ |
| + ShortName: &unionFieldName_{{t|name}}_{{field|name}}, |
| + }, |
| + Type: {{writeMojomType(typepkg, field.kind, pkg, exported, false)}}, |
| + Tag: {{field.ordinal}}, |
| + }, |
| + {%- endfor -%} |
| + }, |
| + } |
| + {%- else -%} |
| + {{writeTypeTypeReference(typepkg, t, pkg)}} |
| + {%- endif -%} |
| +{%- elif t|is_interface or t|is_interface_request -%} |
| + {{- t|identifier(pkg)|identifier_store('WriteMojomType') -}} |
| + {{writeTypeTypeReference(typepkg, t, pkg)}} |
| +{%- else -%} |
| + ERROR: UNSUPPORTED TYPE |
| +{%- endif -%} |
| +{%- endif -%} |
| +{%- endmacro -%} |
| + |
| +{%- macro writeTypeTypeReference(typepkg, t, pkg) -%} |
| +&{{typepkg}}TypeTypeReference{ |
| + Value: {{writeTypeReference(typepkg, t, pkg)}}, |
| +} |
| +{%- endmacro -%} |
| + |
| +{%- macro writeTypeReference(typepkg, t, pkg) -%} |
| +{{typepkg}}TypeReference { |
| + {%- if t|is_nullable -%} |
| + Nullable: true, |
| + {%- endif -%} |
| + {%- if t|is_interface_request -%}{# Interface request collapses to interface. #} |
| + IsInterfaceRequest: true, |
| + Identifier: &{{writePackagedTypeID(typepkg, t.kind, pkg)}}, |
| + TypeKey: &{{writePackagedTypeID(typepkg, t.kind, pkg)}}, |
| + {%- else -%} |
| + Identifier: &{{writePackagedTypeID(typepkg, t, pkg)}}, |
| + TypeKey: &{{writePackagedTypeID(typepkg, t, pkg)}}, |
| + {%- endif -%} |
| +} |
| +{%- endmacro -%} |
| + |
| +{%- macro writePackagedTypeID(typepkg, t, pkg) -%} |
| +{%- if t|package != '' -%}{{t|package}}.{%- endif -%}ID_{{t|mojom_type_identifier(pkg)}} |
| +{%- endmacro -%} |
| + |
| + |
| +{%- macro writeMojomTypeDef(typepkg, t, pkg, exported=True) -%} |
| +{%- if t|is_enum -%} |
| +// String names and labels used by the MojomEnum types. |
| +var ( |
| + enumName_{{t|name}} = "{{t|name}}" |
| +{% for field in t.fields %} |
| + enumFieldName_{{t|name}}_{{field|name}} = "{{field|name}}" |
| +{% endfor %} |
| +) |
| + |
| +func {{t|mojom_type_identifier(pkg)}}() {{typepkg}}MojomEnum { |
| + return {{ writeMojomType(typepkg, t, pkg, exported) }} |
| +} |
| +{%- elif t|is_struct -%} |
| +// String names and labels used by the MojomStruct types. |
| +var ( |
| + structName_{{t|name}} = "{{t|name}}" |
| +{% for field in t.fields %} |
| + structFieldName_{{t|name}}_{{field|name}} = "{{field|name}}" |
| +{% endfor %} |
| +) |
| + |
| +func {{t|mojom_type_identifier(pkg)}}() {{typepkg}}MojomStruct { |
| + return {{ writeMojomType(typepkg, t, pkg, exported) }} |
| +} |
| +{%- elif t|is_union -%} |
| +// String names and labels used by the MojomUnion types. |
| +var ( |
| + unionName_{{t|name}} = "{{t|name}}" |
| +{% for field in t.fields %} |
| + unionFieldName_{{t|name}}_{{field|name}} = "{{field|name}}" |
| +{% endfor %} |
| +) |
| + |
| +func {{t|mojom_type_identifier(pkg)}}() {{typepkg}}MojomUnion { |
| + return {{ writeMojomType(typepkg, t, pkg, exported) }} |
| +} |
| +{%- elif t|is_interface -%} |
| +var ( |
| + interfaceName_{{t|name}} = "{{t|name}}" |
| +{% for method in t.methods %} |
| + interfaceMethodName_{{t|name}}_{{method|name}} = "{{method|name}}" |
| +{% endfor %} |
| +) |
| + |
| +func {{t|mojom_type_identifier(pkg)}}() {{typepkg}}MojomInterface { |
| + responseParamsMap := make(map[string]*{{typepkg}}MojomStruct) |
| + _ = responseParamsMap // To avoid the declared but unused compiler error |
| +{% for method in t.methods %} |
| + {% if method|has_response %} |
| + mstruct_{{method|name}} := {{method.response_param_struct|mojom_type_identifier(pkg)}}() |
| + responseParamsMap[interfaceMethodName_{{t|name}}_{{method|name}}] = &mstruct_{{method|name}} |
| + {% endif %} |
| +{% endfor %} |
| + return {{typepkg}}MojomInterface{ |
| + DeclData: &{{typepkg}}DeclarationData{ |
| + ShortName: &interfaceName_{{t|name}}, |
| + }, |
| + Methods: map[uint32]{{typepkg}}MojomMethod{ |
| +{%- for method in t.methods -%} |
| + {{t|name(False)}}_{{method|name}}_Name: {{typepkg}}MojomMethod{ |
| + DeclData: &{{typepkg}}DeclarationData{ |
| + ShortName: &interfaceMethodName_{{t|name}}_{{method|name}}, |
| + }, |
| + Parameters: {{method.param_struct|mojom_type_identifier(pkg)}}(), |
| + ResponseParams: responseParamsMap[interfaceMethodName_{{t|name}}_{{method|name}}], |
| + }, |
| +{%- endfor -%} |
| + }, |
| + } |
| +} |
| +{%- else -%} |
| +BAD TYPE NAME GIVEN |
| +{%- endif -%} |
| +{%- endmacro -%} |