forked from open-telemetry/opentelemetry-swift-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAttributes.swift.j2
More file actions
166 lines (142 loc) · 5.15 KB
/
SemanticAttributes.swift.j2
File metadata and controls
166 lines (142 loc) · 5.15 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{%- macro reserved_words_filter(string) -%}
{%- if string in ["internal", "extension", "defer", "static", "default", "as", "in"] -%}
_{{string}}
{%- else -%}
{{string}}
{%- endif -%}
{%- endmacro -%}
{%- macro to_swift_return_type(type) -%}
{% if type == "string" %}
String
{%- elif type == "string[]" %}
[String]
{%- elif type == "boolean" %}
Bool
{%- elif type == "number" %}
Int
{%- elif type == "int" %}
Int
{%- elif type == "double" %}
Double
{%- else %}
{{type}}
{%- endif %}
{%- endmacro -%}
{%- macro print_value(type, value) -%}
{{ "\"" if type == "String"}}{{value}}{{ "\"" if type == "String"}}
{%- endmacro -%}
{%- set enum_name = ctx.root_namespace | pascal_case | replace('-', "") %}
{%- set filename = ctx.output + (ctx.root_namespace | pascal_case | replace('-', '')) ~ "_attributes.swift" -%}
{{ template.set_file_name(filename) }}
{%- macro print_example(attribute, example) -%}
{%- set attribute_name = "SemanticConventions." + ctx.root_namespace|pascal_case + "." + attribute.name | replace(ctx.root_namespace, "") | camel_case ~ ".rawValue" -%}
{% if attribute.type|lower == "string" or attribute.type|lower == "template[string]" -%}
attributes[{{attribute_name}}] = "{{example | replace("\"", "\\\"")}}"
{% elif attribute is enum %}
attributes[{{attribute_name}}] = .{{example}}
{% else %}
attributes[{{attribute_name}}] = {{example}}
{%- endif %}
{%- endmacro -%}
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// DO **NOT** EDIT. This file is automatically generated.
import Foundation
extension SemanticConventions {
public enum {{ enum_name }}: String {
{% set attributes = ctx.attributes %}
{% if ctx.root_namespace == "exception" %}
// MARK: - Manual Definitions
// Some definitions have not yet been added to the YAML which generates this script.
// As such as we have some manually defined cases.
/**
An exception event **MUST** be called "exception" as per the [specification](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-spans.md#exception-event).
*/
case exception = "exception"
{% endif %}
{% for attribute in attributes | unique(attribute="name") %}
{% set class_name = attribute.name | replace(ctx.root_namespace, '') | pascal_case ~ "Values" %}
/**
{{attribute.brief | comment }}
{% if attribute.examples %}
- Examples:
```
{% if attribute.examples is sequence %}
{% for example in attribute.examples -%}
{{ print_example(attribute, example) | indent(6, true) }}
{% endfor %}
{% else -%}
{{ print_example(attribute, attribute.examples) | indent(6, true) }}
{% endif %}
```
{% endif %}
{% if attribute.note %}
- Note: {{attribute.note | comment(indent=7) }}
{% endif %}
{% if attribute.deprecated %}
- Warning: Deprecated • {{attribute.deprecated | comment}}
{% endif %}
{% if attribute is enum %}
- Requires: Value should be one of ``{{class_name}}`` (of type `{{to_swift_return_type(attribute.type | enum_type |lower)}}`)
{% else %}
- Requires: Value type should be `{{to_swift_return_type(attribute.type|lower)}}`
{% endif %}
*/
{% if attribute.deprecated %}
@available(*, deprecated)
{% endif %}
case {{reserved_words_filter(attribute.name | replace(ctx.root_namespace, "") | camel_case)}} = "{{attribute.name}}"
{% endfor %}
{% for attribute in attributes | unique(attribute="name") %}
{% set class_name = attribute.name | replace(ctx.root_namespace, "") | pascal_case ~ "Values" %}
{% set type = to_swift_return_type(attribute.type) %}
{% if attribute is enum %}
/**
{{attribute.brief | comment}}
*/
{% if attribute is enum %}
public struct {{class_name}}: CustomStringConvertible {
{% for member in attribute.type.members %}
{% if member.brief is defined %}
/// {{member.brief | comment}}
{% endif %}
public static let {{ reserved_words_filter(member.id | camel_case) }} = {{class_name}}({{ print_value(to_swift_return_type(attribute.type | enum_type), member.value) }})
{% endfor %}
internal let value: {{ to_swift_return_type(attribute.type | enum_type) }}
public init(_ customValue: {{to_swift_return_type(attribute.type | enum_type)}}) {
self.value = customValue
}
public var description: String {
{%- if attribute.type | enum_type == "string" %}
return value
{% else %}
return "\(value)"
{% endif %}
}
}
{% else %}
public enum {{class_name}}: {{ type }} {
{% for member in attribute.type.members -%}
{% if member.brief is defined -%}
/*
{{member.brief | comment}}
*/
{% endif %}
case {{ member.member_id | replace(".", "_") | replace("internal", "`internal`") }} = {{ print_value(type, member.value) }}
{% endfor %}
}
{% endif %}
{% endif %}
{% endfor %}
{% if filename == "ResourceAttributes" %}
public func ==(left: ResourceAttributes, right: String) -> Bool {
return left.rawValue == right
}
public func ==(left: String, right: ResourceAttributes) -> Bool {
return left == right.rawValue
}
{% endif %}
}
}