summaryrefslogtreecommitdiffstats
path: root/third_party/python/glean_parser/glean_parser/templates/swift.jinja2
blob: 82ad37bf202fd38d84366aa86d3f390890a96d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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 %}
}