2022-07-07 22:26:46 +02:00
|
|
|
package sstore
|
|
|
|
|
2022-07-12 23:27:16 +02:00
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-08-30 04:18:02 +02:00
|
|
|
"strconv"
|
2022-07-12 23:27:16 +02:00
|
|
|
)
|
2022-07-07 22:26:46 +02:00
|
|
|
|
|
|
|
func quickSetStr(strVal *string, m map[string]interface{}, name string) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2022-08-30 04:18:02 +02:00
|
|
|
ival, ok := v.(int64)
|
|
|
|
if ok {
|
|
|
|
*strVal = strconv.FormatInt(ival, 10)
|
|
|
|
return
|
|
|
|
}
|
2022-07-07 22:26:46 +02:00
|
|
|
str, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*strVal = str
|
|
|
|
}
|
|
|
|
|
|
|
|
func quickSetInt64(ival *int64, m map[string]interface{}, name string) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sqlInt, ok := v.(int64)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*ival = sqlInt
|
|
|
|
}
|
|
|
|
|
|
|
|
func quickSetBool(bval *bool, m map[string]interface{}, name string) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sqlInt, ok := v.(int64)
|
|
|
|
if ok {
|
|
|
|
if sqlInt > 0 {
|
|
|
|
*bval = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sqlBool, ok := v.(bool)
|
|
|
|
if ok {
|
|
|
|
*bval = sqlBool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 07:02:38 +02:00
|
|
|
func quickSetBytes(bval *[]byte, m map[string]interface{}, name string) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sqlBytes, ok := v.([]byte)
|
|
|
|
if ok {
|
|
|
|
*bval = sqlBytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 22:26:46 +02:00
|
|
|
func quickSetJson(ptr interface{}, m map[string]interface{}, name string) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
str, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if str == "" {
|
2022-07-13 23:16:08 +02:00
|
|
|
str = "{}"
|
2022-07-07 22:26:46 +02:00
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(str), ptr)
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:13:00 +01:00
|
|
|
func quickSetJsonArr(ptr interface{}, m map[string]interface{}, name string) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
str, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if str == "" {
|
|
|
|
str = "[]"
|
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(str), ptr)
|
|
|
|
}
|
|
|
|
|
2022-07-07 22:26:46 +02:00
|
|
|
func quickJson(v interface{}) string {
|
|
|
|
if v == nil {
|
2022-07-13 23:16:08 +02:00
|
|
|
return "{}"
|
2022-07-07 22:26:46 +02:00
|
|
|
}
|
|
|
|
barr, _ := json.Marshal(v)
|
|
|
|
return string(barr)
|
|
|
|
}
|
2022-07-12 23:27:16 +02:00
|
|
|
|
2022-11-28 09:13:00 +01:00
|
|
|
func quickJsonArr(v interface{}) string {
|
|
|
|
if v == nil {
|
|
|
|
return "[]"
|
|
|
|
}
|
|
|
|
barr, _ := json.Marshal(v)
|
|
|
|
return string(barr)
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:27:16 +02:00
|
|
|
func quickScanJson(ptr interface{}, val interface{}) error {
|
2022-07-13 01:10:46 +02:00
|
|
|
barrVal, ok := val.([]byte)
|
2022-07-12 23:27:16 +02:00
|
|
|
if !ok {
|
2022-07-13 01:10:46 +02:00
|
|
|
strVal, ok := val.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot scan '%T' into '%T'", val, ptr)
|
|
|
|
}
|
|
|
|
barrVal = []byte(strVal)
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
2022-07-13 01:10:46 +02:00
|
|
|
if len(barrVal) == 0 {
|
2022-07-13 23:16:08 +02:00
|
|
|
barrVal = []byte("{}")
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
2022-07-13 01:10:46 +02:00
|
|
|
return json.Unmarshal(barrVal, ptr)
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func quickValueJson(v interface{}) (driver.Value, error) {
|
|
|
|
if v == nil {
|
2022-07-13 23:16:08 +02:00
|
|
|
return "{}", nil
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|
2022-07-13 01:10:46 +02:00
|
|
|
barr, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return string(barr), nil
|
2022-07-12 23:27:16 +02:00
|
|
|
}
|