github.com/coveo/gotemplate@v2.7.7+incompatible/collections/dictionary.go (about) 1 package collections 2 3 import ( 4 "fmt" 5 ) 6 7 // IDictionary represents objects that act as map[string]interface. 8 type IDictionary interface { 9 Add(key, value interface{}) IDictionary // Add value to key (if key exist, convert the key value into list and append the new value). 10 AsMap() map[string]interface{} // Returns the object casted as map[string]interface{}. 11 Clone(...interface{}) IDictionary // Returns a distinct copy of the object with only supplied keys. If no keys are supplied, all keys from d are copied. 12 Count() int // Simply an alias for Len. 13 Create(...int) IDictionary // Instantiates a new dictionary of the same type with optional size. 14 CreateList(...int) IGenericList // Instantiates a list of the same type as current dictionary with optional size and capacity. 15 Default(key, defVal interface{}) interface{} // Returns defVal if dictionary doesn't contain key, otherwise, simply returns entry corresponding to key. 16 Delete(interface{}, ...interface{}) (IDictionary, error) // Removes the entry value associated with key. The entry must exist. 17 Flush(...interface{}) IDictionary // Removes all specified keys from the dictionary. If no key is specified, all keys are removed. 18 Get(...interface{}) interface{} // Returns the values associated with key. 19 GetHelpers() (IDictionaryHelper, IListHelper) // Returns the helpers implementation associated with the current type. 20 GetKeys() IGenericList // Returns the keys in the dictionary in alphabetical order. 21 GetValues() IGenericList // Returns the values in the dictionary in alphabetical order of keys. 22 Has(...interface{}) bool // Returns true if the dictionary object contains all the key. 23 KeysAsString() StringArray // Returns the keys in the dictionary in alphabetical order. 24 Len() int // Returns the number of keys in the dictionary. 25 Merge(IDictionary, ...IDictionary) IDictionary // Merges the other dictionaries into the current dictionary. 26 Native() interface{} // Returns the object casted as native go type (applied recursively). 27 Omit(interface{}, ...interface{}) IDictionary // Returns a distinct copy of the object including all keys except specified ones. 28 Pop(...interface{}) interface{} // Returns and remove the objects with the specified keys. 29 PrettyPrint() string // Returns the pretty string representation of the dictionary. 30 Set(key, value interface{}) IDictionary // Sets key to value in the dictionary. 31 String() string // Returns the string representation of the dictionary. 32 Transpose() IDictionary // Transpose keys/values and return the resulting dictionary 33 TypeName() String // Returns the actual type name 34 } 35 36 // IDictionaryHelper represents objects that implement IDictionary compatible objects 37 type IDictionaryHelper interface { 38 AsDictionary(interface{}) IDictionary // Returns the object casted as IDictionary. 39 Convert(object interface{}) interface{} // Tries to convert the supplied object into IDictionary or IGenericList. 40 CreateDictionary(args ...int) IDictionary // Creates a new IDictionary with optional capacity arguments. 41 TryAsDictionary(object interface{}) (IDictionary, error) // Tries to convert any object to IDictionary objects 42 TryConvert(object interface{}) (interface{}, bool) // Tries to convert any object to IGenericList or IDictionary object. 43 } 44 45 // DictionaryHelper configures the default dictionary manager. 46 var DictionaryHelper IDictionaryHelper 47 48 func assertDictionaryHelper() { 49 if DictionaryHelper == nil { 50 panic(fmt.Errorf("DictionaryHelper not configured")) 51 } 52 } 53 54 // AsDictionary returns the object casted as IDictionary. 55 func AsDictionary(object interface{}) IDictionary { 56 return must(TryAsDictionary(object)).(IDictionary) 57 } 58 59 // CreateDictionary instantiates a new dictionary with optional size. 60 func CreateDictionary(size ...int) IDictionary { 61 assertDictionaryHelper() 62 return DictionaryHelper.CreateDictionary(size...) 63 } 64 65 // TryAsDictionary returns the object casted as IDictionary if possible. 66 func TryAsDictionary(object interface{}) (IDictionary, error) { 67 if result, ok := object.(IDictionary); ok { 68 return result, nil 69 } 70 assertDictionaryHelper() 71 return DictionaryHelper.TryAsDictionary(object) 72 }