-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deprecated reflect mode has been replaced with package mode (#207)
It is impossible to create an mock for a generic interface via reflect mode, because it is impossible to compile a generic type without instantiation. This PR replaces the reflect mod for parsing using go/types. All exists mocks have been regenerated and the tests have been passed. But since this radically changes the behavior of reflect mode, I would be grateful if there are those who want to add additional test cases that I did not provide. We can also come up with another name instead of import mode. benefits: generation mocks for generic interfaces generation mocks for aliases to interfaces correct names for method arguments resolves #175 resolves #197 resolves #128 --------- Co-authored-by: Jacob Oaks <joaks@uber.com>
- Loading branch information
Showing
30 changed files
with
3,749 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
) | ||
|
||
const ( | ||
deprecatedFlagProgOnly = "prog_only" | ||
deprecatedFlagExecOnly = "exec_only" | ||
) | ||
|
||
var ( | ||
_ = flag.Bool("prog_only", false, "DEPRECATED (reflect mode) Only generate the reflection program; write it to stdout and exit.") | ||
_ = flag.String("exec_only", "", "DEPRECATED (reflect mode) If set, execute this reflection program.") | ||
) | ||
|
||
// notifyAboutDeprecatedFlags prints a warning message for a deprecated flags if they are set. | ||
func notifyAboutDeprecatedFlags() { | ||
const resetColorPostfix = "\033[0m" | ||
logger := initWarningLogger() | ||
|
||
flag.Visit(func(f *flag.Flag) { | ||
switch f.Name { | ||
case deprecatedFlagProgOnly: | ||
logger.Println("The -prog_only flag is deprecated and has no effect.", resetColorPostfix) | ||
case deprecatedFlagExecOnly: | ||
logger.Println("The -exec_only flag is deprecated and has no effect.", resetColorPostfix) | ||
} | ||
}) | ||
} | ||
|
||
func initWarningLogger() *log.Logger { | ||
const ( | ||
yellowColor = "\033[33m" | ||
warningPrefix = yellowColor + "WARNING: " | ||
) | ||
|
||
return log.New(os.Stdout, warningPrefix, log.Ldate|log.Ltime) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package build_flags | ||
|
||
// one build flag | ||
//go:generate mockgen -destination=mock1/interfaces_mock.go -build_flags=-tags=tag1 . Interface | ||
// multiple build flags | ||
//go:generate mockgen -destination=mock2/interfaces_mock.go "-build_flags=-race -tags=tag2" . Interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build tag1 | ||
|
||
package build_flags | ||
|
||
type Interface interface { | ||
HelloWorld() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build tag2 | ||
|
||
package build_flags | ||
|
||
type Interface interface { | ||
Foo() | ||
} |
54 changes: 54 additions & 0 deletions
54
mockgen/internal/tests/build_flags/mock1/interfaces_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
mockgen/internal/tests/build_flags/mock2/interfaces_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.