pgavlin / yomlette

MIT License
1 stars 0 forks source link

Add support for templates inside of strings. #6

Open pgavlin opened 3 years ago

pgavlin commented 3 years ago

i.e. interpolated strings.

For example, consider the following source:

apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-state-metrics.fullname" . }}
  namespace: {{ template "kube-state-metrics.namespace" . }}
  labels:
    app.kubernetes.io/name: {{ template "kube-state-metrics.name" . }}
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app.kubernetes.io/instance: "{{ .Release.Name }}"
    app.kubernetes.io/managed-by: "{{ .Release.Service }}"
{{- if .Values.customLabels }}
{{ toYaml .Values.customLabels | indent 4 }}
{{- end }}
  annotations:
    {{- if .Values.prometheusScrape }}
    prometheus.io/scrape: '{{ .Values.prometheusScrape }}'
    {{- end }}
    {{- if .Values.service.annotations }}
    {{- toYaml .Values.service.annotations | nindent 4 }}
    {{- end }}
spec:
  type: "{{ .Values.service.type }}"
  ports:
  - name: "http"
    protocol: TCP
    port: {{ .Values.service.port }}
  {{- if .Values.service.nodePort }}
    nodePort: {{ .Values.service.nodePort }}
  {{- end }}
    targetPort: 8080
{{- if .Values.service.loadBalancerIP }}
  loadBalancerIP: "{{ .Values.service.loadBalancerIP }}"
{{- end }}
  selector:
    app.kubernetes.io/name: {{ template "kube-state-metrics.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}

On line 8, "{{ .Chart.Name }}-{{ .Chart.Version }}" is an interpolated string.

pgavlin commented 3 years ago

The proposed approach is to define InterpolateStart and InterpolateEnd tokens. The former will be issued at the beginning of an interpolated string; the latter will be issued at the end. Between these tokens will be a sequence of one or more (String, Template) token pairs followed by at most one String token. The parser should treat this sequence of tokens the same way it treats a string scalar.