-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_param.go
More file actions
72 lines (63 loc) · 1.62 KB
/
format_param.go
File metadata and controls
72 lines (63 loc) · 1.62 KB
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
package picker
import (
"encoding/json"
"github.com/chanced/dynamic"
)
const DefaultFormat = "strict_date_optional_time||epoch_millis"
// WithFormat is a type with a format parameter
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
type WithFormat interface {
// The date format(s) that can be parsed. Defaults to
// "strict_date_optional_time||epoch_millis."
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
Format() string
// SetFormat sets the format to v
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
SetFormat(v string)
}
type formatParam struct {
format string // format
}
//Format is the format(s) that the that can be parsed. Defaults to strict_date_optional_time||epoch_millis.
//
// Multiple formats can be seperated by ||
func (f formatParam) Format() string {
if f.format == "" {
return DefaultFormat
}
return f.format
}
func (f *formatParam) SetFormat(v string) {
if v != f.Format() {
f.format = v
}
}
func unmarshalFormatParam(value dynamic.JSON, target interface{}) error {
if r, ok := target.(WithFormat); ok {
if value.IsNull() {
return nil
}
if value.IsString() {
var str string
err := json.Unmarshal(value, &str)
if err != nil {
return err
}
r.SetFormat(str)
}
}
return nil
}
func marshalFormatParam(source interface{}) (dynamic.JSON, error) {
if b, ok := source.(WithFormat); ok {
if b.Format() != DefaultFormat {
return json.Marshal(b.Format())
}
}
return nil, nil
}