Skip to content

Commit

Permalink
Merge pull request #50 from Saku2/allow-overlapping-interfaces
Browse files Browse the repository at this point in the history
Allow overlapping methods
  • Loading branch information
hexdigest authored May 5, 2022
2 parents 40ff9af + b5b167c commit 2e229bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
24 changes: 9 additions & 15 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,25 +404,19 @@ func processSelector(fs *token.FileSet, currentPackage *packages.Package, se *as
return methods, err
}

var errDuplicateMethod = errors.New("embedded interface has same method")

//mergeMethods merges two methods list, if there is a duplicate method name
//errDuplicateMethod is returned
func mergeMethods(ml1, ml2 methodsList) (methodsList, error) {
if ml1 == nil || ml2 == nil {
return ml1, nil
//mergeMethods merges two methods list. Retains overlapping methods from the
//parent list
func mergeMethods(methods, embeddedMethods methodsList) (methodsList, error) {
if methods == nil || embeddedMethods == nil {
return methods, nil
}

result := make(methodsList, len(ml1)+len(ml2))
for k, v := range ml1 {
result[k] = v
result := make(methodsList, len(methods)+len(embeddedMethods))
for name, signature := range embeddedMethods {
result[name] = signature
}

for name, signature := range ml2 {
if _, ok := ml1[name]; ok {
return nil, errors.Wrap(errDuplicateMethod, name)
}

for name, signature := range methods {
result[name] = signature
}

Expand Down
18 changes: 12 additions & 6 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,24 @@ func Test_mergeMethods(t *testing.T) {
wantErr: false,
},
{
name: "duplicate method",
name: "duplicate methods should return outer method",
args: args{
ml1: methodsList{
"method": Method{},
"method": Method{
Doc: []string{"outer"},
},
},
ml2: methodsList{
"method": Method{},
"method": Method{
Doc: []string{"inner"},
},
},
},
wantErr: true,
inspectErr: func(err error, t *testing.T) {
assert.Equal(t, errDuplicateMethod, errors.Cause(err))
wantErr: false,
want1: methodsList{
"method": {
Doc: []string{"outer"},
},
},
},
{
Expand Down

0 comments on commit 2e229bd

Please sign in to comment.