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

Need GetElement method #15

Open
jyotisj opened this issue Jul 4, 2020 · 2 comments
Open

Need GetElement method #15

jyotisj opened this issue Jul 4, 2020 · 2 comments

Comments

@jyotisj
Copy link
Contributor

jyotisj commented Jul 4, 2020

Hi Elliot,

Thank you so much for implementing an awesome module. Your orderedmap module has helped me in two projects.

One of my project's requirement is to get previous and next values when a specific key is provided. Next() and Prev() are implemented for Element. However there is no GetElement method to get a specific Element when a key if provided.

How about adding a GetElement method in orderedmap package?

// GetElement returns the element for a key. If the key does not exist, the
// second return parameter will be false and the pointer will be nil.
func (m *OrderedMap) GetElement(key interface{}) (*Element, bool) {
value, ok := m.kv[key]
if ok {
element := value.Value.(*orderedMapElement)
return &Element{
element: value,
Key: element.key,
Value: element.value,
}, true
}

return nil, false

}

I am currently writing tests for the GetElement method. Thank you for writing detailed tests for the Get method. I'm following those to write tests for GetElement.

If you are fine with adding GetElement method then I'll create a pull request.

Thank you,
Jyoti

@elliotchance
Copy link
Owner

Hi @jyotisj, I had to think about it for a bit. My gut tells me that this may cause unexpected behaviour if you start copying the element outside of its iterator implementation (in case it changes). However, this library gives no guarantees on concurrent use, so it should be OK.

@jyotisj
Copy link
Contributor Author

jyotisj commented Jul 5, 2020

Thanks, Elliot. I'll submit the pull request shortly.

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

No branches or pull requests

2 participants