github.com/mymmsc/gox@v1.3.33/util/lambda/Adder.go (about) 1 package lambda 2 3 import ( 4 "fmt" 5 "reflect" 6 ) 7 8 type _int struct { 9 v interface{} 10 t reflect.Type 11 } 12 13 func (i _int) Value() interface{} { 14 return i.v 15 } 16 17 func (i *_int) SetZero() { 18 switch i.t.Kind() { 19 case reflect.Int: 20 i.v = 0 21 case reflect.Uint8: 22 i.v = uint8(0) 23 case reflect.Uint16: 24 i.v = uint16(0) 25 case reflect.Uint32: 26 i.v = uint32(0) 27 case reflect.Uint64: 28 i.v = uint64(0) 29 case reflect.Int8: 30 i.v = int8(0) 31 case reflect.Int16: 32 i.v = int16(0) 33 case reflect.Int32: 34 i.v = int32(0) 35 case reflect.Int64: 36 i.v = int64(0) 37 } 38 } 39 40 type _float struct { 41 v interface{} 42 t reflect.Type 43 } 44 45 func (f _float) Value() interface{} { 46 return f.v 47 } 48 49 func (f *_float) SetZero() { 50 switch f.t.Kind() { 51 case reflect.Float32: 52 f.v = float32(0) 53 case reflect.Float64: 54 f.v = float64(0) 55 } 56 } 57 58 func Adder(t reflect.Type) Add { 59 60 support := []reflect.Kind{ 61 reflect.Int, 62 reflect.Uint8, 63 reflect.Uint16, 64 reflect.Uint32, 65 reflect.Uint64, 66 reflect.Int8, 67 reflect.Int16, 68 reflect.Uint32, 69 reflect.Int64, 70 reflect.Float32, 71 reflect.Float64, 72 } 73 vk := t.Kind() 74 if contain(support, vk) { 75 var add Add 76 if vk == reflect.Float64 || vk == reflect.Float32 { 77 add = &_float{t: t} 78 } else { 79 add = &_int{t: t} 80 } 81 add.SetZero() 82 return add 83 } 84 panic("not support type " + t.String()) 85 } 86 87 type Add interface { 88 Add(value reflect.Value) 89 Value() interface{} 90 SetZero() 91 } 92 93 func (i *_int) Add(value reflect.Value) { 94 if value.Kind() != i.t.Kind() { 95 panic(fmt.Sprintf("not support %s add %s", i.t.String(), value.Type().String())) 96 } 97 switch value.Type().Kind() { 98 case reflect.Int: 99 sum := i.v.(int) 100 sum += value.Interface().(int) 101 i.v = sum 102 case reflect.Uint8: 103 sum := i.v.(uint8) 104 sum += value.Interface().(uint8) 105 i.v = sum 106 case reflect.Uint16: 107 sum := i.v.(uint16) 108 sum += value.Interface().(uint16) 109 i.v = sum 110 case reflect.Uint32: 111 sum := i.v.(uint32) 112 sum += value.Interface().(uint32) 113 i.v = sum 114 case reflect.Uint64: 115 sum := i.v.(uint64) 116 sum += value.Interface().(uint64) 117 i.v = sum 118 case reflect.Int8: 119 sum := i.v.(int8) 120 sum += value.Interface().(int8) 121 i.v = sum 122 case reflect.Int16: 123 sum := i.v.(int16) 124 sum += value.Interface().(int16) 125 i.v = sum 126 case reflect.Int32: 127 sum := i.v.(int32) 128 sum += value.Interface().(int32) 129 i.v = sum 130 case reflect.Int64: 131 sum := i.v.(int64) 132 sum += value.Interface().(int64) 133 i.v = sum 134 } 135 } 136 137 func (f *_float) Add(value reflect.Value) { 138 if value.Kind() != f.t.Kind() { 139 panic(fmt.Sprintf("not support %s add %s", f.t.String(), value.Type().String())) 140 } 141 142 switch value.Type().Kind() { 143 case reflect.Float32: 144 sum := f.v.(float32) 145 sum += value.Interface().(float32) 146 f.v = sum 147 case reflect.Float64: 148 sum := f.v.(float64) 149 sum += value.Interface().(float64) 150 f.v = sum 151 } 152 }