github.com/pubgo/xprocess@v0.1.11/xprocess_schema/converter.go (about) 1 // Copyright 2012 The Gorilla Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package xprocess_schema 6 7 import ( 8 "reflect" 9 "strconv" 10 ) 11 12 type Converter func(string) reflect.Value 13 14 var ( 15 invalidValue = reflect.Value{} 16 boolType = reflect.Bool 17 float32Type = reflect.Float32 18 float64Type = reflect.Float64 19 intType = reflect.Int 20 int8Type = reflect.Int8 21 int16Type = reflect.Int16 22 int32Type = reflect.Int32 23 int64Type = reflect.Int64 24 stringType = reflect.String 25 uintType = reflect.Uint 26 uint8Type = reflect.Uint8 27 uint16Type = reflect.Uint16 28 uint32Type = reflect.Uint32 29 uint64Type = reflect.Uint64 30 ) 31 32 // Default converters for basic types. 33 var builtinConverters = map[reflect.Kind]Converter{ 34 boolType: convertBool, 35 float32Type: convertFloat32, 36 float64Type: convertFloat64, 37 intType: convertInt, 38 int8Type: convertInt8, 39 int16Type: convertInt16, 40 int32Type: convertInt32, 41 int64Type: convertInt64, 42 stringType: convertString, 43 uintType: convertUint, 44 uint8Type: convertUint8, 45 uint16Type: convertUint16, 46 uint32Type: convertUint32, 47 uint64Type: convertUint64, 48 } 49 50 func convertBool(value string) reflect.Value { 51 if value == "on" { 52 return reflect.ValueOf(true) 53 } else if v, err := strconv.ParseBool(value); err == nil { 54 return reflect.ValueOf(v) 55 } 56 return invalidValue 57 } 58 59 func convertFloat32(value string) reflect.Value { 60 if v, err := strconv.ParseFloat(value, 32); err == nil { 61 return reflect.ValueOf(float32(v)) 62 } 63 return invalidValue 64 } 65 66 func convertFloat64(value string) reflect.Value { 67 if v, err := strconv.ParseFloat(value, 64); err == nil { 68 return reflect.ValueOf(v) 69 } 70 return invalidValue 71 } 72 73 func convertInt(value string) reflect.Value { 74 if v, err := strconv.ParseInt(value, 10, 0); err == nil { 75 return reflect.ValueOf(int(v)) 76 } 77 return invalidValue 78 } 79 80 func convertInt8(value string) reflect.Value { 81 if v, err := strconv.ParseInt(value, 10, 8); err == nil { 82 return reflect.ValueOf(int8(v)) 83 } 84 return invalidValue 85 } 86 87 func convertInt16(value string) reflect.Value { 88 if v, err := strconv.ParseInt(value, 10, 16); err == nil { 89 return reflect.ValueOf(int16(v)) 90 } 91 return invalidValue 92 } 93 94 func convertInt32(value string) reflect.Value { 95 if v, err := strconv.ParseInt(value, 10, 32); err == nil { 96 return reflect.ValueOf(int32(v)) 97 } 98 return invalidValue 99 } 100 101 func convertInt64(value string) reflect.Value { 102 if v, err := strconv.ParseInt(value, 10, 64); err == nil { 103 return reflect.ValueOf(v) 104 } 105 return invalidValue 106 } 107 108 func convertString(value string) reflect.Value { 109 return reflect.ValueOf(value) 110 } 111 112 func convertUint(value string) reflect.Value { 113 if v, err := strconv.ParseUint(value, 10, 0); err == nil { 114 return reflect.ValueOf(uint(v)) 115 } 116 return invalidValue 117 } 118 119 func convertUint8(value string) reflect.Value { 120 if v, err := strconv.ParseUint(value, 10, 8); err == nil { 121 return reflect.ValueOf(uint8(v)) 122 } 123 return invalidValue 124 } 125 126 func convertUint16(value string) reflect.Value { 127 if v, err := strconv.ParseUint(value, 10, 16); err == nil { 128 return reflect.ValueOf(uint16(v)) 129 } 130 return invalidValue 131 } 132 133 func convertUint32(value string) reflect.Value { 134 if v, err := strconv.ParseUint(value, 10, 32); err == nil { 135 return reflect.ValueOf(uint32(v)) 136 } 137 return invalidValue 138 } 139 140 func convertUint64(value string) reflect.Value { 141 if v, err := strconv.ParseUint(value, 10, 64); err == nil { 142 return reflect.ValueOf(v) 143 } 144 return invalidValue 145 }