-
Notifications
You must be signed in to change notification settings - Fork 28
/
marshal.go
235 lines (201 loc) · 6.06 KB
/
marshal.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
Copyright 2017-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package cbft
import (
"encoding/json"
"fmt"
"io"
"time"
"unsafe"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/search"
"github.com/blevesearch/bleve/v2/search/query"
jsoniter "github.com/json-iterator/go"
)
func init() {
// registers the custom json encoders with jsoniter
registerCustomJSONEncoders()
}
// Marshal abstracts the underlying json lib used
func (p *CustomJSONImpl) Marshal(v interface{}) ([]byte, error) {
return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(v)
}
// Encode abstracts the underlying json lib used
func (p *CustomJSONImpl) Encode(w io.Writer, v interface{}) error {
return jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(w).Encode(v)
}
// MarshalJSON abstracts the underlying json lib used
func MarshalJSON(v interface{}) ([]byte, error) {
if JSONImpl != nil && JSONImpl.GetManagerOption("jsonImpl") != "std" {
return JSONImpl.Marshal(v)
}
return json.Marshal(v)
}
func registerCustomJSONEncoders() {
// adding all the custom encoders that bleve has implemented,
// and need to extend as bleve introduces new custom encoders.
jsoniter.RegisterTypeEncoderFunc("bleve.IndexErrMap", encodeBleveIndexErrMap, nil)
jsoniter.RegisterTypeEncoderFunc("bleve.dateTimeRange", encodeBleveDateTimeRange, nil)
jsoniter.RegisterTypeEncoderFunc("search.SortDocID", encodeSearchSortDocID, nil)
jsoniter.RegisterTypeEncoderFunc("search.SortScore", encodeSearchSortScore, nil)
jsoniter.RegisterTypeEncoderFunc("search.SortField", encodeSearchSortField, nil)
jsoniter.RegisterTypeEncoderFunc("query.BleveQueryTime", encodeBleveQueryTime, nil)
jsoniter.RegisterTypeEncoderFunc("search.SortGeoDistance", encodeSortGeoDistance, nil)
jsoniter.RegisterTypeEncoderFunc("query.MatchAllQuery", encodeMatchAllQuery, nil)
jsoniter.RegisterTypeEncoderFunc("query.MatchNoneQuery", encodeMatchNoneQuery, nil)
jsoniter.RegisterTypeEncoderFunc("query.MatchQueryOperator", encodeMatchQueryOperator, nil)
}
func encodeBleveIndexErrMap(ptr unsafe.Pointer, stream *jsoniter.Stream) {
mapPtr := unsafe.Pointer(&ptr)
iem := *((*bleve.IndexErrMap)(mapPtr))
tmp := make(map[string]string, len(iem))
for k, v := range iem {
tmp[k] = v.Error()
}
stream.WriteVal(tmp)
}
func encodeBleveDateTimeRange(ptr unsafe.Pointer, stream *jsoniter.Stream) {
type temp struct {
Name string `json:"name,omitempty"`
Start time.Time `json:"start,omitempty"`
End time.Time `json:"end,omitempty"`
DateTimeParser string `json:"datetime_parser,omitempty"`
startString *string
endString *string
}
dr := *((*temp)(ptr))
rv := map[string]interface{}{
"name": dr.Name,
}
if !dr.Start.IsZero() {
rv["start"] = dr.Start
} else if dr.startString != nil {
rv["start"] = dr.startString
}
if !dr.End.IsZero() {
rv["end"] = dr.End
} else if dr.endString != nil {
rv["end"] = dr.endString
}
if dr.DateTimeParser != "" {
rv["datetime_parser"] = dr.DateTimeParser
}
stream.WriteVal(rv)
}
func encodeSearchSortDocID(ptr unsafe.Pointer, stream *jsoniter.Stream) {
sid := *((*search.SortDocID)(ptr))
if sid.Desc {
stream.WriteString("-_id")
return
}
stream.WriteString("_id")
}
func encodeSearchSortScore(ptr unsafe.Pointer, stream *jsoniter.Stream) {
ss := *((*search.SortScore)(ptr))
if ss.Desc {
stream.WriteString("-_score")
return
}
stream.WriteString("_score")
}
func encodeSearchSortField(ptr unsafe.Pointer, stream *jsoniter.Stream) {
s := *((*search.SortField)(ptr))
if s.Missing == search.SortFieldMissingLast &&
s.Mode == search.SortFieldDefault &&
s.Type == search.SortFieldAuto {
if s.Desc {
stream.WriteString("-" + s.Field)
return
}
stream.WriteString(s.Field)
return
}
sfm := map[string]interface{}{
"by": "field",
"field": s.Field,
}
if s.Desc {
sfm["desc"] = true
}
if s.Missing > search.SortFieldMissingLast {
switch s.Missing {
case search.SortFieldMissingFirst:
sfm["missing"] = "first"
}
}
if s.Mode > search.SortFieldDefault {
switch s.Mode {
case search.SortFieldMin:
sfm["mode"] = "min"
case search.SortFieldMax:
sfm["mode"] = "max"
}
}
if s.Type > search.SortFieldAuto {
switch s.Type {
case search.SortFieldAsString:
sfm["type"] = "string"
case search.SortFieldAsNumber:
sfm["type"] = "number"
case search.SortFieldAsDate:
sfm["type"] = "date"
}
}
stream.WriteVal(sfm)
}
func encodeBleveQueryTime(ptr unsafe.Pointer, stream *jsoniter.Stream) {
temp := *((*query.BleveQueryTime)(ptr))
tt := time.Time(temp.Time)
stream.WriteString(tt.Format(query.QueryDateTimeFormat))
}
func encodeSortGeoDistance(ptr unsafe.Pointer, stream *jsoniter.Stream) {
s := *((*search.SortGeoDistance)(ptr))
sfm := map[string]interface{}{
"by": "geo_distance",
"field": s.Field,
"location": map[string]interface{}{
"lon": s.Lon,
"lat": s.Lat,
},
}
if s.Unit != "" {
sfm["unit"] = s.Unit
}
if s.Desc {
sfm["desc"] = true
}
stream.WriteVal(sfm)
}
func encodeMatchAllQuery(ptr unsafe.Pointer, stream *jsoniter.Stream) {
q := *((*query.MatchAllQuery)(ptr))
tmp := map[string]interface{}{
"boost": q.BoostVal,
"match_all": map[string]interface{}{},
}
stream.WriteVal(tmp)
}
func encodeMatchNoneQuery(ptr unsafe.Pointer, stream *jsoniter.Stream) {
q := *((*query.MatchNoneQuery)(ptr))
tmp := map[string]interface{}{
"boost": q.BoostVal,
"match_none": map[string]interface{}{},
}
stream.WriteVal(tmp)
}
func encodeMatchQueryOperator(ptr unsafe.Pointer, stream *jsoniter.Stream) {
o := *((*query.MatchQueryOperator)(ptr))
switch o {
case query.MatchQueryOperatorOr:
stream.WriteString("or")
case query.MatchQueryOperatorAnd:
stream.WriteString("and")
default:
stream.Error = fmt.Errorf("cannot marshal match operator %d to JSON", o)
}
}