-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathparamconverters.go
79 lines (61 loc) · 2.1 KB
/
pathparamconverters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"reflect"
"strconv"
)
type PathParameterConverter interface {
Convert(pathPart string) (reflect.Value, error)
}
var PathParameterConverterType = reflect.TypeOf((*PathParameterConverter)(nil)).Elem()
type StringPathParameterConverter struct{}
func (sc StringPathParameterConverter) Convert(pathPart string) (reflect.Value, error) {
return reflect.ValueOf(pathPart), nil
}
var stringPathParameterConverterSingleton = StringPathParameterConverter{}
type IntPathParameterConverter struct {
bitSize int
valueOf func(parsed int64) reflect.Value
}
func (ic IntPathParameterConverter) Convert(pathPart string) (reflect.Value, error) {
parsed, err := strconv.ParseInt(pathPart, 10, ic.bitSize)
if err != nil {
return reflect.Value{}, err
}
return ic.valueOf(parsed), nil
}
type UintPathParameterConverter struct {
bitSize int
valueOf func(parsed uint64) reflect.Value
}
func (uc UintPathParameterConverter) Convert(pathPart string) (reflect.Value, error) {
parsed, err := strconv.ParseUint(pathPart, 10, uc.bitSize)
if err != nil {
return reflect.Value{}, err
}
return uc.valueOf(parsed), nil
}
type BoolPathParameterConverter struct{}
func (bc BoolPathParameterConverter) Convert(pathPart string) (reflect.Value, error) {
parsed, err := strconv.ParseBool(pathPart)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(bool(parsed)), nil
}
var boolPathParameterConverterSingleton = BoolPathParameterConverter{}
type SliceBytePathParameterConverter struct{}
func (sbc SliceBytePathParameterConverter) Convert(pathPart string) (reflect.Value, error) {
return reflect.ValueOf([]byte(pathPart)), nil
}
var sliceBytePathParameterConverterSingleton = SliceBytePathParameterConverter{}
type ArrayBytePathParameterConverter struct {
length int
elementType reflect.Type
}
func (abc ArrayBytePathParameterConverter) Convert(pathPart string) (reflect.Value, error) {
arrayType := reflect.ArrayOf(abc.length, abc.elementType)
arrayValuePtr := reflect.New(arrayType)
arrayValue := arrayValuePtr.Elem()
reflect.Copy(arrayValue, reflect.ValueOf(pathPart))
return arrayValue, nil
}