-
Notifications
You must be signed in to change notification settings - Fork 5
/
stack.go
81 lines (65 loc) · 1.94 KB
/
stack.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
package xjson
import "fmt"
type Stack []*StackValue
// IsEmpty check if Stack is empty
func (s *Stack) IsEmpty() bool {
return len(*s) == 0
}
// Push a new value onto the Stack
func (s *Stack) Push(val *StackValue) {
*s = append(*s, val) // Simply append the new value to the end of the Stack
}
// Pop Remove and return top element of Stack. Return false if Stack is empty.
func (s *Stack) Pop() *StackValue {
index := len(*s) - 1 // Get the index of the top most element.
element := (*s)[index] // Index into the slice and obtain the element.
*s = (*s)[:index] // Remove it from the Stack by slicing it off.
return element
}
// Peek Peek value, not remove element.
func (s *Stack) Peek() *StackValue {
index := len(*s) - 1
element := (*s)[index] // Index into the slice and obtain the element.
return element
}
type StackType string
const (
Object StackType = "object"
ObjectKey = "objectKey"
Array = "array"
)
type StackValue struct {
stackType StackType
object interface{}
}
func NewObjectValue(object interface{}) *StackValue {
return &StackValue{stackType: Object, object: object}
}
func NewObjectKey(key string) *StackValue {
return &StackValue{stackType: ObjectKey, object: key}
}
//func NewArray(array []interface{}) *StackValue {
// return &StackValue{stackType: Array, object: array}
//}
func NewArrayPoint(array *[]interface{}) *StackValue {
return &StackValue{stackType: Array, object: array}
}
func (s *StackValue) ObjectKeyValue() string {
return fmt.Sprint(s.object)
}
func (s *StackValue) ObjectValue() map[string]interface{} {
return s.object.(map[string]interface{})
}
func (s *StackValue) Raw() interface{} {
return s.object
}
//func (s *StackValue) ArrayValue() []interface{} {
// return s.object.([]interface{})
//}
func (s *StackValue) ArrayValuePoint() *[]interface{} {
arr := s.object.(*[]interface{})
return arr
}
func (s *StackValue) StackType() StackType {
return s.stackType
}