-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_format.go
126 lines (113 loc) · 3.18 KB
/
object_format.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
package vim
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
dlvapi "github.com/go-delve/delve/service/api"
)
type structField struct {
name string
value interface{}
}
type goStruct struct {
typeName string
fields []structField
}
// Converts a variable to an object ready to be marshaled into a JSON
func toValue(variable *dlvapi.Variable) (object interface{}) {
if variable.Type != "" && variable.Addr == 0 {
return nil
}
switch variable.Kind {
case reflect.Invalid:
return nil
case reflect.Pointer, reflect.Interface:
if len(variable.Children) == 0 {
object = nil
} else {
object = toValue(&variable.Children[0])
}
case reflect.Array, reflect.Slice:
list := make([]interface{}, 0, len(variable.Children))
for _, child := range variable.Children {
list = append(list, toValue(&child))
}
object = list
case reflect.Map:
dict := make(map[string]interface{})
for i := 0; i+1 < len(variable.Children); i += 2 {
key := variable.Children[i].Value
value := toValue(&variable.Children[i+1])
dict[key] = value
}
object = dict
default:
if len(variable.Children) == 0 {
return variable.Value
}
structValue := goStruct{
typeName: variable.Type,
fields: make([]structField, 0, len(variable.Children)),
}
for _, child := range variable.Children {
structValue.fields = append(structValue.fields, structField{name: child.Name, value: toValue(&child)})
}
object = structValue
}
return object
}
func formatVariable(variable *dlvapi.Variable) []string {
value := toValue(variable)
marshaled := marshalValue(value, 0)
return strings.Split(string(marshaled), "\n")
}
func marshalValue(value interface{}, nestingLevel int) (marshaled []byte) {
switch typedValue := value.(type) {
case goStruct:
marshaled = marshalGoStruct(typedValue, nestingLevel)
case []interface{}:
marshaled = marshalSlice(typedValue, nestingLevel)
default:
marshaled, _ = json.MarshalIndent(value, indent(nestingLevel), " ")
}
return marshaled
}
func marshalSlice(slice []interface{}, nestingLevel int) (marshaled []byte) {
buf := bytes.NewBuffer(nil)
buf.WriteString(fmt.Sprintf("[\n"))
for i, value := range slice {
buf.WriteString(fmt.Sprintf("%s %s", indent(nestingLevel), string(marshalValue(value, nestingLevel+1))))
if i != len(slice)-1 {
buf.WriteString(",")
}
buf.WriteString("\n")
}
buf.WriteString(fmt.Sprintf("%s]", indent(nestingLevel)))
return buf.Bytes()
}
func marshalGoStruct(structValue goStruct, nestingLevel int) []byte {
buf := bytes.NewBuffer(nil)
maybeComma := ""
if len(structValue.fields) > 0 {
maybeComma = ","
}
buf.WriteString(fmt.Sprintf("{ \"#type\": %q%s\n", structValue.typeName, maybeComma))
for i, field := range structValue.fields {
marshaledField := marshalValue(field.value, nestingLevel+1)
maybeComma = ","
if i == len(structValue.fields)-1 {
maybeComma = ""
}
buf.WriteString(fmt.Sprintf("%s %q: %s%s\n", indent(nestingLevel), field.name, string(marshaledField), maybeComma))
}
buf.WriteString(fmt.Sprintf("%s}", indent(nestingLevel)))
return buf.Bytes()
}
func indent(nestingLevel int) (result string) {
for i := 0; i < nestingLevel; i++ {
result = result + " "
}
return result
}