Skip to content

Commit 4844523

Browse files
committed
Fixed bug causing custom types of simple integer and string types to not work correctly in automap decoding
1 parent eb61a4d commit 4844523

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

auto_map_array_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goriak
22

33
import (
4+
"reflect"
45
"testing"
56
)
67

@@ -94,3 +95,39 @@ func TestAutoMapByteSlice(t *testing.T) {
9495
t.Error("Wrong Val")
9596
}
9697
}
98+
99+
func TestCustomTypeUint8(t *testing.T) {
100+
type CustomNumber uint8
101+
type CustomString string
102+
type CustomSlice []uint8
103+
type tt struct {
104+
Num CustomNumber
105+
Str CustomString
106+
Sli CustomSlice
107+
}
108+
109+
input := tt{
110+
Num: 50,
111+
Str: "Hello",
112+
Sli: CustomSlice{1, 2, 3, 4, 5},
113+
}
114+
115+
res, err := bucket().Set(input).Run(con())
116+
if err != nil {
117+
t.Error(err)
118+
}
119+
120+
var output tt
121+
122+
_, err = bucket().Get(res.Key, &output).Run(con())
123+
if err != nil {
124+
t.Error(err)
125+
}
126+
127+
if !reflect.DeepEqual(input, output) {
128+
t.Logf("%+v", input)
129+
t.Logf("%+v", output)
130+
}
131+
132+
t.Logf("%+v", output)
133+
}

auto_map_decode.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ func transRiakToSlice(sliceValue reflect.Value, registerName string, data *riak.
298298

299299
// Override the Slice from "Userland"
300300
sliceValue.Set(finalSliceValue)
301-
302301
}
303302

304303
return nil
@@ -317,58 +316,72 @@ func bytesToValue(input []byte, outputType reflect.Type) (reflect.Value, error)
317316

318317
outputKind := outputType.Kind()
319318

319+
// The final type (can be a custom type for example)
320+
newWithSameType := reflect.New(outputType).Elem()
321+
320322
switch outputKind {
321323
case reflect.String:
322-
return reflect.ValueOf(string(input)), nil
324+
newWithSameType.SetString(string(input))
325+
return newWithSameType, nil
323326

324327
case reflect.Int:
325328
if i, err := strconv.ParseInt(string(input), 10, 0); err == nil {
326-
return reflect.ValueOf(int(i)), nil
329+
newWithSameType.SetInt(i)
330+
return newWithSameType, nil
327331
}
328332

329333
case reflect.Int8:
330334
if i, err := strconv.ParseInt(string(input), 10, 8); err == nil {
331-
return reflect.ValueOf(int8(i)), nil
335+
newWithSameType.SetInt(i)
336+
return newWithSameType, nil
332337
}
333338

334339
case reflect.Int16:
335340
if i, err := strconv.ParseInt(string(input), 10, 16); err == nil {
336-
return reflect.ValueOf(int16(i)), nil
341+
newWithSameType.SetInt(i)
342+
return newWithSameType, nil
337343
}
338344

339345
case reflect.Int32:
340346
if i, err := strconv.ParseInt(string(input), 10, 32); err == nil {
341-
return reflect.ValueOf(int32(i)), nil
347+
newWithSameType.SetInt(i)
348+
return newWithSameType, nil
342349
}
343350

344351
case reflect.Int64:
345352
if i, err := strconv.ParseInt(string(input), 10, 64); err == nil {
346-
return reflect.ValueOf(int64(i)), nil
353+
newWithSameType.SetInt(i)
354+
return newWithSameType, nil
347355
}
348356

349357
case reflect.Uint:
350358
if i, err := strconv.ParseUint(string(input), 10, 0); err == nil {
351-
return reflect.ValueOf(uint(i)), nil
359+
newWithSameType.SetUint(i)
360+
return newWithSameType, nil
352361
}
353362

354363
case reflect.Uint8:
355364
if i, err := strconv.ParseUint(string(input), 10, 8); err == nil {
356-
return reflect.ValueOf(uint8(i)), nil
365+
newWithSameType.SetUint(i)
366+
return newWithSameType, nil
357367
}
358368

359369
case reflect.Uint16:
360370
if i, err := strconv.ParseUint(string(input), 10, 16); err == nil {
361-
return reflect.ValueOf(uint16(i)), nil
371+
newWithSameType.SetUint(i)
372+
return newWithSameType, nil
362373
}
363374

364375
case reflect.Uint32:
365376
if i, err := strconv.ParseUint(string(input), 10, 32); err == nil {
366-
return reflect.ValueOf(uint32(i)), nil
377+
newWithSameType.SetUint(i)
378+
return newWithSameType, nil
367379
}
368380

369381
case reflect.Uint64:
370382
if i, err := strconv.ParseUint(string(input), 10, 64); err == nil {
371-
return reflect.ValueOf(uint64(i)), nil
383+
newWithSameType.SetUint(i)
384+
return newWithSameType, nil
372385
}
373386

374387
case reflect.Slice:

0 commit comments

Comments
 (0)