forked from stephens2424/rrule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrequency.go
71 lines (65 loc) · 1.27 KB
/
frequency.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
package rrule
import (
"encoding/json"
"errors"
"github.com/spf13/cast"
"log"
"strconv"
)
// Frequency defines a set of constants for a base factor for how often recurrences happen.
type Frequency int
// String returns the RFC 5545 string for supported frequencies, and panics otherwise.
func (f Frequency) String() string {
switch f {
case Secondly:
return "SECONDLY"
case Minutely:
return "MINUTELY"
case Hourly:
return "HOURLY"
case Daily:
return "DAILY"
case Weekly:
return "WEEKLY"
case Monthly:
return "MONTHLY"
case Yearly:
return "YEARLY"
}
log.Panicf("%d is not a supported frequency constant", f)
return ""
}
// Frequencies specified in RFC 5545.
const (
Secondly Frequency = iota
Minutely
Hourly
Daily
Weekly
Monthly
Yearly
)
func (f Frequency) MarshalJSON() ([]byte, error) {
return json.Marshal(int(f))
}
func (d *Frequency) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case int,int32,float64,float32,int64:
*d = Frequency(cast.ToInt(value))
return nil
case string:
var err error
i, err := strconv.Atoi(v.(string))
if err != nil {
return err
}
*d = Frequency(i)
return nil
default:
return errors.New("invalid frequency")
}
}