@cryptotaxi247 / kubo / commits / 69374b30b

refactor(commands/optionvalue) use

@mappum this optionvalue is a really great abstraction. It makes maintenance super simple! @jbenet @whyrusleeping

Brian Tiger Chow committed Nov 11, 2014 at 23:31 UTC 69374b30be4c3da7dcf5692f476ae66f5bdf229d
2 files changed +38 -39
commands/option.go
+34 -35
@@ -78,60 +78,59 @@ func (ov OptionValue) Found() bool {
78 }
79
80 // value accessor methods, gets the value as a certain type
81 -func (ov OptionValue) Bool() (bool, error) {
81 +func (ov OptionValue) Bool() (value bool, found bool, err error) {
82 + if !ov.found {
83 + return false, false, nil
84 + }
85 val, ok := ov.value.(bool)
86 if !ok {
84 - var err error
85 - if ov.value != nil {
86 - err = errors.New("error casting to bool")
87 - }
88 - return false, err
87 + err = CastError
88 }
90 - return val, nil
89 + return val, ov.found, err
90 }
92 -func (ov OptionValue) Int() (int, error) {
91 +
92 +func (ov OptionValue) Int() (val int, found bool, err error) {
93 + if !ov.found {
94 + return 0, false, nil
95 + }
96 val, ok := ov.value.(int)
97 if !ok {
95 - var err error
96 - if ov.value != nil {
97 - err = errors.New("error casting to int")
98 - }
99 - return 0, err
98 + err = CastError
99 }
101 - return val, nil
100 + return val, ov.found, err
101 }
103 -func (ov OptionValue) Uint() (uint, error) {
102 +
103 +func (ov OptionValue) Uint() (val uint, found bool, err error) {
104 + if !ov.found {
105 + return 0, false, nil
106 + }
107 val, ok := ov.value.(uint)
108 if !ok {
106 - var err error
107 - if ov.value != nil {
108 - err = errors.New("error casting to uint")
109 - }
110 - return 0, err
109 + err = CastError
110 }
112 - return val, nil
111 + return val, ov.found, err
112 }
114 -func (ov OptionValue) Float() (float64, error) {
113 +
114 +func (ov OptionValue) Float() (val float64, found bool, err error) {
115 + if !ov.found {
116 + return 0, false, nil
117 + }
118 val, ok := ov.value.(float64)
119 if !ok {
117 - var err error
118 - if ov.value != nil {
119 - err = errors.New("error casting to float64")
120 - }
121 - return 0.0, err
120 + err = CastError
121 }
123 - return val, nil
122 + return val, ov.found, err
123 }
125 -func (ov OptionValue) String() (string, error) {
124 +
125 +func (ov OptionValue) String() (val string, found bool, err error) {
126 + if !ov.found {
127 + return "", false, nil
128 + }
129 val, ok := ov.value.(string)
130 if !ok {
128 - var err error
129 - if ov.value != nil {
130 - err = errors.New("error casting to string")
131 - }
132 - return "", err
131 + err = CastError
132 }
134 - return val, nil
133 + return val, ov.found, err
134 }
135
136 // Flag names
commands/response.go
+4 -4
@@ -108,13 +108,13 @@ func (r *response) Marshal() ([]byte, error) {
108 return []byte{}, nil
109 }
110
111 - if !r.req.Option(EncShort).Found() {
112 - return nil, fmt.Errorf("No encoding type was specified")
113 - }
114 - enc, err := r.req.Option(EncShort).String()
111 + enc, found, err := r.req.Option(EncShort).String()
112 if err != nil {
113 return nil, err
114 }
115 + if !found {
116 + return nil, fmt.Errorf("No encoding type was specified")
117 + }
118 encType := EncodingType(strings.ToLower(enc))
119
120 var marshaller Marshaller