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

Add method to remove property #96

Merged
merged 1 commit into from
Sep 28, 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
12 changes: 12 additions & 0 deletions components.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ func (cb *ComponentBase) AddProperty(property ComponentProperty, value string, p
cb.Properties = append(cb.Properties, r)
}

// RemoveProperty removes from the component all properties that has
// the name passed in removeProp.
func (cb *ComponentBase) RemoveProperty(removeProp ComponentProperty) {
var keptProperties []IANAProperty
for i := range cb.Properties {
if cb.Properties[i].IANAToken != string(removeProp) {
keptProperties = append(keptProperties, cb.Properties[i])
Copy link
Owner

@arran4 arran4 Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appending here we effectively make a new array, I think we should do an inplace remove. Completely unverified by me. Using AI I got:

	i := 0
	for _, prop := range cb.Properties {
		if prop.IANAToken != string(removeProp) {
			cb.Properties[i] = prop
			i++
		}
	}
	cb.Properties = cb.Properties[:i]

Probably a good idea to write a test for that too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really prefer readability over premature optimization. For the above in-place attempt, I really have to think very carefully about its workings, and whether it might have some edge cases or such. My code is really straightforward to read and understand. Is making a new array really an issue performance wise here?

Copy link
Owner

@arran4 arran4 Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't a core concern of the library, as I found another area where I preference memory over performance.

I guess the real issue here is if people are using the slice directly. For simplicity I guess it's better to go with yours for now.

However there are lint issues remaining if you resync it should help.

One thing your version will do is put a nil slice instead of an empty slice. Which is different, but I don't know if it's a concern:

https://go.dev/play/p/iRgj5Jsqe8O

--

Looks like it really only matters for json and other reflection like matters. So non-issue.

}
}
cb.Properties = keptProperties
}

const (
icalTimestampFormatUtc = "20060102T150405Z"
icalTimestampFormatLocal = "20060102T150405"
Expand Down
59 changes: 24 additions & 35 deletions components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,44 +133,33 @@ func TestSetMailtoPrefix(t *testing.T) {
}
}

func TestGetStartAt(t *testing.T) {
ref := time.Now().UTC().Truncate(time.Second)
for name, test := range map[string]struct {
rawDTStart string
assertError func(assert.TestingT, error, ...interface{}) bool
expected time.Time
func TestRemoveProperty(t *testing.T) {
testCases := []struct {
name string
output string
}{
"valid_time": {
rawDTStart: ref.Format(icalTimestampFormatUtc),
assertError: assert.NoError,
expected: ref,
},
"no_time": {
assertError: errorIs(ErrorPropertyNotFound),
expected: time.Time{},
},
"invalid_time": {
rawDTStart: "invalid",
assertError: assert.Error,
expected: time.Time{},
{
name: "test RemoveProperty - start",
output: `BEGIN:VTODO
UID:test-removeproperty
X-TEST:42
END:VTODO
`,
},
} {
t.Run(name, func(t *testing.T) {
test := test
t.Parallel()
cb := ComponentBase{}
if test.rawDTStart != "" {
cb.SetProperty(ComponentPropertyDtStart, test.rawDTStart)
}
actual, err := cb.GetStartAt()
test.assertError(t, err)
assert.Equal(t, test.expected, actual)
})
}
}

func errorIs(target error) func(assert.TestingT, error, ...interface{}) bool {
return func(t assert.TestingT, err error, msgAndArgs ...interface{}) bool {
return assert.ErrorIs(t, err, target, msgAndArgs...)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := NewTodo("test-removeproperty")
e.AddProperty("X-TEST", "42")
e.AddProperty("X-TESTREMOVE", "FOO")
e.AddProperty("X-TESTREMOVE", "BAR")
e.RemoveProperty("X-TESTREMOVE")

// adjust to expected linebreaks, since we're not testing the encoding
text := strings.Replace(e.Serialize(), "\r\n", "\n", -1)

assert.Equal(t, tc.output, text)
})
}
}