arran4 / golang-ical

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

add NewEvent function #46

Closed fty4 closed 2 years ago

fty4 commented 2 years ago

Description This will allow to create a event with uniqueId.

Background Currently it is very easy to create a VEvent with e := ics.VEvent{}. But there is a issue with setting the uniqueId. This is not easy possible without a big efford which it should not be.

Unfortunally there is no direct function for changing the uid. But it is probably better to address this functionality directly in the event creation anyway.

New implementation

Example The code works as follows:

c := ics.NewCalendar()

// new way
e1 := ics.NewEvent("id")
e1.SetSummary("new way")
c.Components = append(c.Components, e1)

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

// old way with id
id := "id"
e3 := ics.VEvent{
ics.ComponentBase{
    Properties: []ics.IANAProperty{
        {ics.BaseProperty{IANAToken: ics.ToText(string(ics.ComponentPropertyUniqueId)), Value: id}},
    },
},
}
e3.SetSummary("old way with id")
c.Components = append(c.Components, &e3)

// regular way
e4 := c.AddEvent("1234")
e4.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
UID:id
SUMMARY:new way
END:VEVENT
BEGIN:VEVENT
SUMMARY:old way
END:VEVENT
BEGIN:VEVENT
UID:id
SUMMARY:old way with id
END:VEVENT
BEGIN:VEVENT
UID:1234
SUMMARY:TestSum - regular way
END:VEVENT
END:VCALENDAR

With #45 merged the event also can be added straightforward with:

e5 := ics.NewEvent("id")
e5.SetSummary("with AddVEvent")
c.AddVEvent(e5)
arran4 commented 2 years ago

LGTM. Wish github's line difference algorithm would be a little more forgiving on line movements. (Change looks more significant than what it is.)

I hope there isn't a merge order with this.

Thanks for the effort @fty4 I like a rich set of helper/utility functions with my api. (Hypocritically I didn't provide many in the original.)