/
calendar.go
121 lines (108 loc) · 2.96 KB
/
calendar.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
package ical
import (
"fmt"
"io"
"github.com/knsh14/ical/component"
"github.com/knsh14/ical/parameter"
"github.com/knsh14/ical/property"
"github.com/knsh14/ical/types"
)
func NewCalendar() *Calendar {
return &Calendar{Version: property.NewVersion()}
}
// Calendar is root object of ical
// https://tools.ietf.org/html/rfc5545#section-3.4
// https://tools.ietf.org/html/rfc5545#section-3.5
// https://tools.ietf.org/html/rfc5545#section-3.6
type Calendar struct {
// required field
ProdID *property.ProdID
Version *property.Version
// optional
CalScale *property.CalScale
Method *property.Method
XProperties []*property.NonStandard // https://tools.ietf.org/html/rfc5545#section-3.8.8.2
IANAProperties []*property.IANA
Components []CalenderComponent
}
func (c *Calendar) Decode(w io.Writer) error {
fmt.Fprintf(w, "%s:%s", property.NameBegin, component.TypeCalendar)
if err := c.ProdID.Decode(w); err != nil {
return err
}
if err := c.Version.Decode(w); err != nil {
return err
}
if err := c.CalScale.Decode(w); err != nil {
return err
}
if err := c.Method.Decode(w); err != nil {
return err
}
fmt.Fprintf(w, "%s:%s", property.NameEnd, component.TypeCalendar)
return nil
}
func (c *Calendar) Validate() error {
if c.ProdID == nil {
return NewValidationError(component.TypeCalendar, property.NameProdID, "must not to be nil")
}
if c.ProdID.Value == "" {
return NewValidationError(component.TypeCalendar, property.NameProdID, "must not to be empty")
}
if c.Version == nil {
return NewValidationError(component.TypeCalendar, property.NameVersion, "must not to be nil")
}
if c.Version.Max == "" {
return NewValidationError(component.TypeCalendar, property.NameVersion, "max must not to be empty")
}
for _, component := range c.Components {
if err := component.Validate(); err != nil {
return fmt.Errorf("%w", err)
}
}
return nil
}
func (c *Calendar) SetCalScale(params parameter.Container, value types.Text) error {
if c.CalScale != nil {
return c.CalScale.SetCalScale(params, value)
}
cs := &property.CalScale{}
if err := cs.SetCalScale(params, value); err != nil {
return err
}
c.CalScale = cs
return nil
}
func (c *Calendar) SetMethod(params parameter.Container, value types.Text) error {
if c.Method != nil {
return c.Method.SetMethod(params, value)
}
m := &property.Method{}
if err := m.SetMethod(params, value); err != nil {
return err
}
c.Method = m
return nil
}
func (c *Calendar) SetProdID(params parameter.Container, value types.Text) error {
if c.ProdID != nil {
return c.ProdID.SetProdID(params, value)
}
pid := &property.ProdID{}
if err := pid.SetProdID(params, value); err != nil {
return err
}
c.ProdID = pid
return nil
}
func (c *Calendar) SetVersion(params parameter.Container, value types.Text) error {
if c.Version != nil {
return c.Version.SetVersion(params, value)
}
ver := property.NewVersion()
if err := ver.SetVersion(params, value); err != nil {
return err
}
c.Version = ver
return nil
}