Type checking

pull/150/head
obscuren 11 years ago
parent 9bc5c4a0c5
commit 059ad35215
  1. 16
      ethutil/value.go

@ -36,7 +36,8 @@ func (val *Value) Len() int {
if data, ok := val.Val.([]interface{}); ok { if data, ok := val.Val.([]interface{}); ok {
return len(data) return len(data)
} else if data, ok := val.Val.([]byte); ok { } else if data, ok := val.Val.([]byte); ok {
// FIXME return len(data)
} else if data, ok := val.Val.(string); ok {
return len(data) return len(data)
} }
@ -139,6 +140,19 @@ func (val *Value) SliceFromTo(from, to int) *Value {
return NewValue(slice[from:to]) return NewValue(slice[from:to])
} }
// TODO More type checking methods
func (val *Value) IsSlice() bool {
return val.Type() == reflect.Slice
}
func (val *Value) IsStr() bool {
return val.Type() == reflect.String
}
func (val *Value) IsEmpty() bool {
return (val.IsSlice() || val.IsStr()) && val.Len() == 0
}
// Threat the value as a slice // Threat the value as a slice
func (val *Value) Get(idx int) *Value { func (val *Value) Get(idx int) *Value {
if d, ok := val.Val.([]interface{}); ok { if d, ok := val.Val.([]interface{}); ok {

Loading…
Cancel
Save