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 Equal recognition #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 23 additions & 24 deletions generators/deepequal.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,6 @@ func (g *genDeepEqual) Filter(c *generator.Context, t *types.Type) bool {
return true
}

func (g *genDeepEqual) copyableAndInBounds(t *types.Type) bool {
if !comparableType(t) {
return false
}
// Only packages within the restricted range can be processed.
if !isRootedUnder(t.Name.Package, g.boundingDirs) {
return false
}
return true
}

// deepEqualMethod returns the signature of a DeepEqual() method, nil or an error
// if the type is wrong. DeepEqual allows more efficient deep copy
// implementations to be defined by the type's author. The correct signature
Expand All @@ -337,7 +326,7 @@ func deepEqualMethod(t *types.Type) (*types.Signature, error) {
if len(f.Signature.Parameters) != 1 {
return nil, fmt.Errorf("type %v: invalid DeepEqual signature, expected exactly one parameter", t)
}
if len(f.Signature.Results) != 1 {
if len(f.Signature.Results) != 1 || f.Signature.Results[0].Name.Name != "bool" {
return nil, fmt.Errorf("type %v: invalid DeepEqual signature, expected bool result type", t)
}

Expand Down Expand Up @@ -368,18 +357,6 @@ func deepEqualMethodOrDie(t *types.Type) *types.Signature {
return ret
}

func isRootedUnder(pkg string, roots []string) bool {
// Add trailing / to avoid false matches, e.g. foo/bar vs foo/barn. This
// assumes that bounding dirs do not have trailing slashes.
pkg = pkg + "/"
for _, root := range roots {
if strings.HasPrefix(pkg, root+"/") {
return true
}
}
return false
}

func comparableType(t *types.Type) bool {
// If the type opts out of deepequal-generation, stop.
ttag := extractEnabledTypeTag(t)
Expand Down Expand Up @@ -687,6 +664,22 @@ func (g *genDeepEqual) doSlice(t *types.Type, sw *generator.SnippetWriter) {
sw.Do("}\n", nil)
}

func HasEqual(t *types.Type) (hasEqual, pointerParameter bool) {
method := t.Methods["Equal"]
if method == nil {
return
}

signature := method.Signature
results := len(signature.Results) == 1 && signature.Results[0] == types.Bool
parameters := len(signature.Parameters) == 1 && signature.Parameters[0].Name == t.Name
hasEqual = results && parameters
if hasEqual {
pointerParameter = method.Signature.Parameters[0].Kind == types.Pointer
}
return
}

// IsAssignable returns whether the type is deep-assignable. For example,
// slices and maps and pointers are shallow copies, but ints and strings are
// complete.
Expand Down Expand Up @@ -772,6 +765,12 @@ func (g *genDeepEqual) doStruct(t *types.Type, sw *generator.SnippetWriter) {
case uft.Kind == types.Struct:
if IsComparable(uft) {
sw.Do("if in.$.name$ != other.$.name$ {\n", typeArgs)
} else if hasEqual, pointer := HasEqual(uft); hasEqual {
if pointer {
sw.Do("if !in.$.name$.Equal(&other.$.name$) {\n", typeArgs)
} else {
sw.Do("if !in.$.name$.Equal(other.$.name$) {\n", typeArgs)
}
} else {
sw.Do("if !in.$.name$.DeepEqual(&other.$.name$) {\n", typeArgs)
}
Expand Down
Loading