9 // The list type is an anonymous slice handler which can be used
10 // for containing any slice type to use in an environment which
11 // does not support slice types (e.g., JavaScript, QML)
19 // Initialise a new list. Panics if non-slice type is given.
20 func NewList(t interface{}) *List {
21 list := reflect.ValueOf(t)
22 if list.Kind() != reflect.Slice {
23 panic("list container initialized with a non-slice type")
26 return &List{sync.Mutex{}, t, list, list.Len()}
29 func EmptyList() *List {
30 return NewList([]interface{}{})
33 // Get N element from the embedded slice. Returns nil if OOB.
34 func (self *List) Get(i int) interface{} {
35 if self.list.Len() > i {
37 defer self.mut.Unlock()
39 i := self.list.Index(i).Interface()
47 func (self *List) GetAsJson(i int) interface{} {
49 r, _ := json.Marshal(e)
53 // Appends value at the end of the slice. Panics when incompatible value
55 func (self *List) Append(v interface{}) {
57 defer self.mut.Unlock()
59 self.list = reflect.Append(self.list, reflect.ValueOf(v))
60 self.Length = self.list.Len()
63 // Returns the underlying slice as interface.
64 func (self *List) Interface() interface{} {
65 return self.list.Interface()
69 func (self *List) ToJSON() string {
71 list := make([]interface{}, 0)
72 for i := 0; i < self.Length; i++ {
73 list = append(list, self.Get(i))
75 data, _ := json.Marshal(list)