arran4 / golang-ical

A ICS / ICal parser and serialiser for Golang.
Apache License 2.0
279 stars 72 forks source link

add func to add an existing event to calendar #45

Closed fty4 closed 2 years ago

fty4 commented 2 years ago

Description Add function to add existing events to a calendar.

Background This will be required because I want to add the same event to multiple calendars. Currently I have to do it e.g. this way:

// old way
e2 := ics.VEvent{}
e2.SetSummary("TestSum - old way")
c.Components = append(c.Components, &e2)

Another option also could be adding the already added event (e := c1.AddEvent()) to the second calendar (c2.Compotents = append(c2.Components, &e)). In my opinion, both methods are not really elegant.

New implementation Instead of using the unpleasant way by accessing Components directly it would be great to have another AddEvent function. Therefore I added the function AddVEvent (naming suggestions are welcome here) to be able to add a existing event instead of creating a new one with AddEvent.

(Also added some line breaks to separate functions)

Example The code works as follows:

c := ics.NewCalendar()

// new way
e1 := ics.VEvent{}
e1.SetSummary("TestSum - new way")
c.AddVEvent(&e1)

// old way
e2 := ics.VEvent{}
e2.SetSummary("TestSum - old way")
c.Components = append(c.Components, &e2)

// regular way
e3 := c.AddEvent("1234")
e3.SetSummary("TestSum - regular way")

// print calendar
c_str := c.Serialize()
fmt.Println(c_str)

output:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//arran4//Golang ICS Library
BEGIN:VEVENT
SUMMARY:TestSum - new way
END:VEVENT
BEGIN:VEVENT
SUMMARY:TestSum - old way
END:VEVENT
BEGIN:VEVENT
UID:1234
SUMMARY:TestSum - regular way
END:VEVENT
END:VCALENDAR
fty4 commented 2 years ago

To be 100% correct I would suggest to rename AddEvent() to NewEvent() (as used for NewCalendar()). Because it will not really add an event - it will create (and add) one instead.

But due compatibility it might be not the perfect choice to change..

arran4 commented 2 years ago

LGTM

But due compatibility it might be not the perfect choice to change..

Definitely an issue. There is (given if / when) I have more time I would like to V2 this library with a lot of fixes.