github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/webutil/named-sequence.go (about) 1 package webutil 2 3 import "sort" 4 5 type NamedSequenceItem struct { 6 Sequence float64 7 Name string 8 Value interface{} 9 } 10 11 // NamedSequence represents a list of items that each have a sequence (by convention it's 0 to 100, but that's not enforced), a name and a value. 12 // All mutating operations provided here are guaranteed to copy the original without modifying it. Intended for use by global registries of 13 // things where you have a list of default things that builds up and it gets customized during application startup. 14 type NamedSequence []NamedSequenceItem 15 16 func (p NamedSequence) Len() int { return len(p) } 17 func (p NamedSequence) Less(i, j int) bool { return p[i].Sequence < p[j].Sequence } 18 func (p NamedSequence) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 19 20 // TODO: sort by Sequence, add/replace/find/remove by name, convert to []interface{} 21 22 func (pl NamedSequence) Copy() NamedSequence { 23 ret := make(NamedSequence, len(pl)) 24 copy(ret, pl) 25 return ret 26 } 27 28 // SortedCopy returns a copy of this NamedSequence sorted by Sequence (ascending). 29 func (pl NamedSequence) SortedCopy() NamedSequence { 30 ret := pl.Copy() 31 sort.Sort(ret) 32 return ret 33 } 34 35 // InterfaceSlice returns the Values as `[]interface{}` 36 func (pl NamedSequence) InterfaceSlice() []interface{} { 37 ret := make([]interface{}, 0, len(pl)) 38 for _, item := range pl { 39 ret = append(ret, item.Value) 40 } 41 return ret 42 } 43 44 // // NamedIndex returns the index in this of the item with this name. Returns -1 if not found. 45 // func (pl NamedSequence) NamedIndex(name string) int { 46 47 // } 48 49 // func (pl NamedSequence) NamedValue(name string) interface{} { 50 // } 51 52 // func (pl NamedSequence) RemoveByName(name string) (NamedSequence, bool) { 53 54 // } 55 56 // func (pl NamedSequence) ReplaceValueByName(name string, val interface{}) (NamedSequence, bool) { 57 // } 58 59 // func (pl NamedSequence) Add(seq float64, name string, val interface{}) NamedSequence { 60 // } 61 62 // func (pl NamedSequence) MustAddUnique(seq float64, name string, val interface{}) NamedSequence { 63 // }