/
coerce.go
67 lines (62 loc) · 2.02 KB
/
coerce.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
package mapping
// FieldWithCoerce has a Coerce param
//
// Data is not always clean. Depending on how it is produced a number might be
// rendered in the JSON body as a true JSON number, e.g. 5, but it might also be
// rendered as a string, e.g. "5". Alternatively, a number that should be an
// integer might instead be rendered as a floating point, e.g. 5.0, or even "5.0".
//
// Coercion attempts to clean up dirty values to fit the data type of a field.
// For instance:
//
// - Strings will be coerced to numbers.
//
// - Floating points will be truncated for integer values.
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/coerce.html
type FieldWithCoerce interface {
Field
WithCoerce
}
// WithCoerce has a Coerce param
type WithCoerce interface {
// Coerce attempts to clean up dirty values to fit the data type of a field.
// For instance:
//
// Default: false
Coerce() bool
// SetCoerce sets CoerceParam to v
SetCoerce(v bool)
}
// CoerceParam is a mixin for Field mapping types with coerce
//
// Data is not always clean. Depending on how it is produced a number might be
// rendered in the JSON body as a true JSON number, e.g. 5, but it might also be
// rendered as a string, e.g. "5". Alternatively, a number that should be an
// integer might instead be rendered as a floating point, e.g. 5.0, or even
// "5.0".
//
// Coercion attempts to clean up dirty values to fit the data type of a field.
// For instance:
//
// - Strings will be coerced to numbers.
//
// - Floating points will be truncated for integer values.
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/coerce.html
type CoerceParam struct {
CoerceValue *bool `bson:"coerce,omitempty" json:"coerce,omitempty"`
}
// Coerce attempts to clean up dirty values to fit the data type of a field.
func (cp CoerceParam) Coerce() bool {
if cp.CoerceValue != nil {
return *cp.CoerceValue
}
return false
}
// SetCoerce sets CoerceParam to v
func (cp *CoerceParam) SetCoerce(v bool) {
if cp.Coerce() != v {
cp.CoerceValue = &v
}
}