From ff27df78fc5d638b562bae9b4515eb5f5735d45d Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 7 Sep 2014 10:18:54 +0200 Subject: Added new list type which can embed any slice type --- ethutil/list.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ethutil/list.go (limited to 'ethutil/list.go') diff --git a/ethutil/list.go b/ethutil/list.go new file mode 100644 index 000000000..18bf04792 --- /dev/null +++ b/ethutil/list.go @@ -0,0 +1,42 @@ +package ethutil + +import "reflect" + +// The list type is an anonymous slice handler which can be used +// for containing any slice type to use in an environment which +// does not support slice types (e.g., JavaScript, QML) +type List struct { + list reflect.Value + Length int +} + +// Initialise a new list. Panics if non-slice type is given. +func NewList(t interface{}) *List { + list := reflect.ValueOf(t) + if list.Kind() != reflect.Slice { + panic("list container initialized with a non-slice type") + } + + return &List{list, list.Len()} +} + +// Get N element from the embedded slice. Returns nil if OOB. +func (self *List) Get(i int) interface{} { + if self.list.Len() > i { + return self.list.Index(i).Interface() + } + + return nil +} + +// Appends value at the end of the slice. Panics when incompatible value +// is given. +func (self *List) Append(v interface{}) { + self.list = reflect.Append(self.list, reflect.ValueOf(v)) + self.Length = self.list.Len() +} + +// Returns the underlying slice as interface. +func (self *List) Interface() interface{} { + return self.list.Interface() +} -- cgit v1.2.3 From 2fb57b2ea7b7f697ddc4811c471d87116eae07cc Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 14 Sep 2014 00:13:23 +0200 Subject: Reworked filters --- ethutil/list.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ethutil/list.go') diff --git a/ethutil/list.go b/ethutil/list.go index 18bf04792..0aa657a14 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -20,6 +20,10 @@ func NewList(t interface{}) *List { return &List{list, list.Len()} } +func EmptyList() *List { + return NewList([]interface{}{}) +} + // Get N element from the embedded slice. Returns nil if OOB. func (self *List) Get(i int) interface{} { if self.list.Len() > i { -- cgit v1.2.3 From b65f29f8faa20a93bd83c18232326c935cb16981 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 22 Sep 2014 14:51:41 +0200 Subject: Added JavaScript JSON helper --- ethutil/list.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'ethutil/list.go') diff --git a/ethutil/list.go b/ethutil/list.go index 0aa657a14..9db96cf18 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -1,6 +1,9 @@ package ethutil -import "reflect" +import ( + "encoding/json" + "reflect" +) // The list type is an anonymous slice handler which can be used // for containing any slice type to use in an environment which @@ -44,3 +47,15 @@ func (self *List) Append(v interface{}) { func (self *List) Interface() interface{} { return self.list.Interface() } + +// For JavaScript <3 +func (self *List) ToJSON() string { + var list []interface{} + for i := 0; i < self.Length; i++ { + list = append(list, self.Get(i)) + } + + data, _ := json.Marshal(list) + + return string(data) +} -- cgit v1.2.3 From 57dc435f9b928f5de2a49736a2c71a7bf611289a Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 24 Sep 2014 11:39:17 +0200 Subject: Added TD for each block --- ethutil/list.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'ethutil/list.go') diff --git a/ethutil/list.go b/ethutil/list.go index 9db96cf18..a5147573a 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -2,6 +2,7 @@ package ethutil import ( "encoding/json" + "fmt" "reflect" ) @@ -29,6 +30,10 @@ func EmptyList() *List { // Get N element from the embedded slice. Returns nil if OOB. func (self *List) Get(i int) interface{} { + if self.list.Len() == 3 { + fmt.Println("get", i, self.list.Index(i).Interface()) + } + if self.list.Len() > i { return self.list.Index(i).Interface() } -- cgit v1.2.3 From 1118aaf840a6f6b4dd6b137f39ab895a0cbd5a56 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 24 Sep 2014 20:40:40 +0200 Subject: Temp work around --- ethutil/list.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'ethutil/list.go') diff --git a/ethutil/list.go b/ethutil/list.go index a5147573a..4fb36224f 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -2,7 +2,6 @@ package ethutil import ( "encoding/json" - "fmt" "reflect" ) @@ -10,6 +9,7 @@ import ( // for containing any slice type to use in an environment which // does not support slice types (e.g., JavaScript, QML) type List struct { + val interface{} list reflect.Value Length int } @@ -21,7 +21,7 @@ func NewList(t interface{}) *List { panic("list container initialized with a non-slice type") } - return &List{list, list.Len()} + return &List{t, list, list.Len()} } func EmptyList() *List { @@ -30,17 +30,24 @@ func EmptyList() *List { // Get N element from the embedded slice. Returns nil if OOB. func (self *List) Get(i int) interface{} { - if self.list.Len() == 3 { - fmt.Println("get", i, self.list.Index(i).Interface()) - } if self.list.Len() > i { - return self.list.Index(i).Interface() + i := self.list.Index(i).Interface() + + return i } return nil } +func (self *List) GetAsJson(i int) interface{} { + e := self.Get(i) + + r, _ := json.Marshal(e) + + return string(r) +} + // Appends value at the end of the slice. Panics when incompatible value // is given. func (self *List) Append(v interface{}) { -- cgit v1.2.3 From 0015ce1e353f52cca818d11f566b9a656fb85f24 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 7 Oct 2014 11:18:46 +0200 Subject: kick of bad peers --- ethutil/list.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'ethutil/list.go') diff --git a/ethutil/list.go b/ethutil/list.go index 4fb36224f..6919a02f5 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -3,12 +3,14 @@ package ethutil import ( "encoding/json" "reflect" + "sync" ) // The list type is an anonymous slice handler which can be used // for containing any slice type to use in an environment which // does not support slice types (e.g., JavaScript, QML) type List struct { + mut sync.Mutex val interface{} list reflect.Value Length int @@ -21,7 +23,7 @@ func NewList(t interface{}) *List { panic("list container initialized with a non-slice type") } - return &List{t, list, list.Len()} + return &List{sync.Mutex{}, t, list, list.Len()} } func EmptyList() *List { @@ -30,8 +32,10 @@ func EmptyList() *List { // Get N element from the embedded slice. Returns nil if OOB. func (self *List) Get(i int) interface{} { - if self.list.Len() > i { + self.mut.Lock() + defer self.mut.Unlock() + i := self.list.Index(i).Interface() return i @@ -51,6 +55,9 @@ func (self *List) GetAsJson(i int) interface{} { // Appends value at the end of the slice. Panics when incompatible value // is given. func (self *List) Append(v interface{}) { + self.mut.Lock() + defer self.mut.Unlock() + self.list = reflect.Append(self.list, reflect.ValueOf(v)) self.Length = self.list.Len() } -- cgit v1.2.3