From 2a101eda4d8601be2b0e986bd23cb0d1a65be51d Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Thu, 4 Aug 2022 09:03:35 -0400 Subject: [PATCH] update docs Signed-off-by: Dave Henderson --- docs-src/content/functions/coll.yml | 58 ++++++++++++++++++--- docs-src/content/functions/conv.yml | 8 +-- docs-src/content/functions/func_doc.md.tmpl | 2 +- docs-src/content/functions/strings.yml | 4 +- docs/content/functions/coll.md | 43 ++++++++++++++- 5 files changed, 99 insertions(+), 16 deletions(-) diff --git a/docs-src/content/functions/coll.yml b/docs-src/content/functions/coll.yml index cf267fe11..b0532709c 100644 --- a/docs-src/content/functions/coll.yml +++ b/docs-src/content/functions/coll.yml @@ -43,6 +43,7 @@ funcs: Hello world! Hello everybody! - name: coll.Slice + deprecated: The `slice` alias is deprecated, and `coll.Slice` is renamed to [`coll.MakeSlice`](#coll-makeslice). alias: slice description: | Creates a slice (like an array or list). Useful when needing to `range` over a bunch of variables. @@ -53,10 +54,53 @@ funcs: description: the elements of the slice examples: - | - $ gomplate -i '{{ range slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' + $ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' Hello, Bart Hello, Lisa Hello, Maggie + - name: coll.MakeSlice + description: | + Creates a slice (like an array or list). Useful when needing to `range` over a bunch of variables. + pipeline: false + arguments: + - name: in... + required: true + description: the elements of the slice + examples: + - | + $ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' + Hello, Bart + Hello, Lisa + Hello, Maggie + - name: coll.GoSlice + description: | + This exposes the `slice` function from Go's [`text/template`](https://golang.org/pkg/text/template/#hdr-Functions) + package. Note that using `slice` will use the `coll.Slice` function instead, + which may not be desired. + For some background on this, see [this issue](https://github.com/hairyhenderson/gomplate/issues/1461). + + Here is the upstream documentation: + + ``` + slice returns the result of slicing its first argument by the + remaining arguments. Thus "slice x 1 2" is, in Go syntax, x[1:2], + while "slice x" is x[:], "slice x 1" is x[1:], and "slice x 1 2 3" + is x[1:2:3]. The first argument must be a string, slice, or array. + ``` + + See the [Go language spec](https://go.dev/ref/spec#Slice_expressions) for + more details. + pipeline: false + arguments: + - name: item + required: true + description: the string, slice, or array to slice + - name: indexes... + required: false + description: the indexes to slice the item by (0 to 3 arguments) + examples: + - | + $ gomplate -i '{{ $l := coll.Slice "foo" "bar" "baz" }}{{ if has $l "bar" }}a{{else}}no{{end}} bar' - name: coll.Has alias: has description: | @@ -71,7 +115,7 @@ funcs: description: The item to search for examples: - | - $ gomplate -i '{{ $l := slice "foo" "bar" "baz" }}there is {{ if has $l "bar" }}a{{else}}no{{end}} bar' + $ gomplate -i '{{ $l := coll.Slice "foo" "bar" "baz" }}there is {{ if has $l "bar" }}a{{else}}no{{end}} bar' there is a bar - | $ export DATA='{"foo": "bar"}' @@ -163,7 +207,7 @@ funcs: description: the slice or array to append to examples: - | - $ gomplate -i '{{ slice 1 1 2 3 | append 5 }}' + $ gomplate -i '{{ coll.Slice 1 1 2 3 | append 5 }}' [1 1 2 3 5] - name: coll.Prepend alias: prepend @@ -183,7 +227,7 @@ funcs: description: the slice or array to prepend to examples: - | - $ gomplate -i '{{ slice 4 3 2 1 | prepend 5 }}' + $ gomplate -i '{{ coll.Slice 4 3 2 1 | prepend 5 }}' [5 4 3 2 1] - name: coll.Uniq alias: uniq @@ -198,7 +242,7 @@ funcs: description: the input list examples: - | - $ gomplate -i '{{ slice 1 2 3 2 3 4 1 5 | uniq }}' + $ gomplate -i '{{ coll.Slice 1 2 3 2 3 4 1 5 | uniq }}' [1 2 3 4 5] - name: coll.Flatten alias: flatten @@ -235,7 +279,7 @@ funcs: description: the list to reverse examples: - | - $ gomplate -i '{{ slice 4 3 2 1 | reverse }}' + $ gomplate -i '{{ coll.Slice 4 3 2 1 | reverse }}' [1 2 3 4] - name: coll.Sort alias: sort @@ -257,7 +301,7 @@ funcs: description: the slice or array to sort examples: - | - $ gomplate -i '{{ slice "foo" "bar" "baz" | coll.Sort }}' + $ gomplate -i '{{ coll.Slice "foo" "bar" "baz" | coll.Sort }}' [bar baz foo] - | $ gomplate -i '{{ sort (slice 3 4 1 2 5) }}' diff --git a/docs-src/content/functions/conv.yml b/docs-src/content/functions/conv.yml index 10aa2fc3e..c2a1390df 100644 --- a/docs-src/content/functions/conv.yml +++ b/docs-src/content/functions/conv.yml @@ -65,7 +65,7 @@ funcs: For creating more complex maps, see [`data.JSON`](../data/#data-json) or [`data.YAML`](../data/#data-yaml). - For creating arrays, see [`conv.Slice`](#conv-slice). + For creating arrays, see [`coll.MakeSlice`](#coll-makeslice). arguments: - name: in... required: true @@ -97,7 +97,7 @@ funcs: description: the elements of the slice examples: - | - $ gomplate -i '{{ range slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' + $ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' Hello, Bart Hello, Lisa Hello, Maggie @@ -116,7 +116,7 @@ funcs: description: The item to search for examples: - | - $ gomplate -i '{{ $l := slice "foo" "bar" "baz" }}there is {{ if has $l "bar" }}a{{else}}no{{end}} bar' + $ gomplate -i '{{ $l := coll.Slice "foo" "bar" "baz" }}there is {{ if has $l "bar" }}a{{else}}no{{end}} bar' there is a bar - | $ export DATA='{"foo": "bar"}' @@ -141,7 +141,7 @@ funcs: description: the separator examples: - | - $ gomplate -i '{{ $a := slice 1 2 3 }}{{ join $a "-" }}' + $ gomplate -i '{{ $a := coll.Slice 1 2 3 }}{{ join $a "-" }}' 1-2-3 - name: conv.URL alias: urlParse diff --git a/docs-src/content/functions/func_doc.md.tmpl b/docs-src/content/functions/func_doc.md.tmpl index 0e8668de6..36adc7128 100644 --- a/docs-src/content/functions/func_doc.md.tmpl +++ b/docs-src/content/functions/func_doc.md.tmpl @@ -1,7 +1,7 @@ {{ define "argName" }}{{ if not .required }}[{{ .name }}]{{else}}{{ .name }}{{end}}{{ end }} {{- define "usage" }}### Usage -{{- $arguments := index . "arguments" | default slice }} +{{- $arguments := index . "arguments" | default coll.Slice }} {{ if has . "rawUsage" }}{{ .rawUsage | strings.TrimSpace }}{{ else }} ```go {{ .name }}{{ range $a := $arguments }} {{template "argName" $a }}{{end}} diff --git a/docs-src/content/functions/strings.yml b/docs-src/content/functions/strings.yml index bdc127aa8..4631f5f93 100644 --- a/docs-src/content/functions/strings.yml +++ b/docs-src/content/functions/strings.yml @@ -135,7 +135,7 @@ funcs: description: The list to sort examples: - | - $ gomplate -i '{{ (slice "foo" "bar" "baz") | strings.Sort }}' + $ gomplate -i '{{ (coll.Slice "foo" "bar" "baz") | strings.Sort }}' [bar baz foo] - name: strings.Split description: | @@ -267,7 +267,7 @@ funcs: description: The input to quote examples: - | - $ gomplate -i "{{ slice \"one word\" \"foo='bar baz'\" | shellQuote }}" + $ gomplate -i "{{ coll.Slice \"one word\" \"foo='bar baz'\" | shellQuote }}" 'one word' 'foo='"'"'bar baz'"'"'' - | $ gomplate -i "{{ strings.ShellQuote \"it's a banana\" }}" diff --git a/docs/content/functions/coll.md b/docs/content/functions/coll.md index 318783332..c8b9547ba 100644 --- a/docs/content/functions/coll.md +++ b/docs/content/functions/coll.md @@ -60,7 +60,8 @@ Hello world! Hello everybody! ``` -## `coll.Slice` +## `coll.Slice` _(deprecated)_ +**Deprecation Notice:** `slice` alias is deprecated, use `coll.Slice` instead **Alias:** `slice` @@ -81,7 +82,45 @@ coll.Slice in... ### Examples ```console -$ gomplate -i '{{ range slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' +$ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' +Hello, Bart +Hello, Lisa +Hello, Maggie +``` + +## `coll.GoSlice` + +This exposes the `slice` function from Go's [`text/template`](https://golang.org/pkg/text/template/#hdr-Functions) +package. Note that using `slice` will use the `coll.Slice` function instead, +which may not be desired. +For some background on this, see [this issue](https://github.com/hairyhenderson/gomplate/issues/1461). + +Here is the upstream documentation: + +``` +slice returns the result of slicing its first argument by the +remaining arguments. Thus "slice x 1 2" is, in Go syntax, x[1:2], +while "slice x" is x[:], "slice x 1" is x[1:], and "slice x 1 2 3" +is x[1:2:3]. The first argument must be a string, slice, or array. +``` + +### Usage + +```go +coll.GoSlice item [indexes...] +``` + +### Arguments + +| name | description | +|------|-------------| +| `item` | _(required)_ the string, slice, or array to slice | +| `indexes...` | _(optional)_ the indexes to slice the item by | + +### Examples + +```console +$ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' Hello, Bart Hello, Lisa Hello, Maggie