-
Notifications
You must be signed in to change notification settings - Fork 1
/
as.go
138 lines (127 loc) · 2.9 KB
/
as.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Package as provides a easy way to convert numeric types with overflow check.
package as
import (
"fmt"
"reflect"
"golang.org/x/exp/constraints"
)
// InvalidTypeError is returned when user supplied invalid type to convert.
type InvalidTypeError struct {
ToType string
Value interface{}
}
func (e InvalidTypeError) Error() string {
return fmt.Sprintf("unsupported type %T (converting to %s)", e.Value, e.ToType)
}
// OverflowError is returned when given value overflows maximum number a type can hold.
type OverflowError struct {
ToType string
Value interface{}
}
func (e OverflowError) Error() string {
return fmt.Sprintf("%d (%T) overflows %s", e.Value, e.Value, e.ToType)
}
// Number constraint is used for type cast into any number.
// Only Integers for now until float support is added.
type Number interface {
constraints.Integer
}
// T is generic function to allow for easier checked
// type cast from any type to any Number type.
func T[To Number](v any) (To, error) {
var (
err error
out To
)
switch any(indirect(out)).(type) {
case int:
var n int
n, err = Int(v)
out = To(n)
case int8:
var n int8
n, err = Int8(v)
out = To(n)
case int16:
var n int16
n, err = Int16(v)
out = To(n)
case int32:
var n int32
n, err = Int32(v)
out = To(n)
case int64:
var n int64
n, err = Int64(v)
out = To(n)
case uint:
var n uint
n, err = Uint(v)
out = To(n)
case uint8:
var n uint8
n, err = Uint8(v)
out = To(n)
case uint16:
var n uint16
n, err = Uint16(v)
out = To(n)
case uint32:
var n uint32
n, err = Uint32(v)
out = To(n)
case uint64:
var n uint64
n, err = Uint64(v)
out = To(n)
default:
return out, InvalidTypeError{
ToType: fmt.Sprintf("%T", out),
Value: v,
}
}
return To(out), err
}
// From html/template/content.go
// Copyright 2011 The Go Authors. All rights reserved.
// indirect returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil).
func indirect(a interface{}) interface{} {
if a == nil {
return nil
}
typeof := reflect.TypeOf(a)
switch kind := typeof.Kind(); kind {
case reflect.Ptr:
v := reflect.ValueOf(a)
for v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return v.Interface()
case reflect.Int:
return convertTo(a, int(0))
case reflect.Int8:
return convertTo(a, int8(0))
case reflect.Int16:
return convertTo(a, int16(0))
case reflect.Int32:
return convertTo(a, int32(0))
case reflect.Int64:
return convertTo(a, int64(0))
case reflect.Uint:
return convertTo(a, uint(0))
case reflect.Uint8:
return convertTo(a, uint8(0))
case reflect.Uint16:
return convertTo(a, uint16(0))
case reflect.Uint32:
return convertTo(a, uint32(0))
case reflect.Uint64:
return convertTo(a, uint64(0))
}
return a
}
func convertTo(v interface{}, to interface{}) interface{} {
valueof := reflect.ValueOf(v)
return valueof.Convert(reflect.TypeOf(to)).Interface()
}