-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_row.go
146 lines (116 loc) · 3.02 KB
/
json_row.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
package hake
import (
"bytes"
"encoding/json"
"io"
"path"
"cloud.google.com/go/spanner"
"github.com/tenntenn/jsonschema"
)
// JSONRow is an encodable type of spanner.Row.
type JSONRow spanner.Row
var _ json.Marshaler = (*JSONRow)(nil)
// MarshalJSON implements json.Marshaler
func (r *JSONRow) MarshalJSON() ([]byte, error) {
row := (*spanner.Row)(r)
names := row.ColumnNames()
m := make(map[string]interface{}, len(names))
for _, n := range names {
var col spanner.GenericColumnValue
if err := row.ColumnByName(n, &col); err != nil {
return nil, err
}
m[n] = (*JSONColumn)(&col)
}
return json.Marshal(m)
}
// JSONObject is interface of JSON object.
type JSONObject interface {
Set(key string, value interface{})
Get(key string) (interface{}, bool)
Ref() string
}
type mapJSONObject struct {
m map[string]interface{}
ref string
}
func (o *mapJSONObject) Set(key string, value interface{}) {
o.m[key] = value
}
func (o *mapJSONObject) Get(key string) (value interface{}, ok bool) {
value, ok = o.m[key]
return
}
func (o *mapJSONObject) Ref() string {
return o.ref
}
// Schema writes JSON Schema of the row to writer w.
func (r *JSONRow) JSONSchema(w io.Writer, options ...jsonschema.Option) error {
type colSchema struct {
Name string
Schema string
}
names := (*spanner.Row)(r).ColumnNames()
cols := make([]colSchema, len(names))
var buf bytes.Buffer
for i := range names {
var col spanner.GenericColumnValue
if err := (*spanner.Row)(r).ColumnByName(names[i], &col); err != nil {
return err
}
o := &mapJSONObject{
m: map[string]interface{}{},
ref: path.Join("#/properties", names[i]),
}
opts := make([]jsonschema.Option, len(options)+1)
copy(opts, options)
opts[len(opts)-1] = jsonschema.ByReference(o.Ref(), jsonschema.PropertyOrder(i))
if err := (*JSONColumn)(&col).schema(o, col.Type, opts...); err != nil {
return err
}
if err := json.NewEncoder(&buf).Encode(o.m); err != nil {
return err
}
cols[i] = colSchema{
Name: names[i],
Schema: buf.String(),
}
buf.Reset()
}
if err := jsonSchemaTmpl.Execute(&buf, cols); err != nil {
return err
}
var compact bytes.Buffer
if err := json.Compact(&compact, buf.Bytes()); err != nil {
return err
}
if _, err := io.Copy(w, &compact); err != nil {
return err
}
return nil
}
// JSONRows is an encodable type of []*spanner.Row.
type JSONRows []*spanner.Row
var _ json.Marshaler = (JSONRows)(nil)
// At returns ith row as *JSONRow.
func (rs JSONRows) At(i int) *JSONRow {
return (*JSONRow)(rs[i])
}
func (rs JSONRows) toJSONRowSlice() []*JSONRow {
if rs == nil {
return nil
}
rows := make([]*JSONRow, len(rs))
for i := range rs {
rows[i] = rs.At(i)
}
return rows
}
// MarshalJSON implements json.Marshaler
func (rs JSONRows) MarshalJSON() ([]byte, error) {
return json.Marshal(rs.toJSONRowSlice())
}
// Schema writes JSON Schema of the rows to writer w.
func (rs JSONRows) JSONSchema(w io.Writer, options ...jsonschema.Option) error {
return jsonschema.Generate(w, rs.toJSONRowSlice(), options...)
}