Add Helper 'index': get elem of array with the index
This commit is contained in:
@@ -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,
|
||||
|
Reference in New Issue
Block a user