Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1 fix nonstandard property parsing #2

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions compiler/templates/parseProperties.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ export function parseProperty (key: string, value: string) {
/**${PROPERTIES_BLOCK_KEYMATCHER}**/

default:
if (propertyKey.startsWith('X-') || propertyKey.startsWith('IANA-')) {
return new class extends Property<string> {
readonly key;
constructor(key: string, value: string) {
super(value, {});
this.key = key;
}
} (key, value);
}

throw new Error(`Unexpected property '${propertyKey}'`);
return new class extends Property<string> {
constructor(public readonly key: string, value: string) {
super(value, {}, true);
}
} (key, value);
}

}
Expand Down
13 changes: 6 additions & 7 deletions src/ICS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ import Categories from "./Parser/Properties/Categories";
import DateTimeEnd from "./Parser/Properties/DateTimeEnd";

export namespace ICS {
export type NonStandardPropertyAware = { [key: `X-${string}`]: Property|undefined }
export type IANAPropertyAware = { [key: `IANA-${string}`]: Property|undefined }
export type NonStandardPropertyAware = {[key: string]: undefined|Property<unknown>[] };

export type JSON = {
VCALENDAR: VCALENDAR[]
}

export type VCALENDAR = NonStandardPropertyAware & IANAPropertyAware & {
export type VCALENDAR = NonStandardPropertyAware & {
PRODID: Property,
VERSION: Version,
CALSCALE?: CalendarScale,
Expand All @@ -51,23 +50,23 @@ export namespace ICS {
VTIMEZONE?: VTIMEZONE[],
};

export type VTIMEZONE = NonStandardPropertyAware & IANAPropertyAware & {
export type VTIMEZONE = NonStandardPropertyAware & {
TZID: TimeZoneIdentifier,
TZURL?: TimeZoneUrl,
'LAST-MODIFIED'?: LastModified,
DAYLIGHT?: TimezoneDefinition[],
STANDARD?: TimezoneDefinition[]
}

export type TimezoneDefinition = NonStandardPropertyAware & IANAPropertyAware & {
export type TimezoneDefinition = NonStandardPropertyAware & {
COMMENT?: Property[],
TZOFFSETFROM: TimeZoneOffsetFrom, // Example: -0800
TZOFFSETTO: TimeZoneOffsetTo, // Example: -0700
DTSTART: DateTimeStart, // In local format
TZNAME?: TimeZoneName,
} & XOR<{ RRULE?: RecurrenceRule }, {RDATE?: RecurrenceDateTimes[]}>

export type VALARM = NonStandardPropertyAware & IANAPropertyAware & {
export type VALARM = NonStandardPropertyAware & {
ACTION: Action,
TRIGGER: Duration,
DESCRIPTION?: Description,
Expand All @@ -78,7 +77,7 @@ export namespace ICS {
export namespace VEVENT {
export type Published = {DTSTART: DateTimeStart} & Event;

type Event = NonStandardPropertyAware & IANAPropertyAware & {
type Event = NonStandardPropertyAware & {
UID: UniqueIdentifier,
DTSTAMP: DateTimeStamp,
SUMMARY: Summary,
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Components/Alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export default class Alarm extends Component<ICS.VALARM> {

build (context: Context) : ICS.VALARM {
return {
...this.pickNonStandardProperties(context),
ACTION: this.pickOrThrow(context, 'ACTION'),
DESCRIPTION: this.pick(context, 'DESCRIPTION'),
SUMMARY: this.pick(context, 'SUMMARY'),
ATTENDEE: this.pick(context, 'ATTENDEE'),
TRIGGER: this.pickOrThrow(context, 'TRIGGER'),
};
} as ICS.VALARM;
}
}
4 changes: 2 additions & 2 deletions src/Parser/Components/Calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export default class Calendar extends Component<ICS.VCALENDAR> {

protected build(context: Context) : ICS.VCALENDAR {
return {
...this.pickNonStandardProperties(context),
PRODID: this.pickOrThrow(context, 'PRODID'),
VERSION: this.pickOrThrow(context, 'VERSION'),
CALSCALE: this.pick(context, 'CALSCALE'),
COMMENT: this.pick(context, 'COMMENT'),
...this.pickNonStandardProperties(context),
VTIMEZONE: this.pick<ICS.VTIMEZONE[]>(context, ComponentName.VTIMEZONE),
VEVENT: this.pick<ICS.VEVENT.Published[]>(context, ComponentName.VEVENT),
}
} as ICS.VCALENDAR;
}

}
17 changes: 9 additions & 8 deletions src/Parser/Components/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {BEGIN, END, LIST_PROPERTIES, Property as EProperty} from "../Constants";
import {parseProperty} from "../parseProperties";
import Property from "../Properties/Property";
import {ComponentName} from "./ComponentName";
import PropertyList from "../PropertyList";

export type ContextValue = Property<unknown>|Component|undefined;
export type Context = {[key: string]: ContextValue|ContextValue[]|Context|Context[]};
export type Context = {[key: string]: ContextValue|ContextValue[]|PropertyList|Context|Context[]};

export default abstract class Component<S extends object = Context> {
public abstract readonly key: ComponentName;
Expand Down Expand Up @@ -50,12 +51,12 @@ export default abstract class Component<S extends object = Context> {
closed = true;
} else {
const property = parseProperty(name, value);
if (LIST_PROPERTIES.includes(property.key as EProperty)) {
if (LIST_PROPERTIES.includes(property.key as EProperty) || property.isNonStandard) {
if (context[property.key] === undefined) {
context[property.key] = [];
context[property.key] = new PropertyList();
}

(context[property.key] as Property<unknown>[]).push(property);
(context[property.key] as PropertyList).push(property);
} else {
if (context[property.key] !== undefined) {
throw new Error(`Non-list property '${property.key}' appeared twice in component '${this.key}'`);
Expand Down Expand Up @@ -90,11 +91,11 @@ export default abstract class Component<S extends object = Context> {
return data[key] as T;
}

protected pickNonStandardProperties(data: { [key: string]: unknown }): ICS.NonStandardPropertyAware {
const nonStandardPropertyData: { [key: string]: Property } = {};
protected pickNonStandardProperties(data: Context): ICS.NonStandardPropertyAware {
const nonStandardPropertyData: { [key: `${string}`]: Property<unknown>[] } = {};
for (const [key, value] of Object.entries(data)) {
if (key.startsWith('X-') && value instanceof Property) {
nonStandardPropertyData[key] = value as Property;
if (value instanceof PropertyList && value.containsNonStandard()) {
nonStandardPropertyData[key] = value.nonStandardProperties;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Parser/Components/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class Event extends Component<ICS.VEVENT.Published> {

protected build(context: Context) : ICS.VEVENT.Published {
return {
...this.pickNonStandardProperties(context),
DTSTAMP: this.pickOrThrow(context, 'DTSTAMP'),
DTSTART: this.pickOrThrow(context, 'DTSTART'),
...this.pickDurationOrDateTimeEnd(context),
Expand All @@ -38,8 +39,7 @@ export default class Event extends Component<ICS.VEVENT.Published> {
RDATE: this.pick(context, 'RDATE'),
EXDATE: this.pick(context, 'EXDATE'),
ATTENDEE: this.pick(context, 'ATTENDEE'),
...this.pickNonStandardProperties(context),
};
} as ICS.VEVENT.Published;
}

private pickDurationOrDateTimeEnd (data: {[key: string]: unknown}) : undefined|XOR<{DURATION: Duration}, {DTEND: DateTimeEnd}> {
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/Components/Timezone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export default class Timezone extends Component<ICS.VTIMEZONE> {

build (context: Context) : ICS.VTIMEZONE {
return {
TZID: this.pickOrThrow(context, 'TZID'),
...this.pickNonStandardProperties(context),
TZID: this.pickOrThrow(context, 'TZID'),
DAYLIGHT: this.pick(context, 'DAYLIGHT'),
STANDARD: this.pick(context, 'STANDARD'),
}
} as ICS.VTIMEZONE;
}
}
2 changes: 1 addition & 1 deletion src/Parser/Components/TimezoneDefinitionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default abstract class TimezoneDefinitionBase extends Component<ICS.Timez
DTSTART: this.pickOrThrow(context, 'DTSTART'),
...this.pickNonStandardProperties(context),
...this.pickRRuleOrRdate(context),
}
} as ICS.TimezoneDefinition;
}

private pickRRuleOrRdate (data: {[key: string]: unknown}) : undefined | {RRULE: RecurrenceRule} | {RDATE: RecurrenceDateTimes[]} {
Expand Down
7 changes: 1 addition & 6 deletions src/Parser/Properties/Property.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
export default abstract class Property<T = string, P extends {[key: string]: string|string[]|undefined} = Record<string, string|string[]>> {
public abstract readonly key: string;
public readonly value: T;
public readonly parameters: P;

constructor (value: T, parameters: P) {
this.value = value;
this.parameters = parameters;
}
constructor (public readonly value: T, public readonly parameters: P, public readonly isNonStandard: boolean = false) {}

toString () : string {
if (typeof this.value === 'string') {
Expand Down
31 changes: 31 additions & 0 deletions src/Parser/PropertyList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Property from "./Properties/Property";

export default class PropertyList {
private propertyList: Property<unknown>[] = [];
private nonStandardPropertyList: Property<unknown>[] = [];

push (property: Property<unknown>) {
if (property.isNonStandard) {
this.nonStandardPropertyList.push(property);
} else {
this.propertyList.push(property);
}
}

toJSON () {
return [...this.propertyList, ...this.nonStandardPropertyList];
}

containsNonStandard () {
return this.nonStandardPropertyList.length > 0;
}

get properties () {
return this.propertyList;
}

get nonStandardProperties () {
return this.nonStandardPropertyList;
}

}
16 changes: 5 additions & 11 deletions src/Parser/parseProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,11 @@ export function parseProperty (key: string, value: string) {
case 'VERSION': return new Version(assertEnum('Version', parseValueRaw(value), ['2.0'] as const), parameters);

default:
if (propertyKey.startsWith('X-') || propertyKey.startsWith('IANA-')) {
return new class extends Property<string> {
readonly key;
constructor(key: string, value: string) {
super(value, {});
this.key = key;
}
} (key, value);
}

throw new Error(`Unexpected property '${propertyKey}'`);
return new class extends Property<string> {
constructor(public readonly key: string, value: string) {
super(value, {}, true);
}
} (key, value);
}

}
Expand Down
11 changes: 11 additions & 0 deletions tests/__samples__/ics/non-standard-properties.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:blank_line_mid
COMMENT:This blank line is invalid
X-PREFIXED:Hello
IANA-PREFIXED:World
UNPREFIXED:This should work as well
X-LIST:This
X-LIST:Should
X-LIST:Work
END:VCALENDAR
Loading
Loading