Skip to content

Commit

Permalink
feat: Add fromJsonc template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 6, 2023
1 parent 0a6e190 commit 198795f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `fromJsonc` *jsonctext*

`fromJsonc` parses *jsonctext* as JSONC using
[`github.com/tailscale/hujson`](https://github.com/tailscale/hujson) and returns
the parsed value.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ contents of the file.
{{- .chezmoi.stdin | replaceAllRegex "old" "new" }}
```

To set individual values in JSON, TOML, and YAML files you can use the
`setValueAtPath` template function, for example:
To set individual values in JSON, JSONC, TOML, and YAML files you can use
the `setValueAtPath` template function, for example:

```
{{- /* chezmoi:modify-template */ -}}
Expand Down
1 change: 1 addition & 0 deletions assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ nav:
- encrypt: reference/templates/functions/encrypt.md
- eqFold: reference/templates/functions/eqFold.md
- fromIni: reference/templates/functions/fromIni.md
- fromJsonc: reference/templates/functions/fromJsonc.md
- fromToml: reference/templates/functions/fromToml.md
- fromYaml: reference/templates/functions/fromYaml.md
- glob: reference/templates/functions/glob.md
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func newConfig(options ...configOption) (*Config, error) {
"encrypt": c.encryptTemplateFunc,
"eqFold": c.eqFoldTemplateFunc,
"fromIni": c.fromIniTemplateFunc,
"fromJsonc": c.fromJsoncTemplateFunc,
"fromToml": c.fromTomlTemplateFunc,
"fromYaml": c.fromYamlTemplateFunc,
"gitHubKeys": c.gitHubKeysTemplateFunc,
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func (c *Config) fromIniTemplateFunc(s string) map[string]any {
return iniFileToMap(file)
}

func (c *Config) fromJsoncTemplateFunc(s string) any {
var data any
if err := chezmoi.FormatJSONC.Unmarshal([]byte(s), &data); err != nil {
panic(err)
}
return data
}

func (c *Config) fromTomlTemplateFunc(s string) any {
var data any
if err := chezmoi.FormatTOML.Unmarshal([]byte(s), &data); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/testdata/scripts/templatefuncs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ cmp stdout golden/deleteValueAtPath
exec chezmoi execute-template '{{ eqFold "foo" "Foo" "FOO" }}'
stdout '^true$'

# test fromJsonc template function
stdin golden/example.jsonc
exec chezmoi execute-template --with-stdin '{{ fromJsonc .chezmoi.stdin | toJson }}'
stdout '{"key":1}'

# test glob template function
exec chezmoi execute-template '{{ glob "*.txt" | join "\n" }}{{ "\n" }}'
cmp stdout golden/glob
Expand Down Expand Up @@ -191,6 +196,10 @@ echo '</plist>'
# line2
-- golden/deleteValueAtPath --
{"a":{"b":{"d":2}}}
-- golden/example.jsonc --
{
"key": 1, // Comment
}
-- golden/expected --
255
-- golden/glob --
Expand Down

0 comments on commit 198795f

Please sign in to comment.