-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgranularity.go
More file actions
44 lines (37 loc) · 845 Bytes
/
granularity.go
File metadata and controls
44 lines (37 loc) · 845 Bytes
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
package godruid
import (
"encoding/json"
"time"
)
type Period string
var (
P1D Period = "P1D"
P1W Period = "P1W"
P1M Period = "P1M"
)
type Granularity struct {
GranularityType string `json:"type"`
Period Period `json:"period"`
TimeZone string `json:"timeZone"`
Origin time.Time `json:"origin"`
}
func NewPeriodGranularity(period Period, timeZone string, origin time.Time) Granularity {
return Granularity{
GranularityType: "period",
Period: period,
TimeZone: timeZone,
Origin: origin,
}
}
func NewAllGranularity() Granularity {
return Granularity{
GranularityType: "all",
}
}
func (g Granularity) MarshalJSON() ([]byte, error) {
type Alias Granularity
if g.GranularityType == "all" {
return []byte("\"all\""), nil
}
return json.Marshal((Alias)(g))
}