github.com/coveo/gotemplate@v2.7.7+incompatible/collections/generic_list.go (about) 1 package collections 2 3 import ( 4 "fmt" 5 ) 6 7 // IGenericList represents objects that act as []interface{}. 8 type IGenericList interface { 9 Append(...interface{}) IGenericList // Add elements to the current list. If list is not large enough, it is enlarged to fit the required size. 10 AsArray() []interface{} // Returns the current list as standard array of interface{}. 11 Cap() int // Returns the capacity of the list. 12 Capacity() int // Simply an alias for Cap. 13 Clone() IGenericList // Returns a distinct copy of the object. 14 Contains(...interface{}) bool // Indicates if the list contains all specified elements 15 Count() int // Simply an alias for Len. 16 Create(...int) IGenericList // Allocates a new list of the same type implementation as this list. Optional arguments are size and capacity. 17 CreateDict(...int) IDictionary // Instantiates a new dictionary of the same type with optional size. 18 First() interface{} // Returns the first element of the list. 19 Get(...int) interface{} // Returns the element at position index in the list. If index is out of bound, nil is returned. 20 GetHelpers() (IDictionaryHelper, IListHelper) // Returns the helpers implementation associated with the current type. 21 Has(...interface{}) bool // Alias for contains 22 Intersect(...interface{}) IGenericList // Returns a list that is the result of the intersection of the list and the parameters (removing duplicates). 23 Join(sep interface{}) String // Returns the string representation of the list. 24 Last() interface{} // Returns the last element of the list. 25 Len() int // Returns the number of elements in the list. 26 New(...interface{}) IGenericList // Creates a new generic list from the supplied arguments. 27 Pop(indexes ...int) (interface{}, IGenericList) // Removes and returns the elements of the list (if nothing is specified, remove the last element). 28 Prepend(...interface{}) IGenericList // Add elements to the beginning of the current list. If list is not large enough, it is enlarged to fit the required size. 29 PrettyPrint() string // Returns the pretty string representation of the list. 30 Remove(indexes ...int) IGenericList // Returns a new list without the element specified. 31 Reverse() IGenericList // Returns a copy of the current list in reverse order. 32 Set(index int, value interface{}) (IGenericList, error) // Sets the value at position index into the list. If list is not large enough, it is enlarged to fit the index. 33 String() string // Returns the string representation of the list. 34 StringArray() StringArray // Returns the current list as StringArray. 35 Strings() []string // Returns the current list as list of strings. 36 TypeName() String // Returns the actual type name 37 Union(...interface{}) IGenericList // Returns a list that represents the union of the list and the elements (removing duplicates). 38 Unique() IGenericList // Returns a copy of the list removing all duplicate elements. 39 Without(...interface{}) IGenericList // Returns a copy of the list removing specified elements. 40 } 41 42 // IListHelper represents objects that implement IGenericList compatible objects 43 type IListHelper interface { 44 AsList(interface{}) IGenericList // Converts object to IGenericList object. It panics if conversion is impossible. 45 Convert(object interface{}) interface{} // Tries to convert the supplied object into IDictionary or IGenericList. 46 CreateList(...int) IGenericList // Creates a new IGenericList with optional size/capacity arguments. 47 NewList(...interface{}) IGenericList // Creates a new IGenericList from supplied arguments. 48 NewStringList(...string) IGenericList // Creates a new IGenericList from supplied arguments. 49 TryAsList(object interface{}) (IGenericList, error) // Tries to convert any object to IGenericList object. 50 TryConvert(object interface{}) (interface{}, bool) // Tries to convert any object to IGenericList or IDictionary object. 51 } 52 53 // ListHelper configures the default list manager. 54 var ListHelper IListHelper 55 56 func assertListHelper() { 57 if ListHelper == nil { 58 panic(fmt.Errorf("ListHelper not configured")) 59 } 60 } 61 62 // AsList returns the object casted as IGenericList. 63 func AsList(object interface{}) IGenericList { 64 result, err := TryAsList(object) 65 if err != nil { 66 return NewList(object) 67 } 68 return result 69 } 70 71 // CreateList instantiates a new generic list with optional size and capacity. 72 func CreateList(args ...int) IGenericList { 73 assertListHelper() 74 return ListHelper.CreateList(args...) 75 } 76 77 // NewList instantiates a new generic list from supplied arguments. 78 func NewList(objects ...interface{}) IGenericList { 79 assertListHelper() 80 return ListHelper.NewList(objects...) 81 } 82 83 // NewStringList creates a new IGenericList from supplied arguments. 84 func NewStringList(objects ...string) IGenericList { 85 assertListHelper() 86 return ListHelper.NewStringList(objects...) 87 } 88 89 // TryAsList returns the object casted as IGenericList if possible. 90 func TryAsList(object interface{}) (IGenericList, error) { 91 if result, ok := object.(IGenericList); ok { 92 return result, nil 93 } 94 assertListHelper() 95 return ListHelper.TryAsList(object) 96 }