Add Helper 'index': get elem of array with the index
This commit is contained in:
		| @@ -25,7 +25,7 @@ | |||||||
| {{abbrev 5 "hello world"}}:                                                         he... | {{abbrev 5 "hello world"}}:                                                         he... | ||||||
| {{abbrevboth 5 10 "1234 5678 9123"}}:                                               ...5678... | {{abbrevboth 5 10 "1234 5678 9123"}}:                                               ...5678... | ||||||
| {{initials "First Try"}}:                                                           FT | {{initials "First Try"}}:                                                           FT | ||||||
| {{randNumeric 3}}:                                                                  146 | {{randNumeric 3}}:                                                                  528 | ||||||
| {{- /*{{wrap 80 $someText}}*/}}: | {{- /*{{wrap 80 $someText}}*/}}: | ||||||
| {{wrapWith 5 "\t" "Hello World"}}:                                                  Hello	World | {{wrapWith 5 "\t" "Hello World"}}:                                                  Hello	World | ||||||
| {{contains "cat" "catch"}}:                                                         true | {{contains "cat" "catch"}}:                                                         true | ||||||
| @@ -45,6 +45,9 @@ | |||||||
| {{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}:                             -${1}-${1}- | {{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}:                             -${1}-${1}- | ||||||
| {{regexSplit "z+" "pizza" -1}}:                                                     [pi a] | {{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 | # Sprig: advanced | ||||||
| {{if contains "cat" "catch"}}yes{{else}}no{{end}}:   yes | {{if contains "cat" "catch"}}yes{{else}}no{{end}}:   yes | ||||||
| {{1 | plural "one anchovy" "many anchovies"}}:       one anchovy | {{1 | plural "one anchovy" "many anchovies"}}:       one anchovy | ||||||
|   | |||||||
| @@ -46,6 +46,9 @@ | |||||||
| {{`{{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}`}}:                             {{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}} | {{`{{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}}`}}:                             {{regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}"}} | ||||||
| {{`{{regexSplit "z+" "pizza" -1}}`}}:                                                     {{regexSplit "z+" "pizza" -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 | # Sprig: advanced | ||||||
| {{`{{if contains "cat" "catch"}}yes{{else}}no{{end}}`}}:   {{if contains "cat" "catch"}}yes{{else}}no{{end}} | {{`{{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"}} | {{`{{1 | plural "one anchovy" "many anchovies"}}`}}:       {{1 | plural "one anchovy" "many anchovies"}} | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ package pgghelpers | |||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"reflect" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"text/template" | 	"text/template" | ||||||
| @@ -90,6 +91,16 @@ var ProtoHelpersFuncMap = template.FuncMap{ | |||||||
| 	"trimstr": func(cutset, s string) string { | 	"trimstr": func(cutset, s string) string { | ||||||
| 		return strings.Trim(s, cutset) | 		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, | 	"snakeCase":               xstrings.ToSnakeCase, | ||||||
| 	"getProtoFile":            getProtoFile, | 	"getProtoFile":            getProtoFile, | ||||||
| 	"getMessageType":          getMessageType, | 	"getMessageType":          getMessageType, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user