|
| 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) -%} |
| 17 | +{{ repl(smart_title_case(fqn | replace(" ", "") | replace("_", ".") | acronym)) }} |
| 18 | +{%- endmacro -%} |
| 19 | + |
| 20 | +{%- macro deprecated_doc(attr) -%} |
| 21 | +{% if attr is deprecated %}Deprecated: {{ attr.deprecated }}{% endif %} |
| 22 | +{%- endmacro -%} |
| 23 | + |
| 24 | +{%- macro notes_doc(attr) -%} |
| 25 | +{% if attr.note %}Note: {{ attr.note }}{% endif %} |
| 26 | +{%- endmacro -%} |
| 27 | + |
| 28 | +{%- macro examples_doc(attr) -%} |
| 29 | +{%- if attr.examples is iterable %} |
| 30 | +Examples: {{ attr.examples | trim("[]") }} |
| 31 | +{%- endif %} |
| 32 | +{%- endmacro -%} |
| 33 | + |
| 34 | +{%- macro it_reps(brief) -%} |
| 35 | +{%- set brief = brief | trim() -%} |
| 36 | +It represents {% if brief[:2] == "A " or brief[:3] == "An " or brief[:4] == "The " -%} |
| 37 | + {{ brief[0]|lower }}{{ brief[1:] | trim(".") }}. |
| 38 | +{%- else -%} |
| 39 | + the {{ brief[0]|lower }}{{ brief[1:] | trim(".") }}. |
| 40 | +{%- endif -%} |
| 41 | +{%- endmacro -%} |
| 42 | + |
| 43 | +{%- macro keydoc(attr) -%} |
| 44 | +{{ to_go_name(attr.name) }}Key is the attribute Key conforming to the "{{ attr.name }}" semantic conventions. {{ it_reps(attr.brief) }} |
| 45 | + |
| 46 | +{% if attr is enum -%} |
| 47 | +Type: Enum |
| 48 | +{%- else -%} |
| 49 | +Type: {{ attr.type }} |
| 50 | +{%- endif %} |
| 51 | +RequirementLevel: {{ attr.requirement_level | title }} |
| 52 | +Stability: {{ attr.stability | title }} |
| 53 | +{{ examples_doc(attr) }} |
| 54 | +{{ notes_doc(attr) }} |
| 55 | +{{ deprecated_doc(attr) }} |
| 56 | +{%- endmacro -%} |
| 57 | + |
| 58 | +{%- macro generate_consts(group) -%} |
| 59 | +{#- TODO: generate with group docs (i.e group by registry namespace) #} |
| 60 | + |
| 61 | +{{ ["Namespace: " ~ group.root_namespace] | comment(format="go") }} |
| 62 | +const ( |
| 63 | +{%- for attribute in group.attributes if not attribute.deprecated %} |
| 64 | +{#- TODO: Handle template attributes. #} |
| 65 | +{%- if not attribute.type is template_type %} |
| 66 | +{{ keydoc(attribute) | comment(format="go_1tab") }} |
| 67 | + {{to_go_name(attribute.name)}}Key = attribute.Key("{{attribute.name}}") |
| 68 | +{% endif -%} |
| 69 | +{%- endfor -%} |
| 70 | +) |
| 71 | +{%- endmacro -%} |
| 72 | + |
| 73 | +{%- macro generate_funcs(group) -%} |
| 74 | +{%- for attribute in group.attributes if not attribute is enum %} |
| 75 | +{#- TODO: Handle template attributes. #} |
| 76 | +{%- if not attribute.type is template_type %} |
| 77 | + |
| 78 | +{{ [to_go_name(attribute.name) ~ " returns an attribute KeyValue conforming to the \"" ~ attribute.name ~ "\" semantic conventions. " ~ it_reps(attribute.brief) ] | comment(format="go") }} |
| 79 | +func {{to_go_name(attribute.name)}}(val {{attribute.type | instantiated_type | map_text("attribute_type_value")}}) attribute.KeyValue { |
| 80 | + return {{to_go_name(attribute.name)}}Key.{{attribute.type | instantiated_type | map_text("attribute_type_method")}}(val) |
| 81 | +} |
| 82 | +{%- endif %} |
| 83 | +{%- endfor %} |
| 84 | +{%- endmacro -%} |
| 85 | + |
| 86 | +{%- macro generate_vars(group) -%} |
| 87 | +{#- Render values for enums #} |
| 88 | +{%- for attribute in group.attributes %} |
| 89 | +{%- if attribute is enum %} |
| 90 | + |
| 91 | +{{ ["Enum values for " ~ attribute.name] | comment(format="go") }} |
| 92 | +var ( |
| 93 | +{%- for value in attribute.type.members %} |
| 94 | +{%- if value.deprecated %} |
| 95 | +{{ ["Deprecated: " ~ value.deprecated | trim(".") ~ "." ] | comment(format="go_1tab") }} |
| 96 | +{%- else %} |
| 97 | +{{ [value.brief or value.id, "Stability: " ~ value.stability] | comment(format="go_1tab") }} |
| 98 | +{%- endif %} |
| 99 | + {{to_go_name(attribute.name ~ "." ~ value.id)}} = {{ to_go_name(attribute.name) }}Key.{{attribute.type | instantiated_type | map_text("attribute_type_method")}}({{ value.value | print_member_value }}) |
| 100 | +{%- endfor %} |
| 101 | +) |
| 102 | +{%- endif %} |
| 103 | +{%- endfor %} |
| 104 | +{%- endmacro -%} |
| 105 | + |
| 106 | +{%- macro metric_keydoc(metric) -%} |
| 107 | +{%- if not metric.brief -%} |
| 108 | +{{ to_go_name(metric.metric_name) }} is the metric conforming to the "{{ metric.metric_name}}" semantic conventions. |
| 109 | +{%- else -%} |
| 110 | +{{ to_go_name(metric.metric_name) }} is the metric conforming to the "{{ metric.metric_name}}" semantic conventions. {{ it_reps(metric.brief)|trim(".") }}. |
| 111 | +{%- endif %} |
| 112 | +{%- endmacro -%} |
0 commit comments