Repository navigation
Expand file tree
/
Copy pathnumeric.go
More file actions
174 lines (160 loc) · 4.76 KB
/
Copy pathnumeric.go
File metadata and controls
174 lines (160 loc) · 4.76 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package enc
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"time"
"github.com/ohait/forego/ctx"
)
type Numeric interface {
Int64() (int64, error)
Uint64() (uint64, error)
Float64() (float64, error)
String() string
Duration(unit time.Duration) Duration
}
type numeric interface {
json.Marshaler
Numeric
Node
}
type Integer int64
var _ Node = Integer(0)
var _ Numeric = Integer(0)
func (this Integer) Int64() (int64, error) { return int64(this), nil }
func (this Integer) Uint64() (uint64, error) {
if this < 0 {
return 0, fmt.Errorf("%v: negative integer", this)
}
return uint64(this), nil
}
func (this Integer) Float64() (float64, error) { return float64(this), nil }
func (this Integer) String() string { return strconv.FormatInt(int64(this), 10) }
func (this Integer) GoString() string { return fmt.Sprintf("enc.Int{%v}", int64(this)) }
func (this Integer) native() any { return int64(this) }
func (this Integer) MarshalJSON() ([]byte, error) { return json.Marshal(int64(this)) }
func (this Integer) unmarshalInto(c ctx.C, handler Handler, into reflect.Value) error {
return unmarshalNumericInto(this, c, handler, into)
}
func (this Integer) Duration(unit time.Duration) Duration {
return Duration(time.Duration(this) * unit)
}
type Float float64
var _ Node = Float(0)
var _ Numeric = Float(0)
func (this Float) Int64() (int64, error) { return int64(this), nil }
func (this Float) Uint64() (uint64, error) {
if this < 0 {
return 0, fmt.Errorf("%v: negative float", this)
}
if this > Float(math.MaxUint64) {
return 0, fmt.Errorf("%v: overflows uint64", this)
}
return uint64(this), nil
}
func (this Float) Float64() (float64, error) { return float64(this), nil }
func (this Float) String() string { return strconv.FormatFloat(float64(this), 'g', -1, 64) }
func (this Float) GoString() string { return fmt.Sprintf("enc.Float{%v}", float64(this)) }
func (this Float) native() any { return float64(this) }
func (this Float) MarshalJSON() ([]byte, error) { return json.Marshal(float64(this)) }
func (this Float) unmarshalInto(c ctx.C, handler Handler, into reflect.Value) error {
return unmarshalNumericInto(this, c, handler, into)
}
func (this Float) Duration(unit time.Duration) Duration {
return Duration(float64(this) * float64(unit))
}
type Digits string
var _ numeric = Digits("0")
func (this Digits) Int64() (int64, error) {
i, err := strconv.ParseInt(string(this), 10, 64)
return i, err
}
func (this Digits) Uint64() (uint64, error) {
return strconv.ParseUint(string(this), 10, 64)
}
func (this Digits) Float64() (float64, error) { return strconv.ParseFloat(string(this), 64) }
func (this Digits) String() string { return string(this) }
func (this Digits) GoString() string { return fmt.Sprintf("enc.Num{%q}", string(this)) }
func (this Digits) MustFloat() Float {
f, err := strconv.ParseFloat(string(this), 64)
if err != nil {
panic(err)
}
return Float(f)
}
func (this Digits) MustInteger() Integer {
f, err := strconv.ParseInt(string(this), 10, 64)
if err != nil {
panic(err)
}
return Integer(f)
}
func (this Digits) MustUint() uint64 {
u, err := strconv.ParseUint(string(this), 10, 64)
if err != nil {
panic(err)
}
return u
}
func (this Digits) IsFloat() bool {
return strings.ContainsAny(string(this), ".eEgG")
}
func (this Digits) Duration(unit time.Duration) Duration {
if this.IsFloat() {
return this.MustFloat().Duration(unit)
} else {
u := this.MustUint()
return Duration(time.Duration(u) * unit)
}
}
func (this Digits) native() any {
f, err := this.Float64() // builtin json convert to float64 when unmarshalling into any, we should do the same
if err == nil {
return f
}
return this.String()
}
func (this Digits) MarshalJSON() ([]byte, error) { return []byte(this), nil }
func (this Digits) unmarshalInto(c ctx.C, handler Handler, into reflect.Value) error {
return unmarshalNumericInto(this, c, handler, into)
}
func unmarshalNumericInto(this numeric, c ctx.C, handler Handler, into reflect.Value) error {
//log.Debugf(c, "OHA %#v => %#v", this, into)
switch into.Kind() {
case reflect.Float64, reflect.Float32:
f, err := this.Float64()
if err != nil {
return err
}
into.SetFloat(f)
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
i, err := this.Int64()
if err != nil {
return err
}
into.SetInt(int64(i))
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
u, err := this.Uint64()
if err != nil {
return err
}
into.SetUint(u)
case reflect.Interface:
v := reflect.ValueOf(this.native())
into.Set(v)
default:
return ctx.NewErrorf(c, "can't unmarshal %s %T into %v", handler.path, this, into.Type())
}
return nil
}