Skip to content

Commit

Permalink
Rename CalendarItem -> CalendarEvent to make more intuitive to know that
Browse files Browse the repository at this point in the history
it maps to a ical VEVENT type.
  • Loading branch information
dferrazm committed Dec 14, 2016
1 parent 191fa98 commit 4811ac5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion ical/calendar.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ical

type Calendar struct {
Items []CalendarItem
Items []CalendarEvent
}

func (this *Calendar) Serialize() string {
Expand Down
16 changes: 8 additions & 8 deletions ical/calendar_item.go → ical/calendar_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"
)

type CalendarItem struct {
type CalendarEvent struct {
Id string
Summary string
Description string
Expand All @@ -15,26 +15,26 @@ type CalendarItem struct {
EndAt *time.Time
}

func (this *CalendarItem) StartAtUTC() *time.Time {
func (this *CalendarEvent) StartAtUTC() *time.Time {
return inUTC(this.StartAt)
}

func (this *CalendarItem) EndAtUTC() *time.Time {
func (this *CalendarEvent) EndAtUTC() *time.Time {
return inUTC(this.EndAt)
}

func (this *CalendarItem) Serialize() string {
func (this *CalendarEvent) Serialize() string {
buffer := new(strBuffer)
return this.serializeWithBuffer(buffer)
}

func (this *CalendarItem) ToICS() string {
func (this *CalendarEvent) ToICS() string {
return this.Serialize()
}

func (this *CalendarItem) serializeWithBuffer(buffer *strBuffer) string {
serializer := calItemSerializer{
item: this,
func (this *CalendarEvent) serializeWithBuffer(buffer *strBuffer) string {
serializer := calEventSerializer{
event: this,
buffer: buffer,
}
return serializer.serialize()
Expand Down
30 changes: 15 additions & 15 deletions ical/calendar_item_test.go → ical/calendar_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
)

func TestStartAndEndAtUTC(t *testing.T) {
item := CalendarItem{}
event := CalendarEvent{}

if item.StartAtUTC() != nil {
if event.StartAtUTC() != nil {
t.Error("StartAtUTC should have been nil")
}
if item.EndAtUTC() != nil {
if event.EndAtUTC() != nil {
t.Error("EndAtUTC should have been nil")
}

tUTC := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
item.StartAt = &tUTC
item.EndAt = &tUTC
startTime := *(item.StartAtUTC())
endTime := *(item.EndAtUTC())
event.StartAt = &tUTC
event.EndAt = &tUTC
startTime := *(event.StartAtUTC())
endTime := *(event.EndAtUTC())

if startTime != tUTC {
t.Error("StartAtUTC should have been", tUTC, ", but was", startTime)
Expand All @@ -35,10 +35,10 @@ func TestStartAndEndAtUTC(t *testing.T) {
panic(err)
}
tNYK := tUTC.In(nyk)
item.StartAt = &tNYK
item.EndAt = &tNYK
startTime = *(item.StartAtUTC())
endTime = *(item.EndAtUTC())
event.StartAt = &tNYK
event.EndAt = &tNYK
startTime = *(event.StartAtUTC())
endTime = *(event.EndAtUTC())

if startTime != tUTC {
t.Error("StartAtUTC should have been", tUTC, ", but was", startTime)
Expand All @@ -48,7 +48,7 @@ func TestStartAndEndAtUTC(t *testing.T) {
}
}

func TestCalendarItemSerialize(t *testing.T) {
func TestCalendarEventSerialize(t *testing.T) {
ny, err := time.LoadLocation("America/New_York")
if err != nil {
panic(err)
Expand All @@ -59,7 +59,7 @@ func TestCalendarItemSerialize(t *testing.T) {
startsAt := createdAt.Add(time.Second * 2).In(ny)
endsAt := createdAt.Add(time.Second * 3).In(ny)

item := CalendarItem {
event := CalendarEvent {
Id: "123",
CreatedAtUTC: &createdAt,
ModifiedAtUTC: &modifiedAt,
Expand All @@ -84,8 +84,8 @@ DESCRIPTION:Lorem\nIpsum
LOCATION:Berlin\nGermany
END:VEVENT`

output := item.Serialize()
output := event.Serialize()
if output != strings.TrimSpace(expected) {
t.Error("Expected calendar item serialization to be:\n", expected, "\n\nbut got:\n", output)
t.Error("Expected calendar event serialization to be:\n", expected, "\n\nbut got:\n", output)
}
}
8 changes: 4 additions & 4 deletions ical/calendar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import (
)

func TestCalendarSerialize(t *testing.T) {
calend := new(Calendar)
calendar := new(Calendar)

// test calendar w/o items

expected := "BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR"
output := calend.Serialize()
output := calendar.Serialize()

if output != expected {
t.Error("\nExpected calendar serialization to be:\n", expected, "\n\nbut got:\n", output)
}

// test calendar with items

calend.Items = append(calend.Items, CalendarItem{Summary: "Foo"})
calendar.Items = append(calendar.Items, CalendarEvent{Summary: "Foo"})

expected = "BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\nSUMMARY:Foo\nEND:VEVENT\nEND:VCALENDAR"
output = calend.Serialize()
output = calendar.Serialize()

if output != expected {
t.Error("\nExpected calendar serialization to be:\n", expected, "\n\nbut got:\n", output)
Expand Down
54 changes: 27 additions & 27 deletions ical/serializers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ func (this *calSerializer) items() {
}
}

type calItemSerializer struct {
item *CalendarItem
type calEventSerializer struct {
event *CalendarEvent
buffer *strBuffer
}

const (
itemSerializerTimeFormat = "20060102T150405Z"
eventSerializerTimeFormat = "20060102T150405Z"
)

func (this *calItemSerializer) serialize() string {
this.serializeItem()
func (this *calEventSerializer) serialize() string {
this.serializeEvent()
return strings.TrimSpace(this.buffer.String())
}

func (this *calItemSerializer) serializeItem() {
func (this *calEventSerializer) serializeEvent() {
this.begin()
this.uid()
this.created()
Expand All @@ -67,55 +67,55 @@ func (this *calItemSerializer) serializeItem() {
this.end()
}

func (this *calItemSerializer) begin() {
func (this *calEventSerializer) begin() {
this.buffer.Write("BEGIN:VEVENT\n")
}

func (this *calItemSerializer) end() {
func (this *calEventSerializer) end() {
this.buffer.Write("END:VEVENT\n")
}

func (this *calItemSerializer) uid() {
this.serializeStringProp("UID", this.item.Id)
func (this *calEventSerializer) uid() {
this.serializeStringProp("UID", this.event.Id)
}

func (this *calItemSerializer) summary() {
this.serializeStringProp("SUMMARY", this.item.Summary)
func (this *calEventSerializer) summary() {
this.serializeStringProp("SUMMARY", this.event.Summary)
}

func (this *calItemSerializer) description() {
this.serializeStringProp("DESCRIPTION", this.item.Description)
func (this *calEventSerializer) description() {
this.serializeStringProp("DESCRIPTION", this.event.Description)
}

func (this *calItemSerializer) location() {
this.serializeStringProp("LOCATION", this.item.Location)
func (this *calEventSerializer) location() {
this.serializeStringProp("LOCATION", this.event.Location)
}

func (this *calItemSerializer) dtstart() {
this.serializeTimeProp("DTSTART", this.item.StartAtUTC())
func (this *calEventSerializer) dtstart() {
this.serializeTimeProp("DTSTART", this.event.StartAtUTC())
}

func (this *calItemSerializer) dtend() {
this.serializeTimeProp("DTEND", this.item.EndAtUTC())
func (this *calEventSerializer) dtend() {
this.serializeTimeProp("DTEND", this.event.EndAtUTC())
}

func (this *calItemSerializer) created() {
this.serializeTimeProp("CREATED", this.item.CreatedAtUTC)
func (this *calEventSerializer) created() {
this.serializeTimeProp("CREATED", this.event.CreatedAtUTC)
}

func (this *calItemSerializer) lastModified() {
this.serializeTimeProp("LAST-MODIFIED", this.item.ModifiedAtUTC)
func (this *calEventSerializer) lastModified() {
this.serializeTimeProp("LAST-MODIFIED", this.event.ModifiedAtUTC)
}

func (this *calItemSerializer) serializeStringProp(name, value string) {
func (this *calEventSerializer) serializeStringProp(name, value string) {
if value != "" {
escapedValue := escapeTextType(value)
this.buffer.Write("%s:%s\n", name, escapedValue)
}
}

func (this *calItemSerializer) serializeTimeProp(name string, value *time.Time) {
func (this *calEventSerializer) serializeTimeProp(name string, value *time.Time) {
if value != nil {
this.buffer.Write("%s:%s\n", name, value.Format(itemSerializerTimeFormat))
this.buffer.Write("%s:%s\n", name, value.Format(eventSerializerTimeFormat))
}
}

0 comments on commit 4811ac5

Please sign in to comment.