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

Inlined interfaces #531

Closed
nikolaydubina opened this issue Jan 21, 2023 · 5 comments
Closed

Inlined interfaces #531

nikolaydubina opened this issue Jan 21, 2023 · 5 comments

Comments

@nikolaydubina
Copy link

nikolaydubina commented Jan 21, 2023

Description

Purpose

Avoid declaring non exported upstream interfaces as separate types just so they can be mocked.

Details

https://go.dev/play/p/Bt74sBt01Yl

package main

import "fmt"

// go generate mockery Employee.address

// struct with interface
type Employee struct {
	address interface {
		StreetName() string
		BlockNumber() int
	}
}

// constructor with inlined interface
func NewEmployee(
	address interface {
		StreetName() string
		BlockNumber() int
	},
) Employee {
	return Employee{address: address}
}

// concerete upstream
type HumanAddress struct {
	streetName  string
	blockNumber int
}

func (h HumanAddress) StreetName() string { return h.streetName }

func (h HumanAddress) BlockNumber() int { return h.blockNumber }

func main() {
	e := NewEmployee(HumanAddress{streetName: "some-street", blockNumber: 11})
	fmt.Printf("%#v", e)
}
@nikolaydubina
Copy link
Author

I think I can implement this, if no takers!

@LandonTClipp
Copy link
Contributor

Can you create a proposal of how this would be implemented? Mockery would need to generate some named mock implementation of the inlined interface. I fear whatever implementation you have is going to be kind of messy, but I'm open to hear what you think.

@nikolaydubina
Copy link
Author

nikolaydubina commented Feb 4, 2023 via email

@LandonTClipp
Copy link
Contributor

The problem is moreso that mockery searches a package's global scope for named interface types. If an interface definition lies inline a function signature, mockery would then need to also inspect the function signature of every top-level function. It would then need to also assign a name to that interface. It's not an impossible task, but it's not as simple as you might think.

@LandonTClipp
Copy link
Contributor

Going to close this as we won't be implementing it.

@LandonTClipp LandonTClipp closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2023
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