Files
tecamino-json_data/type.go
Adrian Zürcher 85bd8de2a2 add files
2025-04-25 18:56:27 +02:00

103 lines
1.6 KiB
Go

package models
import (
"fmt"
"math/rand"
"github.com/tecamino/tecamino-dbm/utils"
)
const (
NONE Type = "NONE"
BIT Type = "BIT"
BYU Type = "BYU" // UINT8
BYS Type = "BYS" // INT8
WOS Type = "WOS" // INT16
WOU Type = "WOU" // UINT16
DWS Type = "DWS" // INT32
DWU Type = "DWU" // UINT32
LOS Type = "LOS" // INT64
LOU Type = "LOU" // UINT64
F32 Type = "F32" // FLOAT32
F64 Type = "F64" // FLOAT64
STR Type = "STRING" // STRING
)
type Type string
func (t *Type) DefaultValue() any {
switch *t {
case BIT:
return false
case BYS, BYU, WOS, WOU, DWS, DWU, LOS, LOU, F32, F64:
return 0
case STR:
return ""
}
return nil
}
func (t *Type) ConvertValue(v any) any {
switch *t {
case BIT:
return utils.BoolFrom(v)
case BYS:
return utils.Int8From(v)
case BYU:
return utils.Uint8From(v)
case WOS:
return utils.Int16From(v)
case WOU:
return utils.Uint16From(v)
case DWS:
return utils.Int32From(v)
case DWU:
return utils.Uint32From(v)
case LOS:
return utils.Int64From(v)
case LOU:
return utils.Uint64From(v)
case F32:
return utils.Float32From(v)
case F64:
return utils.Float64From(v)
case STR:
return fmt.Sprintf("%v", v)
}
return nil
}
func RandomType() Type {
n := rand.Intn(11) + 1
switch n {
case 1:
return "BIT"
case 2:
return "BYU"
case 3:
return "BYS"
case 4:
return "WOS"
case 5:
return "WOU"
case 6:
return "DWS"
case 7:
return "DWU"
case 8:
return "LOS"
case 9:
return "LOU"
case 10:
return "F32"
case 11:
return "F64"
case 12:
return "STRING"
default:
return "NONE"
}
}