Debian Codegraph @

semconv/templates/registry/go/helpers.j2

raw
1 {%- macro repl(text) -%}
2 {#- Copied from semconvgen: https://github.com/open-telemetry/opentelemetry-go-build-tools/blob/3e69152c51c56213b65c0fc6e5954293b522103c/semconvgen/generator.go#L419-L426 -#}
3 {{ text | replace("RedisDatabase", "RedisDB") | replace("IPTCP", "TCP") | replace("IPUDP", "UDP") | replace("Lineno", "LineNumber") }}
4 {%- endmacro -%}
5
6 {%- macro smart_title_case(text) -%}
7 {%- for i in range(0, text | length) -%}
8 {%- if i == 0 or text[i-1] in ['.', '_'] -%}
9 {{ text[i] | upper }}
10 {%- elif not text[i] in ['.', '_'] -%}
11 {{ text[i] }}
12 {%- endif -%}
13 {%- endfor -%}
14 {%- endmacro -%}
15
16 {%- macro to_go_name(fqn="", pkg="") -%}
17 {%- if pkg != "" and fqn != pkg -%}
18 {%- set n = pkg | length -%}
19 {%- if pkg == fqn[:n] -%}
20 {%- set fqn = fqn[n:] -%}
21 {%- if fqn[0] == "." or fqn[0] == "." -%}
22 {%- set fqn = fqn[1:] -%}
23 {%- endif -%}
24 {%- endif -%}
25 {%- endif -%}
26 {{ repl(smart_title_case(fqn | replace(" ", "") | replace("_", ".") | acronym)) }}
27 {%- endmacro -%}
28
29 {%- macro deprecated_doc(attr) -%}
30 {% if attr is deprecated %}Deprecated: {{ attr.deprecated }}{% endif %}
31 {%- endmacro -%}
32
33 {%- macro notes_doc(attr) -%}
34 {% if attr.note %}Note: {{ attr.note }}{% endif %}
35 {%- endmacro -%}
36
37 {%- macro examples_doc(attr) -%}
38 {%- if attr.examples is iterable %}
39 Examples: {{ attr.examples | trim("[]") }}
40 {%- endif %}
41 {%- endmacro -%}
42
43 {%- macro lower_first(line) -%}
44 {%- if line is string and line | length > 1 -%}
45 {%- if line[0] is upper and line[1] is upper -%}
46 {#- Assume an acronym -#}
47 {{ line }}
48 {%- else -%}
49 {{ line[0]|lower }}{{ line[1:] }}
50 {%- endif -%}
51 {%- elif line is not none -%}
52 {{ line }}
53 {%- endif -%}
54 {%- endmacro -%}
55
56 {%- macro first_word(line, delim=" ") -%}
57 {%- for c in line -%}
58 {%- if c == delim -%}
59 {{ line[:loop.index0] }}
60 {%- set line = "" -%}
61 {%- endif -%}
62 {%- endfor -%}
63 {%- endmacro -%}
64
65 {%- macro prefix_brief(brief, prefix="") -%}
66 {%- set norms = [
67 "MUST", "REQUIRED", "SHALL",
68 "SHOULD", "RECOMMENDED",
69 "MAY", "OPTIONAL"
70 ] -%}
71 {%- set brief = brief | trim() -%}
72 {%- if first_word(brief) is in norms -%}
73 It {{ brief }}.
74 {%- else -%}
75 {{ prefix }} {% if brief[:2] == "A " or brief[:3] == "An " or brief[:4] == "The " -%}
76 {{ lower_first(brief) | trim(".") }}.
77 {%- else -%}
78 the {{ lower_first(brief) | trim(".") }}.
79 {%- endif -%}
80 {%- endif -%}
81 {%- endmacro -%}
82
83 {%- macro it_reps(brief) -%}
84 {{ prefix_brief(brief, "It represents") }}
85 {%- endmacro -%}
86
87 {%- macro keydoc(attr, pkg="") -%}
88 {{ to_go_name(attr.name, pkg) }}Key is the attribute Key conforming to the "{{ attr.name }}" semantic conventions. {{ it_reps(attr.brief) }}
89
90 {% if attr is enum -%}
91 Type: Enum
92 {%- else -%}
93 Type: {{ attr.type }}
94 {%- endif %}
95 RequirementLevel: {{ attr.requirement_level | title }}
96 Stability: {{ attr.stability | title }}
97 {{ examples_doc(attr) }}
98 {{ notes_doc(attr) }}
99 {{ deprecated_doc(attr) }}
100 {%- endmacro -%}
101
102 {%- macro generate_consts(group, pkg="") -%}
103 {#- TODO: generate with group docs (i.e group by registry namespace) #}
104
105 {{ ["Namespace: " ~ group.root_namespace] | comment(format="go") }}
106 const (
107 {%- for attribute in group.attributes if not attribute.deprecated %}
108 {#- TODO: Handle template attributes. #}
109 {%- if not attribute.type is template_type %}
110 {{ keydoc(attribute) | comment(format="go_1tab") }}
111 {{to_go_name(attribute.name, pkg=pkg)}}Key = attribute.Key("{{attribute.name}}")
112 {% endif -%}
113 {%- endfor -%}
114 )
115 {%- endmacro -%}
116
117 {%- macro generate_funcs(group, pkg="") -%}
118 {%- for attribute in group.attributes if not attribute is enum %}
119 {#- TODO: Handle template attributes. #}
120 {%- if not attribute.type is template_type and attribute.type != "any" %}
121
122 {{ [to_go_name(attribute.name, pkg) ~ " returns an attribute KeyValue conforming to the \"" ~ attribute.name ~ "\" semantic conventions. " ~ it_reps(attribute.brief) ] | comment(format="go") }}
123 func {{to_go_name(attribute.name, pkg)}}(val {{attribute.type | instantiated_type | map_text("attribute_type_value")}}) attribute.KeyValue {
124 return {{to_go_name(attribute.name, pkg)}}Key.{{attribute.type | instantiated_type | map_text("attribute_type_method")}}(val)
125 }
126 {%- elif attribute.type is template_type %}
127
128 {{ [to_go_name(attribute.name, pkg) ~ " returns an attribute KeyValue conforming to the \"" ~ attribute.name ~ "\" semantic conventions. " ~ it_reps(attribute.brief) ] | comment(format="go") }}
129 func {{to_go_name(attribute.name, pkg)}}(key string, val {{attribute.type | instantiated_type | map_text("attribute_type_value")}}) attribute.KeyValue {
130 return attribute.{{attribute.type | instantiated_type | map_text("attribute_type_method")}}("{{attribute.name}}."+key, val)
131 }
132 {%- endif %}
133 {%- endfor %}
134 {%- endmacro -%}
135
136 {%- macro generate_vars(group, pkg="") -%}
137 {#- Render values for enums #}
138 {%- for attribute in group.attributes %}
139 {%- if attribute is enum %}
140
141 {{ ["Enum values for " ~ attribute.name] | comment(format="go") }}
142 var (
143 {%- for value in attribute.type.members if not value.deprecated %}
144 {{ [value.brief or value.id, "Stability: " ~ value.stability] | comment(format="go_1tab") }}
145 {{to_go_name(attribute.name ~ "." ~ value.id, pkg=pkg)}} = {{ to_go_name(attribute.name, pkg=pkg) }}Key.{{attribute.type | instantiated_type | map_text("attribute_type_method")}}({{ value.value | print_member_value }})
146 {%- endfor %}
147 )
148 {%- endif %}
149 {%- endfor %}
150 {%- endmacro -%}
151
152 {%- macro metric_keydoc(metric, pkg="") -%}
153 {%- if not metric.brief -%}
154 {{ to_go_name(metric.metric_name, pkg=pkg) }} is the metric conforming to the "{{ metric.metric_name}}" semantic conventions.
155 {%- else -%}
156 {{ to_go_name(metric.metric_name, pkg=pkg) }} is the metric conforming to the "{{ metric.metric_name}}" semantic conventions. {{ it_reps(metric.brief)|trim(".") }}.
157 {%- endif %}
158 {%- endmacro -%}
159
160 {%- macro metric_typedoc(metric, pkg="") -%}
161 {%- if not metric.brief -%}
162 {{ to_go_name(metric.metric_name, pkg=pkg) }} is an instrument used to record metric values conforming to the "{{ metric.metric_name}}" semantic conventions.
163 {%- else -%}
164 {{ to_go_name(metric.metric_name, pkg=pkg) }} is an instrument used to record metric values conforming to the "{{ metric.metric_name}}" semantic conventions. {{ it_reps(metric.brief)|trim(".") }}.
165 {%- endif %}
166 {%- endmacro -%}
167
168 {%- macro member_type(member) %}
169 {%- if member.value is string %}string{%- endif %}
170 {%- if member.value is boolean %}bool{%- endif %}
171 {%- if member.value is int %}int64{%- endif %}
172 {%- if member.value is float %}float64{%- endif %}
173 {%- endmacro %}
174
175 {%- macro attr_type(attribute) %}
176 {%- if attribute.type is mapping %}
177 {{- member_type(attribute.type.members[0]) }}
178 {%- elif attribute.type == "template[boolean]" %}boolean
179 {%- elif attribute.type == "template[int]" %}int
180 {%- elif attribute.type == "template[double]" %}double
181 {%- elif attribute.type == "template[string]" %}string
182 {%- elif attribute.type == "template[boolean[]]" %}boolean[]
183 {%- elif attribute.type == "template[int[]]" %}int[]
184 {%- elif attribute.type == "template[double[]]" %}double[]
185 {%- elif attribute.type == "template[string[]]" %}string[]
186 {%- else %}{{ attribute.type | trim }}
187 {%- endif %}
188 {%- endmacro %}