Enough-Software / enough_icalendar

iCalendar library to parse, generate and respond to iCal / ics invites. Fully compliant with RFC 5545 (iCalendar) and RFC 5546 (iTIP).
Mozilla Public License 2.0
10 stars 9 forks source link

Is there a way to add custom parsers? #4

Closed jacovdbosch closed 3 years ago

jacovdbosch commented 3 years ago

I added a custom property, but I keep getting this error when adding the property to a component.

No property implementation found for X-CALENDAR-ID:1

I tried to fix this by providing a custom parser on VComponent.parse(ical, customParser: (name, definition) {}), but this does not seem to get used. Is there any way to register properties without having to edit the code directly?

robert-virkus commented 3 years ago

Thanks for the bug report, this is exactly what customParser was meant for, but it seems this is not working as intended.

As a workaround you can still access unsupported properties in their text-representation by calling VComponent.getProperty(), e.g. in your case:

final id = calendar.getProperty<TextProperty>('X-CALENDAR-ID')?.text;
jacovdbosch commented 3 years ago

Yes, thanks for picking this up. When I use the given solution, I get type 'Property' is not a subtype of type 'TextProperty?' in type cast. This might be because I do not have null-safety turned on however, but I do not know for sure. It does seem to work with

final id = event.getProperty('X-CALENDAR-ID')?.textValue;

So I will be using that for now.

robert-virkus commented 3 years ago

Sorry, you are right of course - without knowing the property, it will always be the generic Property and not TextProperty that will be generated. So your code is correct.

robert-virkus commented 3 years ago

This is now released as v 0.10.0

What is X-CALENDAR-ID used for and is it always a numeric value?

robert-virkus commented 3 years ago

Usage example:

test('Calendar simple', () {
    final text = '''BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
X-XXX-NUMBER:123
BEGIN:VEVENT
UID:19970610T172345Z-AF23B2@example.com
DTSTAMP:19970610T172345Z
DTSTART:19970714T170000Z
DTEND:19970715T040000Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR''';
    // final calendar = VComponent.parse(text);
    final calendar = VComponent.parse(text, customParser: (name, definition) {
      if (name == 'X-XXX-NUMBER') {
        return IntegerProperty(definition);
      }
    });
    expect(calendar, isInstanceOf<VCalendar>());
    expect(calendar.children, isNotEmpty);
    expect(
        calendar.getProperty('X-XXX-NUMBER'), isInstanceOf<IntegerProperty>());
    expect(
        calendar.getProperty<IntegerProperty>('X-XXX-NUMBER')?.intValue, 123);
  });
jacovdbosch commented 3 years ago

This is now released as v 0.10.0

What is X-CALENDAR-ID used for and is it always a numeric value?

Yeah, I add it to the events, so I have a reference back to the original calendar, in this case it is an integer value. Thanks for fixing the bug.