aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlparser/ast/dumper.go
blob: dbb7a7efaf3818e4216b13811da1c6532759b712 (plain) (blame)
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
package ast

import (
	"bytes"
	"fmt"
	"reflect"
	"strconv"
	"strings"
)

// Dump turns ast into a stable string format for assertions in tests
func Dump(i interface{}) string {
	v := reflect.ValueOf(i)

	d := dumper{Buffer: &bytes.Buffer{}}
	d.dump(v)

	return d.String()
}

type dumper struct {
	*bytes.Buffer
	indent int
}

type Dumpable interface {
	Dump() string
}

func (d *dumper) dump(v reflect.Value) {
	if dumpable, isDumpable := v.Interface().(Dumpable); isDumpable {
		d.WriteString(dumpable.Dump())
		return
	}
	switch v.Kind() {
	case reflect.Bool:
		if v.Bool() {
			d.WriteString("true")
		} else {
			d.WriteString("false")
		}
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		d.WriteString(fmt.Sprintf("%d", v.Int()))

	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		d.WriteString(fmt.Sprintf("%d", v.Uint()))

	case reflect.Float32, reflect.Float64:
		d.WriteString(fmt.Sprintf("%.2f", v.Float()))

	case reflect.String:
		if v.Type().Name() != "string" {
			d.WriteString(v.Type().Name() + "(" + strconv.Quote(v.String()) + ")")
		} else {
			d.WriteString(strconv.Quote(v.String()))
		}

	case reflect.Array, reflect.Slice:
		d.dumpArray(v)

	case reflect.Interface, reflect.Ptr:
		d.dumpPtr(v)

	case reflect.Struct:
		d.dumpStruct(v)

	default:
		panic(fmt.Errorf("unsupported kind: %s\n buf: %s", v.Kind().String(), d.String()))
	}
}

func (d *dumper) writeIndent() {
	d.Buffer.WriteString(strings.Repeat("  ", d.indent))
}

func (d *dumper) nl() {
	d.Buffer.WriteByte('\n')
	d.writeIndent()
}

func typeName(t reflect.Type) string {
	if t.Kind() == reflect.Ptr {
		return typeName(t.Elem())
	}
	return t.Name()
}

func (d *dumper) dumpArray(v reflect.Value) {
	d.WriteString("[" + typeName(v.Type().Elem()) + "]")

	for i := 0; i < v.Len(); i++ {
		d.nl()
		d.WriteString("- ")
		d.indent++
		d.dump(v.Index(i))
		d.indent--
	}
}

func (d *dumper) dumpStruct(v reflect.Value) {
	d.WriteString("<" + v.Type().Name() + ">")
	d.indent++

	typ := v.Type()
	for i := 0; i < v.NumField(); i++ {
		f := v.Field(i)
		if typ.Field(i).Tag.Get("dump") == "-" {
			continue
		}

		if isZero(f) {
			continue
		}
		d.nl()
		d.WriteString(typ.Field(i).Name)
		d.WriteString(": ")
		d.dump(v.Field(i))
	}

	d.indent--
}

func isZero(v reflect.Value) bool {
	switch v.Kind() {
	case reflect.Ptr, reflect.Interface:
		return v.IsNil()
	case reflect.Func, reflect.Map:
		return v.IsNil()

	case reflect.Array, reflect.Slice:
		if v.IsNil() {
			return true
		}
		z := true
		for i := 0; i < v.Len(); i++ {
			z = z && isZero(v.Index(i))
		}
		return z
	case reflect.Struct:
		z := true
		for i := 0; i < v.NumField(); i++ {
			z = z && isZero(v.Field(i))
		}
		return z
	case reflect.String:
		return v.String() == ""
	}

	// Compare other types directly:
	return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()))
}

func (d *dumper) dumpPtr(v reflect.Value) {
	if v.IsNil() {
		d.WriteString("nil")
		return
	}
	d.dump(v.Elem())
}