-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSemanticAttributes.swift.j2
More file actions
128 lines (112 loc) · 3.91 KB
/
SemanticAttributes.swift.j2
File metadata and controls
128 lines (112 loc) · 3.91 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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// DO **NOT** EDIT. This file is automatically generated.
{%- 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
{%- else -%}
{{type}}
{%- endif -%}
{%- endmacro %}
{%- macro print_value(type, value) -%}
{{ "\"" if type == "String"}}{{value}}{{ "\"" if type == "String"}}
{%- endmacro %}
import Foundation
public enum {{enum}}: String {
{%- for attribute in attributes | unique(attribute="fqn") %}
{%- set class_name = attribute.fqn | to_camelcase(True) ~ "Values" %}
/**
{{attribute.brief | to_doc_brief}}.
{%- if attribute.examples %}
~~~
// Examples
{%- for example in attribute.examples %}
{%- if attribute.attr_type|lower == "string" %}
attributes[.{{attribute.fqn | to_camelcase}}] = "{{example | replace("\"", "\\\"")}}"
{%- else -%}
attributes[.{{attribute.fqn | to_camelcase}}] = {{example}}
{%- endif -%}
{%- endfor %}
~~~
{%- endif %}
{%- if attribute.note %}
- Note: {{attribute.note | to_doc_brief | indent(6, False)}}.
{%- endif %}
{%- if attribute.deprecated %}
- Warning: Deprecated • {{attribute.deprecated | to_doc_brief}}.
{%- endif %}
{%- if attribute.is_enum %}
- Requires: Value should be one of [`{{enum}}.{{class_name}}`](x-source-tag://otel{{class_name}}) (of type `{{to_swift_return_type(attribute.attr_type|lower)}}`)
{% else %}
- Requires: Value type should be `{{to_swift_return_type(attribute.attr_type|lower)}}`
{% endif -%}
*/
{%- if attribute.deprecated %}
@available(*, deprecated)
{%- endif %}
case {{attribute.fqn | to_camelcase}} = "{{attribute.fqn}}"
{%- endfor %}
{%- if enum == "SemanticAttributes" %}
// 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/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/exceptions.md).
*/
case exception = "exception";
{%- endif %}
{% for attribute in attributes | unique(attribute="fqn") %}
{%- set class_name = attribute.fqn | to_camelcase(True) ~ "Values" %}
{%- set type = to_swift_return_type(attribute.attr_type.enum_type) %}
{%- if attribute.is_enum %}
/**
{{attribute.brief | to_doc_brief}}.
*/
/// - Tag: otel{{class_name}}
{%- if attribute.attr_type.custom_values %}
public struct {{class_name}}: CustomStringConvertible {
{%- for member in attribute.attr_type.members %}
/**
{{member.brief | to_doc_brief}}.
*/
public static let {{ member.member_id | to_camelcase }} = {{class_name}}({{ print_value(type, member.value) }})
{%- endfor %}
internal let value: String
public init(_ customValue: String) {
self.value = customValue
}
public var description: String {
return value
}
}
{% else %}
public enum {{class_name}}: {{ type }} {
{%- for member in attribute.attr_type.members %}
/**
{{member.brief | to_doc_brief}}.
*/
case {{ member.member_id | replace(".", "_") | replace("internal", "`internal`") }} = {{ print_value(type, member.value) }}
{%- endfor %}
}
{% endif %}
{%- endif %}
{%- endfor %}
}
{%- if enum == "ResourceAttributes" %}
public func ==(left: ResourceAttributes, right: String) -> Bool {
return left.rawValue == right
}
public func ==(left: String, right: ResourceAttributes) -> Bool {
return left == right.rawValue
}
{% endif %}