/
decode.go
157 lines (145 loc) · 3.46 KB
/
decode.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
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package graphql
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/encoding"
)
// Unmarshal parses the JSON-encoded data and stores the result in dst.
// Node IDs are obtained from the JSON fields identified by the uid parameter.
// UIDs obtained from the JSON encoding must map to unique node ID values
// consistently across the JSON-encoded spanning tree.
func Unmarshal(data []byte, uid string, dst encoding.Builder) error {
if uid == "" {
return errors.New("graphql: invalid UID field name")
}
var src json.RawMessage
err := json.Unmarshal(data, &src)
if err != nil {
return err
}
gen := generator{dst: dst, uidName: uid, nodes: make(map[string]graph.Node)}
return gen.walk(src, nil, "")
}
// StringIDSetter is a graph node that can set its ID based on the given uid string.
type StringIDSetter interface {
SetIDFromString(uid string) error
}
// LabelSetter is a graph edge that can set its label.
type LabelSetter interface {
SetLabel(string)
}
type generator struct {
dst encoding.Builder
// uidName is the name of the UID field in the source JSON.
uidName string
// nodes maps from GraphQL UID string to graph.Node.
nodes map[string]graph.Node
}
func (g *generator) walk(src json.RawMessage, node graph.Node, attr string) error {
switch src[0] {
case '{':
var val map[string]json.RawMessage
err := json.Unmarshal(src, &val)
if err != nil {
return err
}
if next, ok := val[g.uidName]; !ok {
if node != nil {
var buf bytes.Buffer
err := json.Compact(&buf, src)
if err != nil {
panic(err)
}
return fmt.Errorf("graphql: no UID for node: `%s`", &buf)
}
} else {
var v interface{}
err = json.Unmarshal(next, &v)
if err != nil {
return err
}
value := fmt.Sprint(v)
child, ok := g.nodes[value]
if !ok {
child = g.dst.NewNode()
s, ok := child.(StringIDSetter)
if !ok {
return errors.New("graphql: cannot set UID")
}
err = s.SetIDFromString(value)
if err != nil {
return err
}
g.nodes[value] = child
g.dst.AddNode(child)
}
if node != nil {
e := g.dst.NewEdge(node, child)
if s, ok := e.(LabelSetter); ok {
s.SetLabel(attr)
}
g.dst.SetEdge(e)
}
node = child
}
for attr, src := range val {
if attr == g.uidName {
continue
}
err = g.walk(src, node, attr)
if err != nil {
return err
}
}
case '[':
var val []json.RawMessage
err := json.Unmarshal(src, &val)
if err != nil {
return err
}
for _, src := range val {
err = g.walk(src, node, attr)
if err != nil {
return err
}
}
default:
var v interface{}
err := json.Unmarshal(src, &v)
if err != nil {
return err
}
if attr == g.uidName {
value := fmt.Sprint(v)
if s, ok := node.(StringIDSetter); ok {
if _, ok := g.nodes[value]; !ok {
err = s.SetIDFromString(value)
if err != nil {
return err
}
g.nodes[value] = node
}
} else {
return errors.New("graphql: cannot set ID")
}
} else if s, ok := node.(encoding.AttributeSetter); ok {
var value string
if _, ok := v.(float64); ok {
value = string(src)
} else {
value = fmt.Sprint(v)
}
err = s.SetAttribute(encoding.Attribute{Key: attr, Value: value})
if err != nil {
return err
}
}
}
return nil
}