Skip to content

Instantly share code, notes, and snippets.

@ihidchaos
Created March 6, 2020 08:56
Show Gist options
  • Save ihidchaos/be4166c2f46124591157be56d549323e to your computer and use it in GitHub Desktop.
Save ihidchaos/be4166c2f46124591157be56d549323e to your computer and use it in GitHub Desktop.
datatype transformer
package drivers
import (
"encoding/binary"
"encoding/hex"
"github.com/shopspring/decimal"
"math"
"strings"
)
func CheckDataTypeOK(in string) bool {
var DataTypeMap = map[string]bool{
"HEX": true, "STRING": true,
"INT16BE": true, "INT16LE": true, "UINT16BE": true, "UINT16LE": true,
"INT32BE": true, "INT32LE": true, "INT32SW": true, "INT32SB": true,
"UINT32BE": true, "UINT32LE": true, "UINT32SW": true, "UINT32SB": true,
"INT64BE": true, "INT64LE": true, "INT64SW": true, "INT64SB": true,
"UINT64BE": true, "UINT64LE": true, "UINT64SW": true, "UINT64SB": true,
"FLOAT32BE": true, "FLOAT32LE": true, "FLOAT32SW": true, "FLOAT32SB": true,
"FLOAT64BE": true, "FLOAT64LE": true, "FLOAT64SW": true, "FLOAT64SB": true,
}
if DataTypeMap[in] {
return true
}
return false
}
func Scale(transValue, scale float64) (out float64) {
floatTrans := decimal.NewFromFloat(transValue)
floatScale := decimal.NewFromFloat(scale)
o, _ := floatTrans.Mul(floatScale).Float64()
//logrus.Info(floatTrans, floatScale, o)
return o
}
type Transform struct{}
func (t *Transform) HEX(in []byte, scale float64) (out string) {
return strings.ToUpper(hex.EncodeToString(in))
}
func (t *Transform) STRING(in []byte, scale float64) (out string) {
return string(in[:])
}
func (t *Transform) INT16BE(in []byte, scale float64) (out int16) {
transValue := binary.BigEndian.Uint16(in)
return int16(Scale(float64(transValue), scale))
}
func (t *Transform) INT16LE(in []byte, scale float64) (out int16) {
transValue := binary.LittleEndian.Uint16(in)
return int16(Scale(float64(transValue), scale))
}
func (t *Transform) UINT16BE(in []byte, scale float64) (out uint16) {
transValue := binary.BigEndian.Uint16(in)
return uint16(Scale(float64(transValue), scale))
}
func (t *Transform) UINT16LE(in []byte, scale float64) (out uint16) {
transValue := binary.LittleEndian.Uint16(in)
return uint16(Scale(float64(transValue), scale))
}
func (t *Transform) INT32BE(in []byte, scale float64) (out int32) {
transValue := binary.BigEndian.Uint32(in)
return int32(Scale(float64(transValue), scale))
}
func (t *Transform) INT32LE(in []byte, scale float64) (out int32) {
transValue := binary.LittleEndian.Uint32(in)
return int32(Scale(float64(transValue), scale))
}
func (t *Transform) INT32SW(in []byte, scale float64) (out int32) {
tmp := []byte{in[1], in[0], in[3], in[2]}
transValue := binary.BigEndian.Uint32(tmp)
return int32(Scale(float64(transValue), scale))
}
func (t *Transform) INT32SB(in []byte, scale float64) (out int32) {
tmp := []byte{in[1], in[0], in[3], in[2]}
transValue := binary.LittleEndian.Uint32(tmp)
return int32(Scale(float64(transValue), scale))
}
func (t *Transform) UINT32BE(in []byte, scale float64) (out uint32) {
transValue := binary.BigEndian.Uint32(in)
return uint32(Scale(float64(transValue), scale))
}
func (t *Transform) UINT32LE(in []byte, scale float64) (out uint32) {
transValue := binary.LittleEndian.Uint32(in)
return uint32(Scale(float64(transValue), scale))
}
func (t *Transform) UINT32SW(in []byte, scale float64) (out uint32) {
tmp := []byte{in[1], in[0], in[3], in[2]}
transValue := binary.BigEndian.Uint32(tmp)
return uint32(Scale(float64(transValue), scale))
}
func (t *Transform) UINT32SB(in []byte, scale float64) (out uint32) {
tmp := []byte{in[1], in[0], in[3], in[2]}
transValue := binary.LittleEndian.Uint32(tmp)
return uint32(Scale(float64(transValue), scale))
}
func (t *Transform) INT64BE(in []byte, scale float64) (out int64) {
transValue := binary.BigEndian.Uint64(in)
return int64(Scale(float64(transValue), scale))
}
func (t *Transform) INT64LE(in []byte, scale float64) (out int64) {
transValue := binary.LittleEndian.Uint64(in)
return int64(Scale(float64(transValue), scale))
}
func (t *Transform) INT64SW(in []byte, scale float64) (out int64) {
tmp := []byte{in[1], in[0], in[3], in[2], in[5], in[4], in[7], in[6]}
transValue := binary.BigEndian.Uint64(tmp)
return int64(Scale(float64(transValue), scale))
}
func (t *Transform) INT64SB(in []byte, scale float64) (out int64) {
tmp := []byte{in[1], in[0], in[3], in[2], in[5], in[4], in[7], in[6]}
transValue := binary.LittleEndian.Uint64(tmp)
return int64(Scale(float64(transValue), scale))
}
func (t *Transform) UINT64BE(in []byte, scale float64) (out uint64) {
transValue := binary.BigEndian.Uint64(in)
return uint64(Scale(float64(transValue), scale))
}
func (t *Transform) UINT64LE(in []byte, scale float64) (out uint64) {
transValue := binary.LittleEndian.Uint64(in)
return uint64(Scale(float64(transValue), scale))
}
func (t *Transform) UINT64SW(in []byte, scale float64) (out uint64) {
tmp := []byte{in[1], in[0], in[3], in[2], in[5], in[4], in[7], in[6]}
transValue := binary.BigEndian.Uint64(tmp)
return uint64(Scale(float64(transValue), scale))
}
func (t *Transform) UINT64SB(in []byte, scale float64) (out uint64) {
tmp := []byte{in[1], in[0], in[3], in[2], in[5], in[4], in[7], in[6]}
transValue := binary.LittleEndian.Uint64(tmp)
return uint64(Scale(float64(transValue), scale))
}
func (t *Transform) FLOAT32BE(in []byte, scale float64) (out float32) {
transValue := math.Float32frombits(binary.BigEndian.Uint32(in))
return float32(Scale(float64(transValue), scale))
}
func (t *Transform) FLOAT32LE(in []byte, scale float64) (out float32) {
transValue := math.Float32frombits(binary.LittleEndian.Uint32(in))
return float32(Scale(float64(transValue), scale))
}
func (t *Transform) FLOAT32SW(in []byte, scale float64) (out float32) {
tmp := []byte{in[1], in[0], in[3], in[2]}
transValue := math.Float32frombits(binary.BigEndian.Uint32(tmp))
return float32(Scale(float64(transValue), scale))
}
func (t *Transform) FLOAT32SB(in []byte, scale float64) (out float32) {
tmp := []byte{in[1], in[0], in[3], in[2]}
transValue := math.Float32frombits(binary.LittleEndian.Uint32(tmp))
return float32(Scale(float64(transValue), scale))
}
func (t *Transform) FLOAT64BE(in []byte, scale float64) (out float64) {
transValue := math.Float64frombits(binary.BigEndian.Uint64(in))
return Scale(transValue, scale)
}
func (t *Transform) FLOAT64LE(in []byte, scale float64) (out float64) {
transValue := math.Float64frombits(binary.LittleEndian.Uint64(in))
return Scale(transValue, scale)
}
func (t *Transform) FLOAT64SW(in []byte, scale float64) (out float64) {
tmp := []byte{in[1], in[0], in[3], in[2], in[5], in[4], in[7], in[6]}
transValue := math.Float64frombits(binary.BigEndian.Uint64(tmp))
return Scale(transValue, scale)
}
func (t *Transform) FLOAT64SB(in []byte, scale float64) (out float64) {
tmp := []byte{in[1], in[0], in[3], in[2], in[5], in[4], in[7], in[6]}
transValue := math.Float64frombits(binary.LittleEndian.Uint64(tmp))
return Scale(transValue, scale)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment