-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.go
153 lines (127 loc) · 3.8 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package forge
import (
"errors"
"fmt"
)
// List struct used for holding data neede for Reference data type
type List struct {
values []Value
}
// NewList will create and initialize a new List value
func NewList() *List {
return &List{
values: make([]Value, 0),
}
}
// GetType will simply return back LIST
func (list *List) GetType() ValueType {
return LIST
}
// GetValue will resolve and return the value from the underlying list
// this is necessary to inherit from Value
func (list *List) GetValue() interface{} {
var values []interface{}
for _, val := range list.values {
values = append(values, val.GetValue())
}
return values
}
// GetValues will return back the list of underlygin values
func (list *List) GetValues() []Value {
return list.values
}
// UpdateValue will set the underlying list value
func (list *List) UpdateValue(value interface{}) error {
// Valid types
switch value.(type) {
case []Value:
list.values = value.([]Value)
default:
msg := fmt.Sprintf("Unsupported type, %s must be of type []Value", value)
return errors.New(msg)
}
return nil
}
// Get will return the Value at the index
func (list *List) Get(idx int) (Value, error) {
if idx > list.Length() {
return nil, errors.New("index out of range")
}
return list.values[idx], nil
}
// GetBoolean will try to get the value stored at the index as a bool
// will respond with an error if the value does not exist or cannot be converted to a bool
func (list *List) GetBoolean(idx int) (bool, error) {
value, err := list.Get(idx)
if err != nil {
return false, err
}
switch value.(type) {
case *Primative:
return value.(*Primative).AsBoolean()
}
return false, errors.New("could not convert unknown value to boolean")
}
// GetFloat will try to get the value stored at the index as a float64
// will respond with an error if the value does not exist or cannot be converted to a float64
func (list *List) GetFloat(idx int) (float64, error) {
value, err := list.Get(idx)
if err != nil {
return float64(0), err
}
switch value.(type) {
case *Primative:
return value.(*Primative).AsFloat()
}
return float64(0), errors.New("could not convert non-primative value to float")
}
// GetInteger will try to get the value stored at the index as a int64
// will respond with an error if the value does not exist or cannot be converted to a int64
func (list *List) GetInteger(idx int) (int64, error) {
value, err := list.Get(idx)
if err != nil {
return int64(0), err
}
switch value.(type) {
case *Primative:
return value.(*Primative).AsInteger()
}
return int64(0), errors.New("could not convert non-primative value to integer")
}
// GetList will try to get the value stored at the index as a List
// will respond with an error if the value does not exist or is not a List
func (list *List) GetList(idx int) (*List, error) {
value, err := list.Get(idx)
if err != nil {
return nil, err
}
if value.GetType() == LIST {
return value.(*List), nil
}
return nil, errors.New("could not fetch value as list")
}
// GetString will try to get the value stored at the index as a string
// will respond with an error if the value does not exist or cannot be converted to a string
func (list *List) GetString(idx int) (string, error) {
value, err := list.Get(idx)
if err != nil {
return "", err
}
switch value.(type) {
case *Primative:
return value.(*Primative).AsString()
}
return "", errors.New("could not convert non-primative value to string")
}
// Set will set the new Value at the index
func (list *List) Set(idx int, value Value) {
list.values[idx] = value
}
// Append will append a new Value on the end of the internal list
func (list *List) Append(value Value) {
list.values = append(list.values, value)
}
// Length will return back the total number of items in the list
func (list *List) Length() int {
return len(list.values)
}