forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noms_splore.go
341 lines (308 loc) · 8.76 KB
/
noms_splore.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2017 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package splore
import (
"encoding/json"
"fmt"
"net"
"net/http"
"reflect"
"github.com/attic-labs/kingpin"
"github.com/ndau/noms/cmd/util"
"github.com/ndau/noms/go/config"
"github.com/ndau/noms/go/d"
"github.com/ndau/noms/go/spec"
"github.com/ndau/noms/go/types"
"github.com/ndau/noms/go/util/verbose"
humanize "github.com/dustin/go-humanize"
"github.com/skratchdot/open-golang/open"
)
var httpServe = http.Serve // override for tests
const indexHtml = `<!DOCTYPE html>
<html>
<head>
<title>noms splore %s</title>
<style>
body { margin: 0; user-select: none; }
</style>
</head>
<body>
<div id="splore"></div>
<script src="/out.js"></script>
</body>
</html>`
type node struct {
nodeInfo
Children []nodeChild `json:"children"`
}
type nodeInfo struct {
HasChildren bool `json:"hasChildren"`
ID string `json:"id"`
Name string `json:"name"`
}
type nodeChild struct {
Key nodeInfo `json:"key"`
Label string `json:"label"`
Value nodeInfo `json:"value"`
}
func Cmd(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
splore := noms.Command("splore", `Interactively explore a Noms database using a web browser.
See Spelling Objects at https://github.com/ndau/noms/blob/master/doc/spelling.md for details on the path argument.
`)
port := splore.Flag("port", "Server port. Defaults to a random port.").Short('p').Int()
browser := splore.Flag("browser", "Immediately open a web browser.").Short('b').Bool()
spec := splore.Arg("database or path", "The noms database or path to splore.").Required().String()
return splore, func(input string) (exitCode int) {
return run(&http.ServeMux{}, *port, *browser, *spec)
}
}
func run(mux *http.ServeMux, port int, browser bool, spStr string) int {
var sp spec.Spec
var getValue func() types.Value
cfg := config.NewResolver()
if pathSp, err := spec.ForPath(cfg.ResolvePathSpec(spStr)); err == nil {
sp = pathSp
getValue = func() types.Value { return sp.GetValue() }
} else if dbSp, err := spec.ForDatabase(cfg.ResolveDbSpec(spStr)); err == nil {
sp = dbSp
getValue = func() types.Value { return sp.GetDatabase().Datasets() }
} else {
d.CheckError(fmt.Errorf("Not a path or database: %s", spStr))
}
defer sp.Close()
req := func(w http.ResponseWriter, contentType string) {
sp.GetDatabase().Rebase()
w.Header().Add("Content-Type", contentType)
w.Header().Add("Cache-Control", "max-age=0,no-cache")
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
req(w, "text/html")
fmt.Fprintf(w, indexHtml, sp.String())
})
mux.HandleFunc("/out.js", func(w http.ResponseWriter, r *http.Request) {
req(w, "application/javascript")
// To develop JS, uncomment this line and run `yarn start`:
//http.ServeFile(w, r, "splore/out.js")
// To build noms-splore. uncomment this line and run `yarn buildgo`:
fmt.Fprint(w, outJs)
})
mux.HandleFunc("/getNode", func(w http.ResponseWriter, r *http.Request) {
req(w, "application/json")
r.ParseForm()
id := r.Form.Get("id")
var v types.Value
switch {
case id == "":
v = getValue()
case id[0] == '#':
abspath, err := spec.NewAbsolutePath(id)
d.PanicIfError(err)
v = abspath.Resolve(sp.GetDatabase())
default:
path := types.MustParsePath(id)
v = path.Resolve(getValue(), sp.GetDatabase())
}
if v == nil {
http.Error(w, `{"error": "not found"}`, http.StatusNotFound)
return
}
err := json.NewEncoder(w).Encode(node{
nodeInfo: info(v, id),
Children: getNodeChildren(v, id),
})
d.PanicIfError(err)
})
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
d.PanicIfError(err)
if browser {
open.Run("http://" + l.Addr().String())
}
if !verbose.Quiet() {
fmt.Println("Listening on", l.Addr().String())
}
d.PanicIfError(httpServe(l, mux))
return 0
}
func getNodeChildren(v types.Value, parentPath string) (children []nodeChild) {
childPath := func(f string, args ...interface{}) string {
return parentPath + fmt.Sprintf(f, args...)
}
switch v := v.(type) {
case types.Bool, types.Number, types.String:
children = []nodeChild{}
case types.Blob:
children = getMetaChildren(v)
if children == nil {
children = []nodeChild{}
}
case types.List:
children = getMetaChildren(v)
if children == nil {
children = make([]nodeChild, v.Len())
v.IterAll(func(vi types.Value, i uint64) {
children[i] = nodeChild{
Value: info(vi, childPath("[%d]", i)),
}
})
}
case types.Map:
children = getMetaChildren(v)
if children == nil {
children = make([]nodeChild, v.Len())
i := 0
v.IterAll(func(k, v types.Value) {
children[i] = nodeChild{
Key: info(k, childPath("@at(%d)@key", i)),
Value: info(v, childPath("@at(%d)", i)),
}
i++
})
}
case types.Set:
children = getMetaChildren(v)
if children == nil {
children = make([]nodeChild, v.Len())
i := 0
v.IterAll(func(v types.Value) {
children[i] = nodeChild{
Value: info(v, childPath("@at(%d)", i)),
}
i++
})
}
case types.Ref:
children = []nodeChild{{
Value: info(v, childPath("@target")),
}}
case types.Struct:
children = make([]nodeChild, v.Len())
i := 0
v.IterFields(func(name string, v types.Value) bool {
children[i] = nodeChild{
Label: name,
Value: info(v, childPath(".%s", name)),
}
i++
return false
})
case *types.Type:
switch d := v.Desc.(type) {
case types.CompoundDesc:
children = make([]nodeChild, len(d.ElemTypes))
for i, t := range d.ElemTypes {
children[i] = nodeChild{
Value: info(t, childPath("[%d]", i)),
}
}
case types.StructDesc:
children = make([]nodeChild, d.Len())
i := 0
d.IterFields(func(name string, t *types.Type, optional bool) {
children[i] = nodeChild{
Label: name,
Value: info(t, childPath(".%s", name)),
}
i++
})
default:
children = []nodeChild{} // TODO: cycles?
}
default:
panic(fmt.Errorf("unsupported value type %T", v))
}
return
}
func nodeName(v types.Value) string {
typeName := func(iface interface{}) string {
return reflect.TypeOf(iface).Name()
}
switch v := v.(type) {
case types.Bool, types.Number, types.String:
return fmt.Sprintf("%#v", v)
case types.Blob:
return fmt.Sprintf("%s(%s)", typeName(v), humanize.Bytes(v.Len()))
case types.List, types.Map, types.Set:
return fmt.Sprintf("%s(%d)", typeName(v), v.(types.Collection).Len())
case types.Ref:
kind := v.TargetType().Desc.Kind()
return fmt.Sprintf("%s#%s", kind.String(), v.TargetHash().String())
case types.Struct:
if v.Name() == "" {
return "{}"
}
return v.Name()
case *types.Type:
switch d := v.Desc.(type) {
case types.StructDesc:
if d.Name == "" {
return "struct {}"
}
return "struct " + d.Name
default:
return v.Desc.Kind().String()
}
}
panic("unreachable")
}
// getMetaChildren returns the nodeChild children, as refs, of v if it's backed
// by a meta sequence, or nil if not.
//
// This isn't exposed directly on the API but for now just guess it:
// - If there are no chunks, it must be a leaf.
// - If there are MORE chunks than the length of the blob/collection then it
// can only be a leaf with multiple ref values per entry.
// - If there are EQUAL then it could be either, but heuristically assume
// that it's a leaf with a ref value per entry. It's highly unlikely that a
// blob/collection will chunk with single elements.
// - If there are LESS then it could be either a chunked blob/collection or a
// collection of mixed types, but heuristically assume that's it's chunked.
func getMetaChildren(v types.Value) (children []nodeChild) {
var l uint64
if col, ok := v.(types.Collection); ok {
l = col.Len()
} else {
l = v.(types.Blob).Len()
}
v.WalkRefs(func(r types.Ref) {
if r.TargetType().Desc.Kind() == v.Kind() {
children = append(children, nodeChild{
Value: info(r, "#"+r.TargetHash().String()),
})
}
})
if uint64(len(children)) >= l {
children = nil
}
return
}
func nodeHasChildren(v types.Value) bool {
switch k := v.Kind(); k {
case types.BlobKind, types.BoolKind, types.NumberKind, types.StringKind:
return false
case types.RefKind:
return true
case types.ListKind, types.SetKind, types.MapKind:
return v.(types.Collection).Len() > 0
case types.StructKind:
return v.(types.Struct).Len() > 0
case types.TypeKind:
switch d := v.(*types.Type).Desc.(type) {
case types.CompoundDesc:
return len(d.ElemTypes) > 0
case types.StructDesc:
return d.Len() > 0
default:
return false // TODO: cycles?
}
default:
panic(fmt.Errorf("unreachable kind %s", k.String()))
}
}
func info(v types.Value, id string) nodeInfo {
return nodeInfo{
HasChildren: nodeHasChildren(v),
ID: id,
Name: nodeName(v),
}
}