-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwrap_float_enum.go
More file actions
90 lines (70 loc) · 2.42 KB
/
wrap_float_enum.go
File metadata and controls
90 lines (70 loc) · 2.42 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
package enum
import (
"database/sql/driver"
"encoding/xml"
"fmt"
"github.com/xybor-x/enum/internal/core"
"github.com/xybor-x/enum/internal/xreflect"
"gopkg.in/yaml.v3"
)
var _ newableEnum = WrapFloatEnum[int](0)
var _ hookAfterEnum = WrapFloatEnum[int](0)
// WrapFloatEnum provides a set of built-in methods to simplify working with
// float64 enums.
type WrapFloatEnum[underlyingEnum any] float64
func (e WrapFloatEnum[underlyingEnum]) IsValid() bool {
return IsValid(e)
}
func (e WrapFloatEnum[underlyingEnum]) MarshalJSON() ([]byte, error) {
return MarshalJSON(e)
}
func (e *WrapFloatEnum[underlyingEnum]) UnmarshalJSON(data []byte) error {
return UnmarshalJSON(data, e)
}
func (e WrapFloatEnum[underlyingEnum]) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error {
return MarshalXML(encoder, start, e)
}
func (e *WrapFloatEnum[underlyingEnum]) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
return UnmarshalXML(decoder, start, e)
}
func (e WrapFloatEnum[underlyingEnum]) MarshalYAML() (any, error) {
return MarshalYAML(e)
}
func (e *WrapFloatEnum[underlyingEnum]) UnmarshalYAML(node *yaml.Node) error {
return UnmarshalYAML(node, e)
}
func (e WrapFloatEnum[underlyingEnum]) Value() (driver.Value, error) {
return ValueSQL(e)
}
func (e *WrapFloatEnum[underlyingEnum]) Scan(a any) error {
return ScanSQL(a, e)
}
// To returns the underlying representation of this enum.
func (e WrapFloatEnum[underlyingEnum]) To() underlyingEnum {
return MustTo[underlyingEnum](e)
}
func (e WrapFloatEnum[underlyingEnum]) String() string {
return ToString(e)
}
func (e WrapFloatEnum[underlyingEnum]) GoString() string {
if !e.IsValid() {
return fmt.Sprintf("%f", e)
}
return fmt.Sprintf("%f (%s)", e, e)
}
// WARNING: Only use this function if you fully understand its behavior.
// It might cause unexpected results if used improperly.
func (e WrapFloatEnum[underlyingEnum]) newEnum(repr []any) any {
numeric := core.GetNumericRepresentation(repr)
if numeric == nil {
numeric = core.GetAvailableEnumValue[WrapFloatEnum[underlyingEnum]]()
} else {
repr = core.RemoveNumericRepresentation(repr)
}
return core.MapAny(xreflect.Convert[WrapFloatEnum[underlyingEnum]](numeric), repr)
}
// WARNING: Only use this function if you fully understand its behavior.
// It might cause unexpected results if used improperly.
func (e WrapFloatEnum[underlyingEnum]) hookAfter() {
mustHaveUnderlyingRepr[underlyingEnum](e)
}