diff --git a/calendar.go b/calendar.go index e7f6ca0..c17aae5 100644 --- a/calendar.go +++ b/calendar.go @@ -220,7 +220,7 @@ const ( ) func (ps ObjectStatus) KeyValue(s ...interface{}) (string, []string) { - return string(PropertyStatus), []string{ToText(string(ps))} + return string(PropertyStatus), []string{string(ps)} } type RelationshipType string @@ -317,48 +317,48 @@ func (calendar *Calendar) SerializeTo(w io.Writer) error { } func (calendar *Calendar) SetMethod(method Method, props ...PropertyParameter) { - calendar.setProperty(PropertyMethod, ToText(string(method)), props...) + calendar.setProperty(PropertyMethod, string(method), props...) } func (calendar *Calendar) SetXPublishedTTL(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyXPublishedTTL, string(s), props...) + calendar.setProperty(PropertyXPublishedTTL, s, props...) } func (calendar *Calendar) SetVersion(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyVersion, ToText(s), props...) + calendar.setProperty(PropertyVersion, s, props...) } func (calendar *Calendar) SetProductId(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyProductId, ToText(s), props...) + calendar.setProperty(PropertyProductId, s, props...) } func (calendar *Calendar) SetName(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyName, string(s), props...) - calendar.setProperty(PropertyXWRCalName, string(s), props...) + calendar.setProperty(PropertyName, s, props...) + calendar.setProperty(PropertyXWRCalName, s, props...) } func (calendar *Calendar) SetColor(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyColor, string(s), props...) + calendar.setProperty(PropertyColor, s, props...) } func (calendar *Calendar) SetXWRCalName(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyXWRCalName, string(s), props...) + calendar.setProperty(PropertyXWRCalName, s, props...) } func (calendar *Calendar) SetXWRCalDesc(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyXWRCalDesc, string(s), props...) + calendar.setProperty(PropertyXWRCalDesc, s, props...) } func (calendar *Calendar) SetXWRTimezone(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyXWRTimezone, string(s), props...) + calendar.setProperty(PropertyXWRTimezone, s, props...) } func (calendar *Calendar) SetXWRCalID(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyXWRCalID, string(s), props...) + calendar.setProperty(PropertyXWRCalID, s, props...) } func (calendar *Calendar) SetDescription(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyDescription, ToText(s), props...) + calendar.setProperty(PropertyDescription, s, props...) } func (calendar *Calendar) SetLastModified(t time.Time, props ...PropertyParameter) { @@ -366,23 +366,23 @@ func (calendar *Calendar) SetLastModified(t time.Time, props ...PropertyParamete } func (calendar *Calendar) SetRefreshInterval(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyRefreshInterval, string(s), props...) + calendar.setProperty(PropertyRefreshInterval, s, props...) } func (calendar *Calendar) SetCalscale(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyCalscale, string(s), props...) + calendar.setProperty(PropertyCalscale, s, props...) } func (calendar *Calendar) SetUrl(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyUrl, string(s), props...) + calendar.setProperty(PropertyUrl, s, props...) } func (calendar *Calendar) SetTzid(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyTzid, string(s), props...) + calendar.setProperty(PropertyTzid, s, props...) } func (calendar *Calendar) SetTimezoneId(s string, props ...PropertyParameter) { - calendar.setProperty(PropertyTimezoneId, string(s), props...) + calendar.setProperty(PropertyTimezoneId, s, props...) } func (calendar *Calendar) setProperty(property Property, value string, props ...PropertyParameter) { diff --git a/components.go b/components.go index bf30b04..4b5ccbb 100644 --- a/components.go +++ b/components.go @@ -45,7 +45,7 @@ func (cb ComponentBase) serializeThis(writer io.Writer, componentType string) { func NewComponent(uniqueId string) ComponentBase { return ComponentBase{ Properties: []IANAProperty{ - {BaseProperty{IANAToken: ToText(string(ComponentPropertyUniqueId)), Value: uniqueId}}, + {BaseProperty{IANAToken: string(ComponentPropertyUniqueId), Value: uniqueId}}, }, } } @@ -214,19 +214,19 @@ func (cb *ComponentBase) GetDtStampTime() (time.Time, error) { } func (cb *ComponentBase) SetSummary(s string, props ...PropertyParameter) { - cb.SetProperty(ComponentPropertySummary, ToText(s), props...) + cb.SetProperty(ComponentPropertySummary, s, props...) } func (cb *ComponentBase) SetStatus(s ObjectStatus, props ...PropertyParameter) { - cb.SetProperty(ComponentPropertyStatus, ToText(string(s)), props...) + cb.SetProperty(ComponentPropertyStatus, string(s), props...) } func (cb *ComponentBase) SetDescription(s string, props ...PropertyParameter) { - cb.SetProperty(ComponentPropertyDescription, ToText(s), props...) + cb.SetProperty(ComponentPropertyDescription, s, props...) } func (cb *ComponentBase) SetLocation(s string, props ...PropertyParameter) { - cb.SetProperty(ComponentPropertyLocation, ToText(s), props...) + cb.SetProperty(ComponentPropertyLocation, s, props...) } func (cb *ComponentBase) setGeo(lat interface{}, lng interface{}, props ...PropertyParameter) { @@ -729,7 +729,7 @@ func NewTimezone(tzId string) *VTimezone { e := &VTimezone{ ComponentBase{ Properties: []IANAProperty{ - {BaseProperty{IANAToken: ToText(string(ComponentPropertyTzid)), Value: tzId}}, + {BaseProperty{IANAToken: string(ComponentPropertyTzid), Value: tzId}}, }, }, } diff --git a/property.go b/property.go index 388160b..95360ae 100644 --- a/property.go +++ b/property.go @@ -88,6 +88,121 @@ func trimUT8StringUpTo(maxLength int, s string) string { return s[:length] } +func (p *BaseProperty) GetValueType() ValueDataType { + for k, v := range p.ICalParameters { + if Parameter(k) == ParameterValue && len(v) == 1 { + return ValueDataType(v[0]) + } + } + + // defaults from spec if unspecified + switch Property(p.IANAToken) { + case PropertyCalscale: + fallthrough + case PropertyMethod: + fallthrough + case PropertyProductId: + fallthrough + case PropertyVersion: + fallthrough + case PropertyCategories: + fallthrough + case PropertyClass: + fallthrough + case PropertyComment: + fallthrough + case PropertyDescription: + fallthrough + case PropertyLocation: + fallthrough + case PropertyResources: + fallthrough + case PropertyStatus: + fallthrough + case PropertySummary: + fallthrough + case PropertyTransp: + fallthrough + case PropertyTzid: + fallthrough + case PropertyTzname: + fallthrough + case PropertyContact: + fallthrough + case PropertyRelatedTo: + fallthrough + case PropertyUid: + fallthrough + case PropertyAction: + fallthrough + default: + fallthrough + case PropertyRequestStatus: + return ValueDataTypeText + + case PropertyAttach: + fallthrough + case PropertyTzurl: + fallthrough + case PropertyUrl: + return ValueDataTypeUri + + case PropertyGeo: + return ValueDataTypeFloat + + case PropertyPercentComplete: + fallthrough + case PropertyPriority: + fallthrough + case PropertyRepeat: + fallthrough + case PropertySequence: + return ValueDataTypeInteger + + case PropertyCompleted: + fallthrough + case PropertyDtend: + fallthrough + case PropertyDue: + fallthrough + case PropertyDtstart: + fallthrough + case PropertyRecurrenceId: + fallthrough + case PropertyExdate: + fallthrough + case PropertyRdate: + fallthrough + case PropertyCreated: + fallthrough + case PropertyDtstamp: + fallthrough + case PropertyLastModified: + return ValueDataTypeDateTime + + case PropertyDuration: + fallthrough + case PropertyTrigger: + return ValueDataTypeDuration + + case PropertyFreebusy: + return ValueDataTypePeriod + + case PropertyTzoffsetfrom: + fallthrough + case PropertyTzoffsetto: + return ValueDataTypeUtcOffset + + case PropertyAttendee: + fallthrough + case PropertyOrganizer: + return ValueDataTypeCalAddress + + case PropertyRrule: + return ValueDataTypeRecur + } +} + func (property *BaseProperty) serialize(w io.Writer) { b := bytes.NewBufferString("") fmt.Fprint(b, property.IANAToken) @@ -117,7 +232,11 @@ func (property *BaseProperty) serialize(w io.Writer) { } } fmt.Fprint(b, ":") - fmt.Fprint(b, property.Value) + propertyValue := property.Value + if property.GetValueType() == ValueDataTypeText { + propertyValue = ToText(propertyValue) + } + fmt.Fprint(b, propertyValue) r := b.String() if len(r) > 75 { l := trimUT8StringUpTo(75, r) @@ -306,7 +425,10 @@ func parsePropertyValue(r *BaseProperty, contentLine string, p int) *BasePropert if tokenPos == nil { return nil } - r.Value = string(contentLine[p : p+tokenPos[1]]) + r.Value = contentLine[p : p+tokenPos[1]] + if r.GetValueType() == ValueDataTypeText { + r.Value = FromText(r.Value) + } return r } diff --git a/testdata/serialization/expected/input4.ics b/testdata/serialization/expected/input4.ics index e8a51ec..5dc38ea 100644 --- a/testdata/serialization/expected/input4.ics +++ b/testdata/serialization/expected/input4.ics @@ -7,7 +7,7 @@ UID:uid5@example.com ORGANIZER:mailto:jsmith@example.com STATUS:DRAFT CLASS:PUBLIC -CATEGORIES:Project Report,XYZ,Weekly Meeting +CATEGORIES:Project Report\,XYZ\,Weekly Meeting DESCRIPTION:Project xyz Review Meeting Minutes\nAgenda\n1. Review of project version 1.0 requirements.\n2. Definitionof project processes.\n3. Review of project schedule.\nParticipants: John Smith\, Jane Doe\, Jim diff --git a/testdata/serialization/expected/input5.ics b/testdata/serialization/expected/input5.ics index e8a51ec..5dc38ea 100644 --- a/testdata/serialization/expected/input5.ics +++ b/testdata/serialization/expected/input5.ics @@ -7,7 +7,7 @@ UID:uid5@example.com ORGANIZER:mailto:jsmith@example.com STATUS:DRAFT CLASS:PUBLIC -CATEGORIES:Project Report,XYZ,Weekly Meeting +CATEGORIES:Project Report\,XYZ\,Weekly Meeting DESCRIPTION:Project xyz Review Meeting Minutes\nAgenda\n1. Review of project version 1.0 requirements.\n2. Definitionof project processes.\n3. Review of project schedule.\nParticipants: John Smith\, Jane Doe\, Jim