awalterschulze / goderive

Derives and generates mundane golang functions that you do not want to maintain yourself
Apache License 2.0
1.23k stars 44 forks source link

deriveEqual: Use Equal() if it is defined on T #44

Closed gpaul closed 6 years ago

gpaul commented 6 years ago

I would like to generate deriveEqual for the following type hierarchy:

type Foo struct {
    Field Bar
}

func (this *Foo) Equal(that *Foo) bool {
    return deriveEqual(this, that)
}

type Bar map[string]interface{}

Now, as it is not possible to derive an Equal for an interface{} this fails. However, I would like to implement func (Bar) Equal(other Bar) bool {} myself and have deriveEqual notice that and call it instead.

That way I can take responsibility for the fact that I end up calling reflect.DeepEqual() or implementing custom Equal logic lower down my type hierarchy while still benefiting from all the lovely code generation for the rest of it.

awalterschulze commented 6 years ago

goderive does some check for equal methods on structs, but maybe goderive could do checks on other types as well. I'll look into making sure this works.

awalterschulze commented 6 years ago

Should work now https://github.com/awalterschulze/goderive/commit/77a50e43e09d55ef12d439668ef3712369272bde

gpaul commented 6 years ago

Wow that was fast, thank you! Works like a charm.

awalterschulze commented 6 years ago

My pleasure :) It was an easy fix.

If you are using it in some project, we would love to add that project to your users list, if possible?

gpaul commented 6 years ago

Sigh for the moment it is closed source, sorry. We plan on open sourcing part of it in the next few months though.

awalterschulze commented 6 years ago

No problem. Just trying my luck :)

gpaul commented 6 years ago

If you're happy with just "used by XXX" then I'll let you know once we release the closed source version, right now the project is in early development.

awalterschulze commented 6 years ago

Also happy to add used by XXX with a link to the closed source project.

sakeven commented 1 year ago

@awalterschulze Hi, I think we should use Equal() before using “==” if Equal is defined for T. This is because if T changes, we can’t use “==” and it may only be equal through Equal().

awalterschulze commented 1 year ago

Would you like to create a new issue with a more elaborate description, I don't think I fully understand