cmd/modelcmd: return ModelCommand from Wrap #7209

Merged
merged 1 commit into from Apr 7, 2017
Jump to file or symbol
Failed to load files and symbols.
+17 −3
Split
@@ -254,11 +254,11 @@ func wrapSkipDefaultModel(w *modelCommandWrapper) {
w.useDefaultModel = false
}
-// Wrap wraps the specified ModelCommand, returning a Command
+// Wrap wraps the specified ModelCommand, returning a ModelCommand
// that proxies to each of the ModelCommand methods.
// Any provided options are applied to the wrapped command
// before it is returned.
-func Wrap(c ModelCommand, options ...WrapOption) cmd.Command {
+func Wrap(c ModelCommand, options ...WrapOption) ModelCommand {
wrapper := &modelCommandWrapper{
ModelCommand: c,
skipModelFlags: false,
@@ -267,7 +267,21 @@ func Wrap(c ModelCommand, options ...WrapOption) cmd.Command {
for _, option := range options {
option(wrapper)
}
- return WrapBase(wrapper)
+ // Define a new type so that we can embed the ModelCommand
+ // interface one level deeper than cmd.Command, so that
+ // we'll get the Command methods from WrapBase
+ // and all the ModelCommand methods not in cmd.Command
+ // from modelCommandWrapper.
@mhilton

mhilton Apr 6, 2017

Member

what's modelCommandWrapper?

@rogpeppe

rogpeppe Apr 6, 2017

Owner

It's the type we're using above that wraps c.

@howbazaar

howbazaar Apr 6, 2017

Owner

Seems a little magical, but the comment helps.

+ type embed struct {
+ ModelCommand
+ }
+ return struct {
+ embed
+ cmd.Command
+ }{
+ Command: WrapBase(wrapper),
+ embed: embed{wrapper},
+ }
}
type modelCommandWrapper struct {