summaryrefslogtreecommitdiffstats
path: root/third_party/python/glean_parser/glean_parser/templates/swift.jinja2
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/python/glean_parser/glean_parser/templates/swift.jinja2')
-rw-r--r--third_party/python/glean_parser/glean_parser/templates/swift.jinja2139
1 files changed, 139 insertions, 0 deletions
diff --git a/third_party/python/glean_parser/glean_parser/templates/swift.jinja2 b/third_party/python/glean_parser/glean_parser/templates/swift.jinja2
new file mode 100644
index 0000000000..82ad37bf20
--- /dev/null
+++ b/third_party/python/glean_parser/glean_parser/templates/swift.jinja2
@@ -0,0 +1,139 @@
+// -*- mode: Swift -*-
+
+// AUTOGENERATED BY glean_parser v{{ parser_version }}. DO NOT EDIT. DO NOT COMMIT.
+{# The rendered markdown is autogenerated, but this
+Jinja2 template is not. Please file bugs! #}
+
+#if canImport(Foundation)
+ import Foundation
+#endif
+
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+{% macro obj_declaration(obj, suffix='', access='') %}
+{{ access }}static let {{ obj.name|camelize|variable_name }}{{ suffix }} = {{ obj|type_name }}( // generated from {{ obj.identifier() }}
+ CommonMetricData(
+ {% for arg_name in common_metric_args if obj[arg_name] is defined %}
+ {{ arg_name|camelize }}: {{ obj[arg_name]|swift }}{{ "," if not loop.last }}
+ {% endfor %}
+ )
+ {% for arg_name in extra_metric_args if obj[arg_name] is defined %}
+ , {{ obj[arg_name]|swift }}
+ {% endfor %}
+ )
+{% endmacro %}
+
+{% macro struct_decl(obj, name, suffix) %}
+struct {{ obj.name|Camelize }}{{ suffix }}: EventExtras {
+ {% for item, typ in obj|attr(name) %}
+ var {{ item|camelize|variable_name }}: {{typ|extra_type_name}}?
+ {% endfor %}
+
+ func toExtraRecord() -> [String: String] {
+ var record = [String: String]()
+
+ {% for item in obj|attr(name) %}
+ if let {{ item[0]|camelize }} = self.{{item[0]|camelize}} {
+ record["{{item[0]}}"] = String({{ item[0]|camelize }})
+ }
+ {% endfor %}
+
+ return record
+ }
+ }
+{% endmacro %}
+
+{% if not allow_reserved %}
+import {{ glean_namespace }}
+
+{% endif %}
+// swiftlint:disable superfluous_disable_command
+// swiftlint:disable nesting
+// swiftlint:disable line_length
+// swiftlint:disable identifier_name
+// swiftlint:disable force_try
+
+extension {{ namespace }} {
+ {% if build_info %}
+ class GleanBuild {
+ private init() {
+ // Intentionally left private, no external user can instantiate a new global object.
+ }
+
+ public static let info = BuildInfo(buildDate: {{ build_info.build_date }})
+ }
+ {% endif %}
+
+ {% for category in categories %}
+ {% if category.contains_pings %}
+ class {{ category.name|Camelize }} {
+ public static let shared = {{ category.name|Camelize }}()
+ private init() {
+ // Intentionally left private, no external user can instantiate a new global object.
+ }
+
+ {% for obj in category.objs.values() %}
+ {% if obj|attr("_generate_enums") %}
+ {% for name, suffix in obj["_generate_enums"] %}
+ {% if obj|attr(name)|length %}
+ enum {{ obj.name|Camelize }}{{ suffix }}: Int, ReasonCodes {
+ {% for key in obj|attr(name) %}
+ case {{ key|camelize|variable_name }} = {{ loop.index-1 }}
+ {% endfor %}
+
+ public func index() -> Int {
+ return self.rawValue
+ }
+ }
+
+ {% endif %}
+ {% endfor %}
+ {% endif %}
+ /// {{ obj.description|wordwrap() | replace('\n', '\n /// ') }}
+ let {{ obj.name|camelize|variable_name }} = {{obj|type_name}}(
+ name: {{ obj.name|swift }},
+ includeClientId: {{obj.include_client_id|swift}},
+ sendIfEmpty: {{obj.send_if_empty|swift}},
+ preciseTimestamps: {{obj.precise_timestamps|swift}},
+ reasonCodes: {{obj.reason_codes|swift}}
+ )
+
+ {% endfor %}
+ }
+
+ {% else %}
+ enum {{ category.name|Camelize }} {
+ {% for obj in category.objs.values() %}
+ {% if obj|attr("_generate_enums") %}
+ {% for name, suffix in obj["_generate_enums"] %}
+ {% if obj|attr(name)|length %}
+ {{ struct_decl(obj, name, suffix)|indent }}
+ {% endif %}
+ {% endfor %}
+ {% endif %}
+ {% endfor %}
+ {% for obj in category.objs.values() %}
+ {% if obj.labeled %}
+ {{ obj_declaration(obj, 'Label', 'private ') | indent }}
+ /// {{ obj.description|wordwrap() | replace('\n', '\n /// ') }}
+ static let {{ obj.name|camelize|variable_name }} = try! LabeledMetricType<{{ obj|type_name }}>( // generated from {{ obj.identifier() }}
+ category: {{ obj.category|swift }},
+ name: {{ obj.name|swift }},
+ sendInPings: {{ obj.send_in_pings|swift }},
+ lifetime: {{ obj.lifetime|swift }},
+ disabled: {{ obj.is_disabled()|swift }},
+ subMetric: {{ obj.name|camelize }}Label,
+ labels: {{ obj.labels|swift }}
+ )
+
+ {% else %}
+ /// {{ obj.description|wordwrap() | replace('\n', '\n /// ') }}
+ {{ obj_declaration(obj) | indent }}
+ {% endif %}
+ {% endfor %}
+ }
+
+ {% endif %}
+ {% endfor %}
+}