Skip to content

Commit

Permalink
move ToText to Serialize logic
Browse files Browse the repository at this point in the history
This is a breaking change. Property.Value of value-type: TEXT is now unescaped in the deserialized model. Previously, this was escaped.
  • Loading branch information
brenank committed May 20, 2024
1 parent 22cb4bc commit 0a707b2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 28 deletions.
36 changes: 18 additions & 18 deletions calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -317,72 +317,72 @@ 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) {
calendar.setProperty(PropertyLastModified, t.UTC().Format(icalTimestampFormatUtc), props...)
}

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) {
Expand Down
12 changes: 6 additions & 6 deletions components.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
},
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}},
},
},
}
Expand Down
126 changes: 124 additions & 2 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/serialization/expected/input4.ics
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ UID:[email protected]
ORGANIZER:mailto:[email protected]
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
Expand Down
2 changes: 1 addition & 1 deletion testdata/serialization/expected/input5.ics
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ UID:[email protected]
ORGANIZER:mailto:[email protected]
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
Expand Down

0 comments on commit 0a707b2

Please sign in to comment.