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

Conversation

quite
Copy link
Contributor

@quite quite commented Aug 22, 2024

Fixes #95

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.

@arran4
Copy link
Owner

arran4 commented Aug 22, 2024

Thanks. I have reviewed. The fix might solve the lint issue too.

@quite
Copy link
Contributor Author

quite commented Aug 22, 2024

lint

The lint issue is probably caused by the github action is not locked to a specific version of golangci-lint. So whenever things change in new version, the next run of the ci might trigger some lint errors that were not errors before. So we get unrelated lint complaints on PRs, like here.

I'd suggest locking golangci-lint to a specific version, and fixing the lints on main. Then yes, bumping the linter will have to become a regular chore, but at least PRs won't be hit by random new lint rules.

@quite
Copy link
Contributor Author

quite commented Aug 23, 2024

Added test though

@arran4
Copy link
Owner

arran4 commented Sep 28, 2024

@quite I've merged a PR that conflicts with components_test.go would be nice of github to provide warnings for that IMHO. If you resync it should help with lint resolution depending on what's going on

arran4
arran4 previously approved these changes Sep 28, 2024
Copy link
Owner

@arran4 arran4 left a comment

Choose a reason for hiding this comment

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

Will have to reapprove once the lint issue is resolved. Comments applied

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 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.

@quite
Copy link
Contributor Author

quite commented Sep 28, 2024

rebased, i think lint issues should be done

@arran4
Copy link
Owner

arran4 commented Sep 28, 2024

When do you need this released? Soon, or you can wait for the #73 ?

@arran4 arran4 merged commit f364b80 into arran4:master Sep 28, 2024
10 checks passed
@quite
Copy link
Contributor Author

quite commented Sep 28, 2024 via email

@quite
Copy link
Contributor Author

quite commented Sep 28, 2024 via email

@arran4
Copy link
Owner

arran4 commented Sep 28, 2024

You mean it will return a nil slice if the last prop is removed right? I think that is fine, since in practice there is no difference between a nil slice and an empty slice. Go styleguide also: https://google.github.io/styleguide/go/decisions#nil-slices

On 28 September 2024 09:03:19 CEST, Arran Ubels @.***> wrote: @arran4 approved this pull request. Will have to reapprove once the lint issue is resolved. Comments applied > @@ -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]) 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

Yeah I had to verify that. It's maps which you aren't so lucky, kinda wished they were both one way or the other.

@quite quite deleted the removeproperty branch September 28, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Method to remove property from component
2 participants