andrey1s
4 years ago
5 changed files with 56 additions and 47 deletions
@ -0,0 +1,46 @@ |
|||
package input |
|||
|
|||
import ( |
|||
"context" |
|||
"log" |
|||
|
|||
"gitoa.ru/go-4devs/console/input/value" |
|||
) |
|||
|
|||
func Chain(c ...Input) Input { |
|||
return chain(c) |
|||
} |
|||
|
|||
type chain []Input |
|||
|
|||
func (c chain) Option(ctx context.Context, name string) value.Value { |
|||
log.Println(name, len(c)) |
|||
for _, in := range c { |
|||
log.Printf("%T\n", in) |
|||
if val := in.Option(ctx, name); !value.IsEmpty(val) { |
|||
return val |
|||
} |
|||
} |
|||
|
|||
return value.Empty |
|||
} |
|||
|
|||
func (c chain) Argument(ctx context.Context, name string) value.Value { |
|||
for _, in := range c { |
|||
if val := in.Argument(ctx, name); !value.IsEmpty(val) { |
|||
return val |
|||
} |
|||
} |
|||
|
|||
return value.Empty |
|||
} |
|||
|
|||
func (c chain) Bind(ctx context.Context, def *Definition) error { |
|||
for _, input := range c { |
|||
if err := input.Bind(ctx, def); err != nil { |
|||
return err |
|||
} |
|||
} |
|||
|
|||
return nil |
|||
} |
@ -1,36 +0,0 @@ |
|||
package input |
|||
|
|||
import ( |
|||
"context" |
|||
|
|||
"gitoa.ru/go-4devs/console/input/value" |
|||
) |
|||
|
|||
type Wrap struct { |
|||
Input |
|||
Array |
|||
} |
|||
|
|||
func (w *Wrap) Option(ctx context.Context, name string) value.Value { |
|||
if v, ok := w.Array.GetOption(name); ok { |
|||
return v |
|||
} |
|||
|
|||
return w.Input.Option(ctx, name) |
|||
} |
|||
|
|||
func (w *Wrap) Argument(ctx context.Context, name string) value.Value { |
|||
if v, ok := w.Array.GetArgument(name); ok { |
|||
return v |
|||
} |
|||
|
|||
return w.Input.Argument(ctx, name) |
|||
} |
|||
|
|||
func (w *Wrap) Bind(ctx context.Context, def *Definition) error { |
|||
if err := w.Input.Bind(ctx, def); err != nil { |
|||
return err |
|||
} |
|||
|
|||
return w.Array.Bind(ctx, def) |
|||
} |
Loading…
Reference in new issue