-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.go
123 lines (99 loc) · 2.16 KB
/
dict.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
package hio
import (
"fmt"
"reflect"
"sort"
)
type Dict interface {
Get(name string, v Value) error
Has(name string) bool
Del(name string) error
Set(name string, v Value) error
Keys() []string
//Values() []Value
}
type Value interface {
//Name() string
//Value() reflect.Value
}
type ditem struct {
k string
v Value
}
type dict struct {
slice []ditem
}
func newdict() dict {
return dict{
slice: make([]ditem, 0),
}
}
func (d dict) getidx(name string) int {
for idx, item := range d.slice {
if item.k == name {
return idx
}
}
return -1
}
func (d dict) get(name string) (Value, error) {
i := d.getidx(name)
if i < 0 {
return nil, fmt.Errorf("hio: no such key [%s]", name)
}
return d.slice[i].v, nil
}
func (d dict) Get(name string, v Value) error {
idx := d.getidx(name)
if idx < 0 {
return fmt.Errorf("hio: no such key [%s]", name)
}
vv := d.slice[idx].v
rv := reflect.ValueOf(vv)
rr := reflect.Indirect(rv)
ptr := reflect.ValueOf(v)
// fmt.Printf("v: %v (%T)\n", ptr.Elem().Interface(), ptr.Elem().Interface())
// fmt.Printf("vv: %v (%T)\n", vv, vv)
// fmt.Printf("rr: %v (%T)\n", rr.Interface(), rr.Interface())
// fmt.Printf("rv: %v (%T)\n", rv.Elem().Interface(), rv.Elem().Interface())
//rv := reflect.Indirect(ptr)
//rv.Set(reflect.ValueOf(vv))
//pval := reflect.Indirect(ptr)
pval := ptr.Elem()
pval.Set(rr)
// fmt.Printf("pv: %v (%T)\n", pval.Interface(), pval.Interface())
// fmt.Printf("v: %v (%T)\n", ptr.Elem().Interface(), ptr.Elem().Interface())
return nil
}
func (d dict) Has(name string) bool {
idx := d.getidx(name)
return idx >= 0
}
func (d *dict) Del(name string) error {
var err error
i := d.getidx(name)
if i < 0 {
return fmt.Errorf("hio: no such key [%s]", name)
}
d.slice = append(d.slice[:i], d.slice[i+1:]...)
return err
}
func (d *dict) Set(name string, v Value) error {
var err error
i := d.getidx(name)
if i >= 0 {
d.slice[i].v = v
} else {
d.slice = append(d.slice, ditem{k: name, v: v})
}
return err
}
func (d dict) Keys() []string {
keys := make([]string, 0, len(d.slice))
for _, item := range d.slice {
keys = append(keys, item.k)
}
sort.Strings(keys)
return keys
}
// EOF