forked from h2oai/wave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
172 lines (160 loc) · 3.58 KB
/
card.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2020 H2O.ai, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wave
import (
"strconv"
"strings"
)
// Buf represents a generic dictionary-like buffer.
type Buf interface {
// get cursor at key k
get(k string) (Cur, bool)
// overwrite all records
put(v interface{})
// set record at key k
set(k string, v interface{})
// dump contents
dump() BufD
}
func loadBuf(ns *Namespace, b BufD) Buf {
if b.C != nil {
return loadCycBuf(ns, b.C)
}
if b.F != nil {
return loadFixBuf(ns, b.F)
}
if b.M != nil {
return loadMapBuf(ns, b.M)
}
return nil
}
// Card represents an item on a Page, and holds attributes and data for rendering views.
type Card struct {
data map[string]interface{}
}
const dataPrefix = "~"
func loadCard(ns *Namespace, c CardD) *Card {
card := &Card{make(map[string]interface{})}
ks := make([]string, 1) // to avoid allocation during card.set() below
for k, v := range c.D {
if len(k) > 0 && strings.HasPrefix(k, dataPrefix) {
if f, ok := v.(float64); ok {
i := int(f)
if i >= 0 && i < len(c.B) {
k, v = strings.TrimPrefix(k, dataPrefix), loadBuf(ns, c.B[i])
}
}
}
ks[0] = k
card.set(ks, v)
}
return card
}
func (c *Card) set(ks []string, v interface{}) {
switch len(ks) {
case 0: // should not get here; outer interpreter loop will clobber this card.
return
case 1: // .foo = bar
p := ks[0]
if ib, ok := c.data[p]; ok { // TODO can optimize by duplicating all bufs in a card.bufs map
if b, ok := ib.(Buf); ok { // avoid clobbering buffers; overwrite instead.
b.put(v)
return
}
}
if v == nil {
delete(c.data, p)
} else {
c.data[p] = v
}
default: // .foo.bar.baz = qux
var x interface{} = c.data
p := ks[len(ks)-1]
for _, k := range ks[:len(ks)-1] {
x = get(x, k)
}
set(x, p, v)
}
}
func set(ix interface{}, k string, v interface{}) {
switch x := ix.(type) {
case Buf:
x.set(k, v)
case Cur:
x.set(k, v)
case map[string]interface{}:
if v == nil {
delete(x, k)
} else {
x[k] = v
}
case []interface{}:
if i, err := strconv.Atoi(k); err == nil {
if i >= 0 && i < len(x) {
x[i] = v
}
}
}
}
func get(ix interface{}, k string) interface{} {
switch x := ix.(type) {
case Buf:
if r, ok := x.get(k); ok {
return r
}
case Cur:
return x.get(k)
case map[string]interface{}:
if v, ok := x[k]; ok {
return v
}
case []interface{}:
if i, err := strconv.Atoi(k); err == nil {
if i >= 0 && i < len(x) {
return x[i]
}
}
}
return nil
}
func (c *Card) dump() CardD {
data := make(map[string]interface{})
var bufs []BufD
for k, iv := range c.data {
if v, ok := iv.(Buf); ok {
data[dataPrefix+k] = len(bufs)
bufs = append(bufs, v.dump())
} else {
data[k] = deepClone(iv)
}
}
return CardD{data, bufs}
}
func deepClone(ix interface{}) interface{} {
switch x := ix.(type) {
case map[string]interface{}:
m := make(map[string]interface{})
for k, v := range x {
m[k] = deepClone(v)
}
return m
case []interface{}:
s := make([]interface{}, len(x))
for i, v := range x {
s[i] = deepClone(v)
}
return s
}
return ix
}