diff --git a/examples/helpers/example.txt b/examples/helpers/example.txt index 9a7773b..0224f48 100644 --- a/examples/helpers/example.txt +++ b/examples/helpers/example.txt @@ -25,7 +25,7 @@ {{abbrev 5 "hello world"}}: he... {{abbrevboth 5 10 "1234 5678 9123"}}: ...5678... {{initials "First Try"}}: FT -{{randNumeric 3}}: 146 +{{randNumeric 3}}: 528 {{- /*{{wrap 80 $someText}}*/}}: {{wrapWith 5 "\t" "Hello World"}}: Hello World {{contains "cat" "catch"}}: true @@ -45,6 +45,9 @@ {{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}: -${1}-${1}- {{regexSplit "z+" "pizza" -1}}: [pi a] +# Get one specific method on array method using index +{{ index .Service.Method 1 }}: name:"Iii" input_type:".dummy.Dummy2" output_type:".dummy.Dummy1" options:<> + # Sprig: advanced {{if contains "cat" "catch"}}yes{{else}}no{{end}}: yes {{1 | plural "one anchovy" "many anchovies"}}: one anchovy diff --git a/examples/helpers/example.txt.tmpl b/examples/helpers/example.txt.tmpl index c5f1638..21ed2ca 100644 --- a/examples/helpers/example.txt.tmpl +++ b/examples/helpers/example.txt.tmpl @@ -46,6 +46,9 @@ {{`{{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}`}}: {{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}} {{`{{regexSplit "z+" "pizza" -1}}`}}: {{regexSplit "z+" "pizza" -1}} +# Get one specific method on array method using index +{{`{{ index .Service.Method 1 }}`}}: {{ index .Service.Method 1 }} + # Sprig: advanced {{`{{if contains "cat" "catch"}}yes{{else}}no{{end}}`}}: {{if contains "cat" "catch"}}yes{{else}}no{{end}} {{`{{1 | plural "one anchovy" "many anchovies"}}`}}: {{1 | plural "one anchovy" "many anchovies"}} diff --git a/helpers/helpers.go b/helpers/helpers.go index da92080..c7ee41d 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -3,6 +3,7 @@ package pgghelpers import ( "encoding/json" "fmt" + "reflect" "regexp" "strings" "text/template" @@ -90,6 +91,16 @@ var ProtoHelpersFuncMap = template.FuncMap{ "trimstr": func(cutset, s string) string { return strings.Trim(s, cutset) }, + "index": func(array interface{}, i int) interface{} { + slice := reflect.ValueOf(array) + if slice.Kind() != reflect.Slice { + panic("Error in index(): given a non-slice type") + } + if i < 0 || i >= slice.Len() { + panic("Error in index(): index out of bounds") + } + return slice.Index(i).Interface() + }, "snakeCase": xstrings.ToSnakeCase, "getProtoFile": getProtoFile, "getMessageType": getMessageType,