github.com/moleculer-go/moleculer@v0.3.3/payload/numberTransformers.go (about) 1 package payload 2 3 import ( 4 "fmt" 5 "strconv" 6 ) 7 8 type toIntFunc func(source *interface{}) int 9 type toInt64Func func(source *interface{}) int64 10 type toFloat32Func func(source *interface{}) float32 11 type toFloat64Func func(source *interface{}) float64 12 13 type toUint64Func func(source *interface{}) uint64 14 15 type numberTransformer struct { 16 name string 17 toInt toIntFunc 18 toInt64 toInt64Func 19 20 toFloat32 toFloat32Func 21 toFloat64 toFloat64Func 22 23 toUint64 toUint64Func 24 } 25 26 func stringToFloat64(value string) float64 { 27 fvalue, err := strconv.ParseFloat(value, 64) 28 if err != nil { 29 panic(fmt.Sprint("Could not convert string: ", value, " to a number!")) 30 } 31 return fvalue 32 } 33 34 var numberTransformers = []numberTransformer{ 35 { 36 name: "string", 37 toInt: func(source *interface{}) int { 38 return int(stringToFloat64((*source).(string))) 39 }, 40 toInt64: func(source *interface{}) int64 { 41 return int64(stringToFloat64((*source).(string))) 42 }, 43 toFloat32: func(source *interface{}) float32 { 44 return float32(stringToFloat64((*source).(string))) 45 }, 46 toFloat64: func(source *interface{}) float64 { 47 return float64(stringToFloat64((*source).(string))) 48 }, 49 toUint64: func(source *interface{}) uint64 { 50 return uint64(stringToFloat64((*source).(string))) 51 }, 52 }, 53 { 54 name: "int", 55 toInt: func(source *interface{}) int { 56 return (*source).(int) 57 }, 58 toInt64: func(source *interface{}) int64 { 59 return int64((*source).(int)) 60 }, 61 toFloat32: func(source *interface{}) float32 { 62 return float32((*source).(int)) 63 }, 64 toFloat64: func(source *interface{}) float64 { 65 return float64((*source).(int)) 66 }, 67 toUint64: func(source *interface{}) uint64 { 68 return uint64((*source).(int)) 69 }, 70 }, 71 { 72 name: "int32", 73 toInt: func(source *interface{}) int { 74 return int((*source).(int32)) 75 }, 76 toInt64: func(source *interface{}) int64 { 77 return int64((*source).(int32)) 78 }, 79 toFloat32: func(source *interface{}) float32 { 80 return float32((*source).(int32)) 81 }, 82 toFloat64: func(source *interface{}) float64 { 83 return float64((*source).(int32)) 84 }, 85 toUint64: func(source *interface{}) uint64 { 86 return uint64((*source).(int32)) 87 }, 88 }, 89 { 90 name: "int64", 91 toInt: func(source *interface{}) int { 92 return int((*source).(int64)) 93 }, 94 toInt64: func(source *interface{}) int64 { 95 return (*source).(int64) 96 }, 97 toFloat32: func(source *interface{}) float32 { 98 return float32((*source).(int64)) 99 }, 100 toFloat64: func(source *interface{}) float64 { 101 return float64((*source).(int64)) 102 }, 103 toUint64: func(source *interface{}) uint64 { 104 return uint64((*source).(int64)) 105 }, 106 }, 107 { 108 name: "float32", 109 toInt: func(source *interface{}) int { 110 return int((*source).(float32)) 111 }, 112 toInt64: func(source *interface{}) int64 { 113 return int64((*source).(float32)) 114 }, 115 toFloat32: func(source *interface{}) float32 { 116 return (*source).(float32) 117 }, 118 toFloat64: func(source *interface{}) float64 { 119 return float64((*source).(float32)) 120 }, 121 toUint64: func(source *interface{}) uint64 { 122 return uint64((*source).(float32)) 123 }, 124 }, 125 { 126 name: "float64", 127 toInt: func(source *interface{}) int { 128 return int((*source).(float64)) 129 }, 130 toInt64: func(source *interface{}) int64 { 131 return int64((*source).(float64)) 132 }, 133 toFloat32: func(source *interface{}) float32 { 134 return float32((*source).(float64)) 135 }, 136 toFloat64: func(source *interface{}) float64 { 137 return (*source).(float64) 138 }, 139 toUint64: func(source *interface{}) uint64 { 140 return uint64((*source).(float64)) 141 }, 142 }, 143 { 144 name: "uint64", 145 toInt: func(source *interface{}) int { 146 return int((*source).(uint64)) 147 }, 148 toInt64: func(source *interface{}) int64 { 149 return int64((*source).(uint64)) 150 }, 151 toFloat32: func(source *interface{}) float32 { 152 return float32((*source).(uint64)) 153 }, 154 toFloat64: func(source *interface{}) float64 { 155 return float64((*source).(uint64)) 156 }, 157 toUint64: func(source *interface{}) uint64 { 158 return (*source).(uint64) 159 }, 160 }, 161 } 162 163 // MapTransformer : return the map transformer for the specific map type 164 func getNumberTransformer(value *interface{}) *numberTransformer { 165 valueType := GetValueType(value) 166 for _, transformer := range numberTransformers { 167 if valueType == transformer.name { 168 return &transformer 169 } 170 } 171 fmt.Println("[WARN] getNumberTransformer() no transformer for valueType -> ", valueType) 172 return nil 173 }