-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New processors add/delete Property
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package processors | ||
|
||
import ( | ||
"github.com/akamensky/argparse" | ||
"github.com/emersion/go-ical" | ||
"strings" | ||
) | ||
|
||
type AddPropertyProcessor struct { | ||
propertyName *string | ||
propertyValue *string | ||
overwrite *bool | ||
toolbox Toolbox | ||
} | ||
|
||
func (a *AddPropertyProcessor) Initialize(parser *argparse.Parser) (*argparse.Command, error) { | ||
c := parser.NewCommand("addProperty", "Adds a new property to each selected event") | ||
a.propertyName = c.String("N", "name", &argparse.Options{ | ||
Help: "Name of the new property", | ||
Required: true, | ||
}) | ||
a.propertyValue = c.String("V", "value", &argparse.Options{ | ||
Help: "Value of the new property (only text values allowed)", | ||
Required: true, | ||
}) | ||
a.overwrite = c.Flag("O", "overwrite", &argparse.Options{ | ||
Help: "Overwrite property if it exists", | ||
Required: false, | ||
Default: true, | ||
}) | ||
return c, nil | ||
} | ||
|
||
func (a *AddPropertyProcessor) SetToolbox(toolbox Toolbox) { | ||
a.toolbox = toolbox | ||
} | ||
|
||
func (a *AddPropertyProcessor) Process(input ical.Calendar, output *ical.Calendar) error { | ||
n := strings.ToUpper(*a.propertyName) | ||
for _, event := range input.Events() { | ||
if a.toolbox.EventMatchesSelector(event) { | ||
if event.Props.Get(n) != nil && *a.overwrite { | ||
event.Props.Del(n) | ||
} | ||
if event.Props.Get(n) == nil { | ||
event.Props.SetText(n, *a.propertyValue) | ||
} | ||
} | ||
output.Children = append(output.Children, event.Component) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package processors | ||
|
||
import ( | ||
"github.com/emersion/go-ical" | ||
"github.com/stretchr/testify/assert" | ||
"icarus/internal" | ||
"testing" | ||
) | ||
|
||
func TestAddPropertyProcessor_Process(t *testing.T) { | ||
event1 := ical.NewEvent() | ||
event1.Props.SetText(ical.PropSummary, "test") | ||
input := ical.NewCalendar() | ||
input.Children = append(input.Children, event1.Component) | ||
subject := AddPropertyProcessor{} | ||
subject.SetToolbox(NewToolbox()) | ||
subject.propertyName = internal.StringAddr("X-TEST") | ||
subject.propertyValue = internal.StringAddr("test") | ||
subject.overwrite = internal.BoolAddr(false) | ||
output := ical.NewCalendar() | ||
err := subject.Process(*input, output) | ||
assert.NoError(t, err, "Process got an error") | ||
assert.Len(t, output.Children, 1, "Invalid number of events") | ||
assert.NotNil(t, output.Children[0].Props.Get("X-TEST"), "Test property does not exists") | ||
assert.Equal(t, "test", output.Children[0].Props.Get("X-TEST").Value, "Test property has wrong value") | ||
} | ||
|
||
func TestAddPropertyProcessor_NotOverwrite(t *testing.T) { | ||
event1 := ical.NewEvent() | ||
event1.Props.SetText(ical.PropSummary, "test") | ||
event1.Props.SetText("X-TEST", "test") | ||
input := ical.NewCalendar() | ||
input.Children = append(input.Children, event1.Component) | ||
subject := AddPropertyProcessor{} | ||
subject.SetToolbox(NewToolbox()) | ||
subject.propertyName = internal.StringAddr("X-TEST") | ||
subject.propertyValue = internal.StringAddr("test2") | ||
subject.overwrite = internal.BoolAddr(false) | ||
output := ical.NewCalendar() | ||
err := subject.Process(*input, output) | ||
assert.NoError(t, err, "Process got an error") | ||
assert.Len(t, output.Children, 1, "Invalid number of events") | ||
assert.NotNil(t, output.Children[0].Props.Get("X-TEST"), "Test property does not exists") | ||
assert.Equal(t, "test", output.Children[0].Props.Get("X-TEST").Value, "Test property has wrong value") | ||
} | ||
|
||
func TestAddPropertyProcessor_Overwrite(t *testing.T) { | ||
event1 := ical.NewEvent() | ||
event1.Props.SetText(ical.PropSummary, "test") | ||
event1.Props.SetText("X-TEST", "test") | ||
input := ical.NewCalendar() | ||
input.Children = append(input.Children, event1.Component) | ||
subject := AddPropertyProcessor{} | ||
subject.SetToolbox(NewToolbox()) | ||
subject.propertyName = internal.StringAddr("X-TEST") | ||
subject.propertyValue = internal.StringAddr("test2") | ||
subject.overwrite = internal.BoolAddr(true) | ||
output := ical.NewCalendar() | ||
err := subject.Process(*input, output) | ||
assert.NoError(t, err, "Process got an error") | ||
assert.Len(t, output.Children, 1, "Invalid number of events") | ||
assert.NotNil(t, output.Children[0].Props.Get("X-TEST"), "Test property does not exists") | ||
assert.Equal(t, "test2", output.Children[0].Props.Get("X-TEST").Value, "Test property has wrong value") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package processors | ||
|
||
import ( | ||
"github.com/akamensky/argparse" | ||
"github.com/emersion/go-ical" | ||
"strings" | ||
) | ||
|
||
type DeletePropertyProcessor struct { | ||
propertyName *string | ||
toolbox Toolbox | ||
} | ||
|
||
func (d *DeletePropertyProcessor) Initialize(parser *argparse.Parser) (*argparse.Command, error) { | ||
c := parser.NewCommand("deleteProperty", "Deletes a property from all selected events") | ||
d.propertyName = c.String("P", "property", &argparse.Options{ | ||
Help: "Property to delete (e.g. X-SPECIAL-PROP, CATEGORIES)", | ||
Required: true, | ||
}) | ||
return c, nil | ||
} | ||
|
||
func (d *DeletePropertyProcessor) SetToolbox(toolbox Toolbox) { | ||
d.toolbox = toolbox | ||
} | ||
|
||
func (d *DeletePropertyProcessor) Process(input ical.Calendar, output *ical.Calendar) error { | ||
for _, event := range input.Events() { | ||
if d.toolbox.EventMatchesSelector(event) { | ||
if event.Props.Get(strings.ToUpper(*d.propertyName)) != nil { | ||
event.Props.Del(strings.ToUpper(*d.propertyName)) | ||
} | ||
} | ||
output.Children = append(output.Children, event.Component) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package processors | ||
|
||
import ( | ||
"github.com/emersion/go-ical" | ||
"github.com/stretchr/testify/assert" | ||
"icarus/internal" | ||
"testing" | ||
) | ||
|
||
func TestDeleteFieldProcessor_Process(t *testing.T) { | ||
event1 := ical.NewEvent() | ||
event1.Props.SetText(ical.PropSummary, "test") | ||
event1.Props.SetText("X-TEST", "test") | ||
assert.NotNil(t, event1.Props.Get("X-TEST"), "Test property was not set") | ||
input := ical.NewCalendar() | ||
input.Children = append(input.Children, event1.Component) | ||
subject := DeletePropertyProcessor{} | ||
subject.SetToolbox(NewToolbox()) | ||
subject.propertyName = internal.StringAddr("X-TEST") | ||
output := ical.NewCalendar() | ||
err := subject.Process(*input, output) | ||
assert.NoError(t, err, "Process got an error") | ||
assert.Len(t, output.Children, 1, "Invalid number of events") | ||
assert.Nil(t, output.Children[0].Props.Get("X-TEST"), "Test property still exists") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters