github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/csvdec/fillslice.go (about) 1 // ***** DO NOT EDIT THIS FILE MANUALLY. ***** 2 // 3 // This file was auto-generated from 'fillslice.got' using Gent. 4 // 5 // Gent: https://www.github.com/fluhus/gent 6 7 package csvdec 8 9 import ( 10 "reflect" 11 "strconv" 12 ) 13 14 // Populates any slice value. 15 func fillSlice(value reflect.Value, fields []string) error { 16 kind := value.Type().Elem().Kind() 17 switch kind { 18 case reflect.String: 19 return fillStringSlice(value, fields) 20 case reflect.Int: 21 return fillIntSlice(value, fields) 22 case reflect.Int8: 23 return fillInt8Slice(value, fields) 24 case reflect.Int16: 25 return fillInt16Slice(value, fields) 26 case reflect.Int32: 27 return fillInt32Slice(value, fields) 28 case reflect.Int64: 29 return fillInt64Slice(value, fields) 30 case reflect.Uint: 31 return fillUintSlice(value, fields) 32 case reflect.Uint8: 33 return fillUint8Slice(value, fields) 34 case reflect.Uint16: 35 return fillUint16Slice(value, fields) 36 case reflect.Uint32: 37 return fillUint32Slice(value, fields) 38 case reflect.Uint64: 39 return fillUint64Slice(value, fields) 40 case reflect.Float32: 41 return fillFloat32Slice(value, fields) 42 case reflect.Float64: 43 return fillFloat64Slice(value, fields) 44 } 45 panic("Unsupported type: " + value.Type().String()) 46 } 47 48 // Populates the given int slice with values parsed from fields. 49 // Returns an error if parsing fails. 50 func fillIntSlice(value reflect.Value, fields []string) error { 51 parsed := make([]int, len(fields)) 52 for i, field := range fields { 53 n, err := strconv.ParseInt(field, 0, 0) 54 if err != nil { 55 return err 56 } 57 parsed[i] = int(n) 58 } 59 value.Set(reflect.ValueOf(parsed)) 60 return nil 61 } 62 63 // Populates the given int8 slice with values parsed from fields. 64 // Returns an error if parsing fails. 65 func fillInt8Slice(value reflect.Value, fields []string) error { 66 parsed := make([]int8, len(fields)) 67 for i, field := range fields { 68 n, err := strconv.ParseInt(field, 0, 8) 69 if err != nil { 70 return err 71 } 72 parsed[i] = int8(n) 73 } 74 value.Set(reflect.ValueOf(parsed)) 75 return nil 76 } 77 78 // Populates the given int16 slice with values parsed from fields. 79 // Returns an error if parsing fails. 80 func fillInt16Slice(value reflect.Value, fields []string) error { 81 parsed := make([]int16, len(fields)) 82 for i, field := range fields { 83 n, err := strconv.ParseInt(field, 0, 16) 84 if err != nil { 85 return err 86 } 87 parsed[i] = int16(n) 88 } 89 value.Set(reflect.ValueOf(parsed)) 90 return nil 91 } 92 93 // Populates the given int32 slice with values parsed from fields. 94 // Returns an error if parsing fails. 95 func fillInt32Slice(value reflect.Value, fields []string) error { 96 parsed := make([]int32, len(fields)) 97 for i, field := range fields { 98 n, err := strconv.ParseInt(field, 0, 32) 99 if err != nil { 100 return err 101 } 102 parsed[i] = int32(n) 103 } 104 value.Set(reflect.ValueOf(parsed)) 105 return nil 106 } 107 108 // Populates the given int64 slice with values parsed from fields. 109 // Returns an error if parsing fails. 110 func fillInt64Slice(value reflect.Value, fields []string) error { 111 parsed := make([]int64, len(fields)) 112 for i, field := range fields { 113 n, err := strconv.ParseInt(field, 0, 64) 114 if err != nil { 115 return err 116 } 117 parsed[i] = int64(n) 118 } 119 value.Set(reflect.ValueOf(parsed)) 120 return nil 121 } 122 123 // Populates the given uint slice with values parsed from fields. 124 // Returns an error if parsing fails. 125 func fillUintSlice(value reflect.Value, fields []string) error { 126 parsed := make([]uint, len(fields)) 127 for i, field := range fields { 128 n, err := strconv.ParseUint(field, 0, 0) 129 if err != nil { 130 return err 131 } 132 parsed[i] = uint(n) 133 } 134 value.Set(reflect.ValueOf(parsed)) 135 return nil 136 } 137 138 // Populates the given uint8 slice with values parsed from fields. 139 // Returns an error if parsing fails. 140 func fillUint8Slice(value reflect.Value, fields []string) error { 141 parsed := make([]uint8, len(fields)) 142 for i, field := range fields { 143 n, err := strconv.ParseUint(field, 0, 8) 144 if err != nil { 145 return err 146 } 147 parsed[i] = uint8(n) 148 } 149 value.Set(reflect.ValueOf(parsed)) 150 return nil 151 } 152 153 // Populates the given uint16 slice with values parsed from fields. 154 // Returns an error if parsing fails. 155 func fillUint16Slice(value reflect.Value, fields []string) error { 156 parsed := make([]uint16, len(fields)) 157 for i, field := range fields { 158 n, err := strconv.ParseUint(field, 0, 16) 159 if err != nil { 160 return err 161 } 162 parsed[i] = uint16(n) 163 } 164 value.Set(reflect.ValueOf(parsed)) 165 return nil 166 } 167 168 // Populates the given uint32 slice with values parsed from fields. 169 // Returns an error if parsing fails. 170 func fillUint32Slice(value reflect.Value, fields []string) error { 171 parsed := make([]uint32, len(fields)) 172 for i, field := range fields { 173 n, err := strconv.ParseUint(field, 0, 32) 174 if err != nil { 175 return err 176 } 177 parsed[i] = uint32(n) 178 } 179 value.Set(reflect.ValueOf(parsed)) 180 return nil 181 } 182 183 // Populates the given uint64 slice with values parsed from fields. 184 // Returns an error if parsing fails. 185 func fillUint64Slice(value reflect.Value, fields []string) error { 186 parsed := make([]uint64, len(fields)) 187 for i, field := range fields { 188 n, err := strconv.ParseUint(field, 0, 64) 189 if err != nil { 190 return err 191 } 192 parsed[i] = uint64(n) 193 } 194 value.Set(reflect.ValueOf(parsed)) 195 return nil 196 } 197 198 // Populates the given float32 slice with values parsed from fields. 199 // Returns an error if parsing fails. 200 func fillFloat32Slice(value reflect.Value, fields []string) error { 201 parsed := make([]float32, len(fields)) 202 for i, field := range fields { 203 n, err := strconv.ParseFloat(field, 32) 204 if err != nil { 205 return err 206 } 207 parsed[i] = float32(n) 208 } 209 value.Set(reflect.ValueOf(parsed)) 210 return nil 211 } 212 213 // Populates the given float64 slice with values parsed from fields. 214 // Returns an error if parsing fails. 215 func fillFloat64Slice(value reflect.Value, fields []string) error { 216 parsed := make([]float64, len(fields)) 217 for i, field := range fields { 218 n, err := strconv.ParseFloat(field, 64) 219 if err != nil { 220 return err 221 } 222 parsed[i] = float64(n) 223 } 224 value.Set(reflect.ValueOf(parsed)) 225 return nil 226 } 227 228 // Populates the given string slice with values parsed from fields. 229 // Returns an error if parsing fails. 230 func fillStringSlice(value reflect.Value, fields []string) error { 231 // Fields may be a part of a bigger slice, so copying to allow the big 232 // slice to get CG'ed. 233 slice := make([]string, len(fields)) 234 copy(slice, fields) 235 value.Set(reflect.ValueOf(slice)) 236 return nil 237 }