more platform stuff
This commit is contained in:
1 parent
4b9a882018
commit
fc5eefbe5e
7 files changed
+756
No files matched your search
+140
@@ -0,0 +1,140 @@
|
||||
package conv
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Uint32 coerces an interface value to a uint32.
|
||||
// It returns the uint32 value and true on success, or 0 and false if coercion fails.
|
||||
func Uint32(v any) (uint32, bool) {
|
||||
if v == nil {
|
||||
return 0, false
|
||||
}
|
||||
switch n := v.(type) {
|
||||
case uint32:
|
||||
return n, true
|
||||
case float64:
|
||||
if n < 0 || n > 4294967295 {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(n), true
|
||||
case float32:
|
||||
if n < 0 || n > 4294967295 {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(n), true
|
||||
case int:
|
||||
if n < 0 || n > 4294967295 {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(n), true
|
||||
case int64:
|
||||
if n < 0 || n > 4294967295 {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(n), true
|
||||
case uint64:
|
||||
if n > 4294967295 {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(n), true
|
||||
case json.Number:
|
||||
u, err := strconv.ParseUint(n.String(), 10, 32)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(u), true
|
||||
case string:
|
||||
s := strings.TrimSpace(n)
|
||||
if s == "" {
|
||||
return 0, false
|
||||
}
|
||||
u, err := strconv.ParseUint(s, 10, 32)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return uint32(u), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// String coerces an interface value to a string.
|
||||
// It handles common numeric types and falls back to fmt.Sprintf.
|
||||
func String(v any) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
switch n := v.(type) {
|
||||
case string:
|
||||
return n
|
||||
case int:
|
||||
return strconv.Itoa(n)
|
||||
case int64:
|
||||
return strconv.FormatInt(n, 10)
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(n), 10)
|
||||
case uint64:
|
||||
return strconv.FormatUint(n, 10)
|
||||
case float64:
|
||||
// If it's a whole number, represent it as an integer to avoid scientific notation
|
||||
if n == float64(int64(n)) {
|
||||
return strconv.FormatInt(int64(n), 10)
|
||||
}
|
||||
return strconv.FormatFloat(n, 'f', -1, 64)
|
||||
case float32:
|
||||
if n == float32(int32(n)) {
|
||||
return strconv.Itoa(int(n))
|
||||
}
|
||||
return strconv.FormatFloat(float64(n), 'f', -1, 32)
|
||||
case json.Number:
|
||||
return n.String()
|
||||
case bool:
|
||||
if n {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// Float64 coerces an interface value to a float64.
|
||||
func Float64(v any) (float64, bool) {
|
||||
if v == nil {
|
||||
return 0, false
|
||||
}
|
||||
switch n := v.(type) {
|
||||
case float64:
|
||||
return n, true
|
||||
case float32:
|
||||
return float64(n), true
|
||||
case int:
|
||||
return float64(n), true
|
||||
case int64:
|
||||
return float64(n), true
|
||||
case uint32:
|
||||
return float64(n), true
|
||||
case uint64:
|
||||
return float64(n), true
|
||||
case string:
|
||||
s := strings.TrimSpace(n)
|
||||
if s == "" {
|
||||
return 0, false
|
||||
}
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return f, true
|
||||
case json.Number:
|
||||
f, err := n.Float64()
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return f, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package conv
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUint32(t *testing.T) {
|
||||
tests := []struct {
|
||||
input any
|
||||
expected uint32
|
||||
ok bool
|
||||
}{
|
||||
{uint32(123), 123, true},
|
||||
{float64(456.0), 456, true},
|
||||
{float64(-1.0), 0, false},
|
||||
{float32(789.0), 789, true},
|
||||
{100, 100, true},
|
||||
{int64(200), 200, true},
|
||||
{uint64(300), 300, true},
|
||||
{uint64(0xFFFFFFFF + 1), 0, false},
|
||||
{json.Number("42"), 42, true},
|
||||
{json.Number("invalid"), 0, false},
|
||||
{"500", 500, true},
|
||||
{" 600 ", 600, true},
|
||||
{"invalid", 0, false},
|
||||
{nil, 0, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
val, ok := Uint32(tt.input)
|
||||
if val != tt.expected || ok != tt.ok {
|
||||
t.Errorf("Uint32(%v) = (%d, %t), expected (%d, %t)", tt.input, val, ok, tt.expected, tt.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
tests := []struct {
|
||||
input any
|
||||
expected string
|
||||
}{
|
||||
{"hello", "hello"},
|
||||
{123, "123"},
|
||||
{int64(456), "456"},
|
||||
{uint32(789), "789"},
|
||||
{uint64(100), "100"},
|
||||
{float64(12.34), "12.34"},
|
||||
{float64(56.0), "56"},
|
||||
{float32(78.0), "78"},
|
||||
{json.Number("42.2"), "42.2"},
|
||||
{true, "true"},
|
||||
{false, "false"},
|
||||
{nil, ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
val := String(tt.input)
|
||||
if val != tt.expected {
|
||||
t.Errorf("String(%v) = %q, expected %q", tt.input, val, tt.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64(t *testing.T) {
|
||||
tests := []struct {
|
||||
input any
|
||||
expected float64
|
||||
ok bool
|
||||
}{
|
||||
{float64(12.34), 12.34, true},
|
||||
{float32(12.5), 12.5, true},
|
||||
{100, 100.0, true},
|
||||
{int64(200), 200.0, true},
|
||||
{uint32(300), 300.0, true},
|
||||
{uint64(400), 400.0, true},
|
||||
{"12.34", 12.34, true},
|
||||
{" 56.78 ", 56.78, true},
|
||||
{"invalid", 0.0, false},
|
||||
{json.Number("42.2"), 42.2, true},
|
||||
{nil, 0.0, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
val, ok := Float64(tt.input)
|
||||
if val != tt.expected || ok != tt.ok {
|
||||
t.Errorf("Float64(%v) = (%f, %t), expected (%f, %t)", tt.input, val, ok, tt.expected, tt.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user