request.go 2.95 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2
package commands

3 4
import (
	"fmt"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"io"
6 7 8 9
	"reflect"
	"strconv"
)

10 11
type optMap map[string]interface{}

Matt Bell's avatar
Matt Bell committed
12
// Request represents a call to a command from a consumer
13 14 15 16 17
type Request interface {
	Path() []string
	Option(name string) (interface{}, bool)
	SetOption(name string, val interface{})
	Arguments() []string
18
	Stream() io.Reader
19 20 21 22 23

	ConvertOptions(options map[string]Option) error
}

type request struct {
Matt Bell's avatar
Matt Bell committed
24
	path      []string
25
	options   optMap
Matt Bell's avatar
Matt Bell committed
26
	arguments []string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
	in        io.Reader
Matt Bell's avatar
Matt Bell committed
28 29
}

30 31
// Path returns the command path of this request
func (r *request) Path() []string {
32 33 34
	return r.path
}

35 36 37 38
// Option returns the value of the option for given name.
func (r *request) Option(name string) (interface{}, bool) {
	val, err := r.options[name]
	return val, err
Matt Bell's avatar
Matt Bell committed
39 40
}

41 42 43
// SetOption sets the value of the option for given name.
func (r *request) SetOption(name string, val interface{}) {
	r.options[name] = val
44 45
}

46 47
// Arguments returns the arguments slice
func (r *request) Arguments() []string {
Matt Bell's avatar
Matt Bell committed
48
	return r.arguments
49
}
Matt Bell's avatar
Matt Bell committed
50

51 52 53 54 55
// Stream returns the input stream Reader
func (r *request) Stream() io.Reader {
	return r.in
}

Matt Bell's avatar
Matt Bell committed
56 57
type converter func(string) (interface{}, error)

58
var converters = map[reflect.Kind]converter{
Matt Bell's avatar
Matt Bell committed
59
	Bool: func(v string) (interface{}, error) {
60 61 62 63 64
		if v == "" {
			return true, nil
		}
		return strconv.ParseBool(v)
	},
Matt Bell's avatar
Matt Bell committed
65
	Int: func(v string) (interface{}, error) {
66 67
		return strconv.ParseInt(v, 0, 32)
	},
Matt Bell's avatar
Matt Bell committed
68
	Uint: func(v string) (interface{}, error) {
69 70
		return strconv.ParseInt(v, 0, 32)
	},
Matt Bell's avatar
Matt Bell committed
71
	Float: func(v string) (interface{}, error) {
72 73 74 75
		return strconv.ParseFloat(v, 64)
	},
}

76
func (r *request) ConvertOptions(options map[string]Option) error {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	converted := make(map[string]interface{})

	for k, v := range r.options {
		opt, ok := options[k]
		if !ok {
			return fmt.Errorf("Unrecognized option: '%s'", k)
		}

		kind := reflect.TypeOf(v).Kind()
		var value interface{}

		if kind != opt.Type {
			if kind == String {
				convert := converters[opt.Type]
				val, err := convert(v.(string))
				if err != nil {
					return fmt.Errorf("Could not convert string value '%s' to type '%s'",
						v, opt.Type.String())
				}
				value = val

			} else {
				return fmt.Errorf("Option '%s' should be type '%s', but got type '%s'",
					k, opt.Type.String(), kind.String())
			}
		} else {
			value = v
		}

		for _, name := range opt.Names {
			if _, ok := r.options[name]; name != k && ok {
				return fmt.Errorf("Duplicate command options were provided ('%s' and '%s')",
					k, name)
			}

Matt Bell's avatar
Matt Bell committed
112
			converted[name] = value
113 114 115 116 117 118 119
		}
	}

	r.options = converted
	return nil
}

120 121
// NewEmptyRequest initializes an empty request
func NewEmptyRequest() Request {
122
	return NewRequest(nil, nil, nil, nil)
Matt Bell's avatar
Matt Bell committed
123 124
}

125
// NewRequest returns a request initialized with given arguments
126
func NewRequest(path []string, opts optMap, args []string, in io.Reader) Request {
127
	if path == nil {
Matt Bell's avatar
Matt Bell committed
128
		path = make([]string, 0)
Matt Bell's avatar
Matt Bell committed
129
	}
130 131 132 133 134 135
	if opts == nil {
		opts = make(map[string]interface{})
	}
	if args == nil {
		args = make([]string, 0)
	}
136
	return &request{path, opts, args, in}
Matt Bell's avatar
Matt Bell committed
137
}