Skip to content

Commit f62ee09

Browse files
ccoVeille42atomys
andauthored
feat: use uppercase acronyms in template functions (#98)
## Description See discussions in - #84 ## Changes The functions are now using uppercase acronym, old ones are aliased, but marked as deprecated. ## Fixes #84 ## Checklist - [X] I have read the **CONTRIBUTING.md** document. - [X] My code follows the code style of this project. - [ ] I have added tests to cover my changes. - [X] All new and existing tests passed. - [X] I have updated the documentation accordingly. - [X] This change requires a change to the documentation on the website. ## Additional Information <!-- Any additional information regarding this pull request. --> --------- Co-authored-by: Atomys <[email protected]>
1 parent 95c1667 commit f62ee09

File tree

7 files changed

+109
-79
lines changed

7 files changed

+109
-79
lines changed

benchmarks/allFunctions.sprig.tmpl

+9
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,23 @@
4444
{{ "" | b32dec }}
4545
{{ "JBSWY3DPEBLW64TMMQ======" | b32dec }}
4646
{{ "JBSWY3DPEBLW64TMMQ" | b32dec }}
47+
{{ "" | fromJSON }}
4748
{{ "" | fromJson }}
49+
{{ .json | fromJSON }}
4850
{{ .json | fromJson }}
51+
{{ (.json | fromJSON).foo }}
4952
{{ (.json | fromJson).foo }}
53+
{{ "" | toJSON }}
5054
{{ "" | toJson }}
55+
{{ .object | toJSON }}
5156
{{ .object | toJson }}
57+
{{ "" | toPrettyJSON }}
5258
{{ "" | toPrettyJson }}
59+
{{ .object | toPrettyJSON }}
5360
{{ .object | toPrettyJson }}
61+
{{ "" | toRawJSON }}
5462
{{ "" | toRawJson }}
63+
{{ .object | toRawJSON }}
5564
{{ .object | toRawJson }}
5665
{{ .json | mustFromJson }}
5766
{{ .object | mustToJson }}

benchmarks/allFunctions.sprout.tmpl

+13
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,31 @@
6262
{{ "JBSWY3DPEBLW64TMMQ======" | base32Decode }}
6363
{{ "" | safeFromJson }}
6464
{{ .json | fromJson }}
65+
{{ .json | fromJSON }}
6566
{{ (.json | fromJson).foo }}
67+
{{ (.json | fromJSON).foo }}
6668
{{ "" | toJson }}
69+
{{ "" | toJSON }}
6770
{{ .object | toJson }}
71+
{{ .object | toJSON }}
6872
{{ "" | toPrettyJson }}
73+
{{ "" | toPrettyJSON }}
6974
{{ .object | toPrettyJson }}
75+
{{ .object | toPrettyJSON }}
7076
{{ "" | toRawJson }}
77+
{{ "" | toRawJSON }}
7178
{{ .object | toRawJson }}
79+
{{ .object | toRawJSON }}
7280
{{ "" | fromYaml }}
81+
{{ "" | fromYAML }}
7382
{{ .yaml | fromYaml }}
83+
{{ .yaml | fromYAML }}
7484
{{ (.yaml | fromYaml).foo }}
85+
{{ (.yaml | fromYAML).foo }}
7586
{{ "" | toYaml }}
87+
{{ "" | toYAML }}
7688
{{ .yaml | toYaml }}
89+
{{ .yaml | toYAML }}
7790
{{ .json | mustFromJson }}
7891
{{ .object | mustToJson }}
7992
{{ .object | mustToPrettyJson }}

docs/registries/encoding.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -75,114 +75,114 @@ The function decodes a Base32 encoded string back to its original form. If the i
7575
{% endtab %}
7676
{% endtabs %}
7777

78-
### <mark style="color:purple;">fromJson</mark>
78+
### <mark style="color:purple;">fromJSON</mark>
7979

8080
The function converts a JSON string into a corresponding Go data structure, enabling easy manipulation of the JSON data in a Go environment.
8181

82-
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">FromJson(v string) (any, error)
82+
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">fromJSON(v string) (any, error)
8383
</code></pre></td></tr></tbody></table>
8484

8585
{% tabs %}
8686
{% tab title="Template Example" %}
8787
```go
88-
{{ `{"name":"John", "age":30}` | fromJson }} // Output: map[age:30 name:John]
89-
{{ "{\\invalid" | fromJson }} // Error
88+
{{ `{"name":"John", "age":30}` | fromJSON }} // Output: map[age:30 name:John]
89+
{{ "{\\invalid" | fromJSON }} // Error
9090
```
9191
{% endtab %}
9292
{% endtabs %}
9393

94-
### <mark style="color:purple;">toJson</mark>
94+
### <mark style="color:purple;">toJSON</mark>
9595

9696
The function converts a Go data structure into a JSON string, allowing the data to be easily serialized for storage, transmission, or further processing.
9797

98-
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">ToJson(v any) (string, error)
98+
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">ToJSON(v any) (string, error)
9999
</code></pre></td></tr></tbody></table>
100100

101101
{% tabs %}
102102
{% tab title="Template Example" %}
103103
```go
104104
{{- $d := dict "key1" "value1" "key2" "value2" "key3" "value3" -}}
105-
{{ toJson $d }} // Output: {\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}
105+
{{ toJSON $d }} // Output: {\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}
106106
```
107107
{% endtab %}
108108
{% endtabs %}
109109

110-
### <mark style="color:purple;">toPrettyJson</mark>
110+
### <mark style="color:purple;">toPrettyJSON</mark>
111111

112112
The function converts a Go data structure into a pretty-printed JSON string, formatting the output with indentation and line breaks for better readability.
113113

114-
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">ToPrettyJson(v any) (string, error)
114+
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">toPrettyJSON(v any) (string, error)
115115
</code></pre></td></tr></tbody></table>
116116

117117
{% tabs %}
118118
{% tab title="Template Example" %}
119119
```go
120120
{{- $d := dict "key1" "value1" "key2" "value2" "key3" "value3" -}}
121-
{{ toPrettyJson $d }} // Output: "{\n \"key1\": \"value1\",\n \"key2\": \"value2\",\n \"key3\": \"value3\"\n}"
121+
{{ toPrettyJSON $d }} // Output: "{\n \"key1\": \"value1\",\n \"key2\": \"value2\",\n \"key3\": \"value3\"\n}"
122122
```
123123
{% endtab %}
124124
{% endtabs %}
125125

126-
### <mark style="color:purple;">toRawJson</mark>
126+
### <mark style="color:purple;">toRawJSON</mark>
127127

128128
The function converts a Go data structure into a JSON string without escaping HTML characters, preserving the raw content as it is.
129129

130-
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">ToRawJson(v any) (string, error)
130+
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">toRawJSON(v any) (string, error)
131131
</code></pre></td></tr></tbody></table>
132132

133133
{% tabs %}
134134
{% tab title="Template Example" %}
135135
```go
136136
{{- $d := dict "content" "<p>Hello World</p>" -}}
137-
{{ toRawJson $d }} // Output: {\"content\":\"<p>Hello World</p>\"}
137+
{{ toRawJSON $d }} // Output: {\"content\":\"<p>Hello World</p>\"}
138138
```
139139
{% endtab %}
140140
{% endtabs %}
141141

142-
### <mark style="color:purple;">fromYaml</mark>
142+
### <mark style="color:purple;">fromYAML</mark>
143143

144144
The function deserializes a YAML string into a Go map, allowing the structured data from YAML to be used and manipulated within a Go program.
145145

146-
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">FromYAML(v string) (any, error)
146+
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">fromYAML(v string) (any, error)
147147
</code></pre></td></tr></tbody></table>
148148

149149
{% tabs %}
150150
{% tab title="Template Example" %}
151151
```go
152152

153-
{{ "name: John Doe\nage: 30" | fromYaml }} // Output: map[age:30 name:John Doe]
153+
{{ "name: John Doe\nage: 30" | fromYAML }} // Output: map[age:30 name:John Doe]
154154
```
155155
{% endtab %}
156156
{% endtabs %}
157157

158-
### <mark style="color:purple;">toYaml</mark>
158+
### <mark style="color:purple;">toYAML</mark>
159159

160160
The function serializes a Go data structure into a YAML string, converting the data into a format suitable for YAML representation.
161161

162-
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">ToYAML(v any) (string, error)
162+
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">toYAML(v any) (string, error)
163163
</code></pre></td></tr></tbody></table>
164164

165165
{% tabs %}
166166
{% tab title="Template Example" %}
167167
```go
168168
{{- $d := dict "name" "John Doe" "age" 30 -}}
169-
{{ $d | toYaml }} // Output: age: 30\nname: John Doe
169+
{{ $d | toYAML }} // Output: age: 30\nname: John Doe
170170
```
171171
{% endtab %}
172172
{% endtabs %}
173173

174-
### <mark style="color:purple;">toIndentYaml</mark>
174+
### <mark style="color:purple;">toIndentYAML</mark>
175175

176-
The function serializes a Go data structure into a YAML string, converting the data into a format suitable for YAML representation. In addition to toYaml, toIndentYaml takes a parameter to define the indentation width in spaces.
176+
The function serializes a Go data structure into a YAML string, converting the data into a format suitable for YAML representation. In addition to toYAML, toIndentYAML takes a parameter to define the indentation width in spaces.
177177

178-
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">ToIndentYAML(indent int, v any) (string, error)
178+
<table data-header-hidden><thead><tr><th width="164">Name</th><th>Value</th></tr></thead><tbody><tr><td>Signature</td><td><pre class="language-go"><code class="lang-go">toIndentYAML(indent int, v any) (string, error)
179179
</code></pre></td></tr></tbody></table>
180180

181181
{% tabs %}
182182
{% tab title="Template Example" %}
183183
```go
184184
{{ $person := dict "name" "John Doe" "age" 30 "location" (dict "country" "US" "planet" "Earth") }}
185-
{{ $person | toIndentYaml 2 }} // Output: \nage: 30\nlocation:\n country: US\n planet: Earth\nname: John Doe
185+
{{ $person | toIndentYAML 2 }} // Output: \nage: 30\nlocation:\n country: US\n planet: Earth\nname: John Doe
186186
```
187187
{% endtab %}
188188
{% endtabs %}

docs/roadmap-to-sprout-v1.0.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ This page will be updated each time function are re-implemented correctly in Spr
103103

104104
A list of functions wanted for v1 based on issues, pull requests from sprig, feedback on sprout. All functions listed here will be implemented for the v1.
105105

106-
<table><thead><tr><th width="94" data-type="checkbox">DONE</th><th>Functions</th><th>Description</th></tr></thead><tbody><tr><td>true</td><td><code>toYaml</code></td><td>Convert a struct to a YAML String</td></tr><tr><td>true</td><td><code>fromYaml</code></td><td>Convert YAML String to a struct</td></tr><tr><td>true</td><td><code>toBool</code></td><td>Convert any to a boolean</td></tr><tr><td>true</td><td><code>toDuration</code></td><td>Convert any to a <code>time.Duration</code></td></tr><tr><td>true</td><td><code>default</code>,<code>empty</code>,<code>coalesce</code></td><td>Don't trigger default go value as false</td></tr><tr><td>true</td><td><code>dig</code></td><td>Dig into a map without crashes in format <code>book.author.name</code></td></tr><tr><td>true</td><td><code>sha512sum</code></td><td>Support of SHA512</td></tr><tr><td>true</td><td><code>md5sum</code></td><td>Support of md5 hash</td></tr><tr><td>true</td><td><code>hasField</code></td><td>Detect if a field are present in an object using reflect. <a href="https://github.com/Masterminds/sprig/issues/401">Source</a></td></tr><tr><td>true</td><td><code>toDuration</code></td><td>convert a value to a <code>time.Duration</code></td></tr><tr><td>true</td><td><code>toCamelCase</code>, <code>toPascalCase</code>, <code>toKebakCase</code>, <code>toDotCase</code>, <code>topathCase</code>, <code>toConstantCase</code>,<code>toSnakeCase</code>,<code>toTitleCase</code></td><td>A batch of functions to change casing of a string to aby casing you want.</td></tr><tr><td>true</td><td><code>capitalize</code>, <code>uncapitalize</code></td><td>Capitalize / Uncapitalize a string (Upper/lower only the first character)</td></tr><tr><td>true</td><td><code>flatten</code></td><td>Flatten nested list be one level</td></tr><tr><td>true</td><td><code>regexpFindSubmatch</code>, <code>regexpAllSubmatches</code>,<code>regexpFindNamedSubmatch</code>, <code>regexpAllNamedSubmatches</code></td><td>Collection of function to found and retrieve submatches and named submatches</td></tr><tr><td>true</td><td><code>cidr</code>, <code>ip</code>, <code>mac</code></td><td>A collection of functions for network ip manipulation</td></tr><tr><td>true</td><td><code>toLocalDate</code></td><td>Convert to a <code>time.Time</code> with a timezone support</td></tr></tbody></table>
106+
<table><thead><tr><th width="94" data-type="checkbox">DONE</th><th>Functions</th><th>Description</th></tr></thead><tbody><tr><td>true</td><td><code>toYAML</code></td><td>Convert a struct to a YAML String</td></tr><tr><td>true</td><td><code>fromYAML</code></td><td>Convert YAML String to a struct</td></tr><tr><td>true</td><td><code>toBool</code></td><td>Convert any to a boolean</td></tr><tr><td>true</td><td><code>toDuration</code></td><td>Convert any to a <code>time.Duration</code></td></tr><tr><td>true</td><td><code>default</code>,<code>empty</code>,<code>coalesce</code></td><td>Don't trigger default go value as false</td></tr><tr><td>true</td><td><code>dig</code></td><td>Dig into a map without crashes in format <code>book.author.name</code></td></tr><tr><td>true</td><td><code>sha512sum</code></td><td>Support of SHA512</td></tr><tr><td>true</td><td><code>md5sum</code></td><td>Support of md5 hash</td></tr><tr><td>true</td><td><code>hasField</code></td><td>Detect if a field are present in an object using reflect. <a href="https://github.com/Masterminds/sprig/issues/401">Source</a></td></tr><tr><td>true</td><td><code>toDuration</code></td><td>convert a value to a <code>time.Duration</code></td></tr><tr><td>true</td><td><code>toCamelCase</code>, <code>toPascalCase</code>, <code>toKebakCase</code>, <code>toDotCase</code>, <code>topathCase</code>, <code>toConstantCase</code>,<code>toSnakeCase</code>,<code>toTitleCase</code></td><td>A batch of functions to change casing of a string to aby casing you want.</td></tr><tr><td>true</td><td><code>capitalize</code>, <code>uncapitalize</code></td><td>Capitalize / Uncapitalize a string (Upper/lower only the first character)</td></tr><tr><td>true</td><td><code>flatten</code></td><td>Flatten nested list be one level</td></tr><tr><td>true</td><td><code>regexpFindSubmatch</code>, <code>regexpAllSubmatches</code>,<code>regexpFindNamedSubmatch</code>, <code>regexpAllNamedSubmatches</code></td><td>Collection of function to found and retrieve submatches and named submatches</td></tr><tr><td>true</td><td><code>cidr</code>, <code>ip</code>, <code>mac</code></td><td>A collection of functions for network ip manipulation</td></tr><tr><td>true</td><td><code>toLocalDate</code></td><td>Convert to a <code>time.Time</code> with a timezone support</td></tr></tbody></table>

registry/encoding/encoding.go

+27-19
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,40 @@ func (er *EncodingRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error
2727
sprout.AddFunction(funcsMap, "base64Decode", er.Base64Decode)
2828
sprout.AddFunction(funcsMap, "base32Encode", er.Base32Encode)
2929
sprout.AddFunction(funcsMap, "base32Decode", er.Base32Decode)
30-
sprout.AddFunction(funcsMap, "fromJson", er.FromJson)
31-
sprout.AddFunction(funcsMap, "toJson", er.ToJson)
32-
sprout.AddFunction(funcsMap, "toPrettyJson", er.ToPrettyJson)
33-
sprout.AddFunction(funcsMap, "toRawJson", er.ToRawJson)
34-
sprout.AddFunction(funcsMap, "fromYaml", er.FromYAML)
35-
sprout.AddFunction(funcsMap, "toYaml", er.ToYAML)
36-
sprout.AddFunction(funcsMap, "toIndentYaml", er.ToIndentYAML)
30+
sprout.AddFunction(funcsMap, "fromJSON", er.FromJSON)
31+
sprout.AddFunction(funcsMap, "toJSON", er.ToJSON)
32+
sprout.AddFunction(funcsMap, "toPrettyJSON", er.ToPrettyJSON)
33+
sprout.AddFunction(funcsMap, "toRawJSON", er.ToRawJSON)
34+
sprout.AddFunction(funcsMap, "fromYAML", er.FromYAML)
35+
sprout.AddFunction(funcsMap, "toYAML", er.ToYAML)
36+
sprout.AddFunction(funcsMap, "toIndentYAML", er.ToIndentYAML)
3737
return nil
3838
}
3939

4040
func (er *EncodingRegistry) RegisterAliases(aliasesMap sprout.FunctionAliasMap) error {
41-
sprout.AddAlias(aliasesMap, "fromJson", "mustFromJson")
42-
sprout.AddAlias(aliasesMap, "toJson", "mustToJson")
43-
sprout.AddAlias(aliasesMap, "toPrettyJson", "mustToPrettyJson")
44-
sprout.AddAlias(aliasesMap, "toRawJson", "mustToRawJson")
45-
sprout.AddAlias(aliasesMap, "fromYaml", "mustFromYaml")
46-
sprout.AddAlias(aliasesMap, "toYaml", "mustToYaml")
41+
sprout.AddAlias(aliasesMap, "fromJSON", "fromJson", "mustFromJson")
42+
sprout.AddAlias(aliasesMap, "toJSON", "toJson", "mustToJson")
43+
sprout.AddAlias(aliasesMap, "toPrettyJSON", "toPrettyJson", "mustToPrettyJson")
44+
sprout.AddAlias(aliasesMap, "toRawJSON", "toRawJson", "mustToRawJson")
45+
sprout.AddAlias(aliasesMap, "fromYAML", "fromYaml", "mustFromYaml")
46+
sprout.AddAlias(aliasesMap, "toYAML", "toYaml", "mustToYaml")
4747
return nil
4848
}
4949

5050
func (er *EncodingRegistry) RegisterNotices(notices *[]sprout.FunctionNotice) error {
51-
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustFromJson", "please use `fromJson` instead"))
52-
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToJson", "please use `toJson` instead"))
53-
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToPrettyJson", "please use `toPrettyJson` instead"))
54-
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToRawJson", "please use `toRawJson` instead"))
55-
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustFromYaml", "please use `fromYaml` instead"))
56-
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToYaml", "please use `toYaml` instead"))
51+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustFromJson", "please use `fromJSON` instead"))
52+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToJson", "please use `toJSON` instead"))
53+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToPrettyJson", "please use `toPrettyJSON` instead"))
54+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToRawJson", "please use `toRawJSON` instead"))
55+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustFromYaml", "please use `fromYAML` instead"))
56+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("mustToYaml", "please use `toYAML` instead"))
57+
58+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("fromJson", "please use `fromJSON` instead"))
59+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("toJson", "please use `toJSON` instead"))
60+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("toPrettyJson", "please use `toPrettyJSON` instead"))
61+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("toRawJson", "please use `toRawJSON` instead"))
62+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("fromYaml", "please use `fromYAML` instead"))
63+
sprout.AddNotice(notices, sprout.NewDeprecatedNotice("toYaml", "please use `toYAML` instead"))
64+
5765
return nil
5866
}

0 commit comments

Comments
 (0)