proposal: text/template: safe navigation with ?.
It would be beautiful if the package `text/template` (used by e.g. Helm) could support optional chaining. Similar to JavaScript in latest standard ECMAScript 2020. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining It would basically look like this: ``` spec: type: {{ .Values.service?.type | default "ClusterIP" }} ``` If service is not defined, it would short-circuit to undefined/false and thus default to "ClusterIP". This could be used in other cases, such as: ``` spec: {{- if .Values.global?.internalIPFamily }} ipFamily: {{ .Values.global.internalIPFamily }} {{- end }} ``` It solves a few things in e.g. Helm that uses `text/template` for templating: 1. `default` not working when parent values does not exist in a chain, giving errors, see https://github.com/helm/helm/issues/8026. 2. It would be able to get rid of the, in my opinion, not great best practice for nested or flat values for e.g. Helm (which I assume is applicable to other `text/template` usages as well), see https://helm.sh/docs/chart_best_practices/values/#flat-or-nested-values. Optional chaining would instead still make the code more readable (one-liner) not caring about value depth. It would also in my opinion make it easier to read YAML files with the nested structure, clearly separating levels, than to use camelCase. As I know very little about Go and the packge `text/template`, feel free to add more precise info on how this translates to what nee
Posted by thernstig on Jan 9, 2021, 10:20 AM
Tags
Related issues
- Add 65536 size support for maps
- TypeScript 7.1 Iteration Plan
- nvim-treeconfig: upstream nvim-treesitter
- Add the "action" and "method" attributes to <button>
- [FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
- > We were imposed GitHub for so many exercises!
Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments
support (Software)
Add 65536 size support for maps
TypeScript 7.1 Iteration Plan
nvim-treeconfig: upstream nvim-treesitter
Add the "action" and "method" attributes to <button>
[FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
> We were imposed GitHub for so many exercises!
Add support for Visual Studio 2026 Integration
Support for Helm v4
proposal: text/template: safe navigation with ?.
It would be beautiful if the package `text/template` (used by e.g. Helm) could support optional chaining. Similar to JavaScript in latest standard ECMAScript 2020. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining It would basically look like this: ``` spec: type: {{ .Values.service?.type | default "ClusterIP" }} ``` If service is not defined, it would short-circuit to undefined/false and thus default to "ClusterIP". This could be used in other cases, such as: ``` spec: {{- if .Values.global?.internalIPFamily }} ipFamily: {{ .Values.global.internalIPFamily }} {{- end }} ``` It solves a few things in e.g. Helm that uses `text/template` for templating: 1. `default` not working when parent values does not exist in a chain, giving errors, see https://github.com/helm/helm/issues/8026. 2. It would be able to get rid of the, in my opinion, not great best practice for nested or flat values for e.g. Helm (which I assume is applicable to other `text/template` usages as well), see https://helm.sh/docs/chart_best_practices/values/#flat-or-nested-values. Optional chaining would instead still make the code more readable (one-liner) not caring about value depth. It would also in my opinion make it easier to read YAML files with the nested structure, clearly separating levels, than to use camelCase. As I know very little about Go and the packge `text/template`, feel free to add more precise info on how this translates to what nee
Comments (0)
No comments yet. Start the discussion.