Skip to content
Browse files

Binder: Add parsing of uint and float values

  • Loading branch information...
1 parent 1220ff2 commit 51dc31da66702d88af58162c681035352fa995c5 @robfig committed Feb 3, 2013
Showing with 71 additions and 0 deletions.
  1. +57 −0 binder.go
  2. +14 −0 binder_test.go
View
57 binder.go
@@ -67,6 +67,15 @@ func init() {
KindBinders[reflect.Int32] = ValueBinder(bindInt32)
KindBinders[reflect.Int64] = ValueBinder(bindInt64)
+ KindBinders[reflect.Uint] = ValueBinder(bindUint)
+ KindBinders[reflect.Uint8] = ValueBinder(bindUint8)
+ KindBinders[reflect.Uint16] = ValueBinder(bindUint16)
+ KindBinders[reflect.Uint32] = ValueBinder(bindUint32)
+ KindBinders[reflect.Uint64] = ValueBinder(bindUint64)
+
+ KindBinders[reflect.Float32] = ValueBinder(bindFloat32)
+ KindBinders[reflect.Float64] = ValueBinder(bindFloat64)
+
KindBinders[reflect.String] = ValueBinder(bindStr)
KindBinders[reflect.Bool] = ValueBinder(bindBool)
KindBinders[reflect.Slice] = bindSlice
@@ -91,6 +100,7 @@ func init() {
func bindStr(val string, typ reflect.Type) reflect.Value {
return reflect.ValueOf(val)
}
+
func bindInt(val string, typ reflect.Type) reflect.Value {
return reflect.ValueOf(int(bindIntHelper(val, 0)))
}
@@ -119,6 +129,53 @@ func bindIntHelper(val string, bits int) int64 {
return intValue
}
+func bindUint(val string, typ reflect.Type) reflect.Value {
+ return reflect.ValueOf(uint(bindUintHelper(val, 0)))
+}
+func bindUint8(val string, typ reflect.Type) reflect.Value {
+ return reflect.ValueOf(uint8(bindUintHelper(val, 8)))
+}
+func bindUint16(val string, typ reflect.Type) reflect.Value {
+ return reflect.ValueOf(uint16(bindUintHelper(val, 16)))
+}
+func bindUint32(val string, typ reflect.Type) reflect.Value {
+ return reflect.ValueOf(uint32(bindUintHelper(val, 32)))
+}
+func bindUint64(val string, typ reflect.Type) reflect.Value {
+ return reflect.ValueOf(uint64(bindUintHelper(val, 64)))
+}
+
+func bindUintHelper(val string, bits int) uint64 {
+ if len(val) == 0 {
+ return 0
+ }
+ uintValue, err := strconv.ParseUint(val, 10, bits)
+ if err != nil {
+ WARN.Println(err)
+ return 0
+ }
+ return uintValue
+}
+
+func bindFloat32(val string, typ reflect.Type) reflect.Value {
+ return reflect.ValueOf(float32(bindFloatHelper(val, 32)))
+}
+func bindFloat64(val string, typ reflect.Type) reflect.Value {
+ return reflect.ValueOf(float64(bindFloatHelper(val, 64)))
+}
+
+func bindFloatHelper(val string, bits int) float64 {
+ if len(val) == 0 {
+ return 0
+ }
+ floatValue, err := strconv.ParseFloat(val, bits)
+ if err != nil {
+ WARN.Println(err)
+ return 0
+ }
+ return floatValue
+}
+
// Booleans support a couple different value formats:
// "true" and "false"
// "on" and "" (a checkbox)
View
14 binder_test.go
@@ -29,6 +29,13 @@ var (
"int16": {"1"},
"int32": {"1"},
"int64": {"1"},
+ "uint": {"1"},
+ "uint8": {"1"},
+ "uint16": {"1"},
+ "uint32": {"1"},
+ "uint64": {"1"},
+ "float32": {"1.0"},
+ "float64": {"1.0"},
"str": {"hello"},
"bool-true": {"true"},
"bool-1": {"1"},
@@ -79,6 +86,13 @@ var binderTestCases = map[string]interface{}{
"int16": int16(1),
"int32": int32(1),
"int64": int64(1),
+ "uint": 1,
+ "uint8": uint8(1),
+ "uint16": uint16(1),
+ "uint32": uint32(1),
+ "uint64": uint64(1),
+ "float32": float32(1.0),
+ "float64": float64(1.0),
"str": "hello",
"bool-true": true,
"bool-1": true,

0 comments on commit 51dc31d

Please sign in to comment.
Something went wrong with that request. Please try again.