-
Notifications
You must be signed in to change notification settings - Fork 15
/
raw_op.go
71 lines (60 loc) · 1.41 KB
/
raw_op.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
package quill
import (
"fmt"
"html"
"strconv"
)
type rawOp struct {
// Insert is the string containing the data.
Insert interface{} `json:"insert"`
// Attrs contains the "attributes" property of the op.
Attrs map[string]interface{} `json:"attributes"`
}
// makeOp takes a raw Delta op as extracted from the JSON and turns it into an Op to make it usable for rendering.
func (ro *rawOp) makeOp(o *Op) error {
if ro.Insert == nil {
return fmt.Errorf("op %+v lacks an insert", *ro)
}
switch ins := ro.Insert.(type) {
case string:
// This op is a simple string insert.
o.Type = "text"
o.Data = html.EscapeString(ins)
case map[string]interface{}:
if len(ins) == 0 {
return fmt.Errorf("op %+v lacks a non-text insert", *ro)
}
// There should be one item in the map (the element's key being the insert type).
for mk := range ins {
o.Type = mk
o.Data = extractString(ins[mk])
break
}
default:
return fmt.Errorf("op %+v lacks an insert", *ro)
}
// Clear the map for reuse.
for k := range o.Attrs {
delete(o.Attrs, k)
}
if ro.Attrs != nil {
// The map was already made
for attr := range ro.Attrs {
o.Attrs[attr] = extractString(ro.Attrs[attr])
}
}
return nil
}
func extractString(v interface{}) string {
switch val := v.(type) {
case string:
return val
case bool:
if val == true {
return "y"
}
case float64:
return strconv.FormatFloat(val, 'f', 0, 64)
}
return ""
}