I have those 2 almost exact functions :
Number 1:
func mergeSlicesOfRequestObjects(aSliceDestination *[]RequestObject, aSliceSource []RequestObject) {
for _, oSliceSourceItem := range aSliceSource {
// Get current key
iIndexSourceItemFound := -1
for iIndexAttribute, oSliceDestinationItem := range *aSliceDestination {
if oSliceSourceItem.Key == oSliceDestinationItem.Key {
iIndexSourceItemFound = iIndexAttribute
break
}
}
// Update attribute
if iIndexSourceItemFound == -1 {
*aSliceDestination = append(*aSliceDestination, oSliceSourceItem)
}
}
}
Number 2:
func mergeSlicesOfResponseObjects(aSliceDestination *[]ResponseObject, aSliceSource []ResponseObject) {
for _, oSliceSourceItem := range aSliceSource {
// Get current key
iIndexSourceItemFound := -1
for iIndexAttribute, oSliceDestinationItem := range *aSliceDestination {
if oSliceSourceItem.Key == oSliceDestinationItem.Key {
iIndexSourceItemFound = iIndexAttribute
break
}
}
// Update attribute
if iIndexSourceItemFound == -1 {
*aSliceDestination = append(*aSliceDestination, oSliceSourceItem)
}
}
}
As you can see the only difference is the struct type of the arguments of the functions.
So here is my question : is there any way to merge those 2 functions into one ?
I've tried using interfaces but I can't figure it out...
Thanks for all the answer in advance :)
Cheers
EDIT
I've implemented thwd answer but I'm getting errors. Here's what I've done:
type Request struct {
Headers []RequestObject
}
type RequestObject {
Key string
}
type Keyer interface {
GetKey() string
}
func (oRequestObject RequestObject) GetKey() string {
return oRequestObject.Key
}
func mergeKeyers(aDst *[]Keyer, aSrc []Keyer) {
// Logic
}
func test() {
// rDst and rSrc are Request struct
mergeKeyers(&rDst.Headers, rSrc.Headers)
}
And I'm getting the following errors when executing test():
cannot use &rDst.Headers (type *[]RequestObject) as type *[]Keyer in argument to mergeKeyers
cannot use rSrc.Headers (type []RequestObject) as type []Keyer in argument to mergeKeyers
Any idea why ?
Define an interface:
type Keyer interface {
Key() int // or whatever type the Key field has
}
Then implement the interface on both types:
func (r RequestObject) Key() int {
return r.Key
}
func (r ResponseObject) Key() int {
return r.Key
}
And rewrite your function to take this interface (and not use hungarian notation and endlessly long variable names):
func mergeKeyers(dst *[]Keyer, src []Keyer) {
for _, s := range src {
f := -1
for i, d := range *dst {
if s.Key() == d.Key() {
f = i
break
}
}
if f == -1 {
*dst = append(*dst, s)
}
}
}
Also, take into account Dave C's comment:
you have an O(n×m) algorithm where you could use a O(n+m) one.
I'll leave it up to you to optimize your code in that way.
Edit to address your second question:
The types *[]RequestObject
and *[]Keyer
are distinct and not interchangeable. What you need to do is convert your slice of RequestObjects to a slice of Keyers.
This is as easy as iterating over the []RequestObject
and assigning each entry to a new entry in a value of type []Keyer
.
See also these answers: