- no platform dependency in sync.go

This commit is contained in:
gazebo
2018-10-11 22:17:20 +08:00
parent 02b97909bc
commit 6133998e54
17 changed files with 152 additions and 52 deletions

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"math"
"math/rand"
"reflect"
"regexp"
"strings"
"sync"
@@ -333,10 +332,6 @@ func FilterMapByFieldList(mapData map[string]interface{}, fields []string) (vali
return valid, invalid
}
func GetObjFieldByName(obj interface{}, fieldName string) interface{} {
return reflect.Indirect(reflect.ValueOf(obj)).FieldByName(fieldName).Interface()
}
func MakeValidationMapFromSlice(validValues []string, flag int) map[string]int {
retVal := make(map[string]int)
for _, v := range validValues {

View File

@@ -131,3 +131,20 @@ func GetSliceLen(list interface{}) int {
func CaculateSkuVendorPrice(price int, percentage int) int {
return price * percentage / 100
}
// 生成一个不重复的临时ID
func genFakeID1() int64 {
return time.Now().UnixNano() / 1000000
}
func GenFakeID() int64 {
return genFakeID1() * 2
}
func IsFakeID(id int64) bool {
if id == 0 {
return true
}
multiple := id / genFakeID1()
return multiple == 2 || multiple == 3
}

View File

@@ -31,3 +31,18 @@ func TestGetPolygonFromCircle(t *testing.T) {
}
t.Log(points)
}
func TestFakeID(t *testing.T) {
id := GenFakeID()
if !IsFakeID(id) {
t.Fatalf("wrong result for id:%d", id)
}
id = 0
if !IsFakeID(id) {
t.Fatalf("wrong result for id:%d", id)
}
id = 23424234
if IsFakeID(id) {
t.Fatalf("wrong result for id:%d", id)
}
}

View File

@@ -0,0 +1,22 @@
package jxutils
import "reflect"
func CheckAndGetStructValue(item interface{}) *reflect.Value {
value := reflect.ValueOf(item)
if value.Kind() == reflect.Ptr {
value = value.Elem()
} else {
panic("item ust be ptr type")
}
return &value
}
func GetObjFieldByName(obj interface{}, fieldName string) interface{} {
return reflect.Indirect(reflect.ValueOf(obj)).FieldByName(fieldName).Interface()
}
func SetObjFieldByName(obj interface{}, fieldName string, value interface{}) {
refValue := CheckAndGetStructValue(obj)
refValue.FieldByName(fieldName).Set(reflect.ValueOf(value))
}

View File

@@ -0,0 +1,41 @@
package jxutils
import (
"testing"
"git.rosy.net.cn/baseapi/utils"
)
func TestObjFieldByName(t *testing.T) {
kk := struct {
ID int8
Name string
}{
ID: 1,
Name: "hello",
}
if GetObjFieldByName(kk, "ID").(int8) != 1 {
t.Fatal("value is not ok")
}
if GetObjFieldByName(kk, "Name").(string) != "hello" {
t.Fatal("value is not ok")
}
pKK := &kk
if GetObjFieldByName(pKK, "ID").(int8) != 1 {
t.Fatal("value is not ok")
}
if GetObjFieldByName(pKK, "Name").(string) != "hello" {
t.Fatal("value is not ok")
}
SetObjFieldByName(pKK, "ID", int8(100))
SetObjFieldByName(pKK, "Name", "world")
if GetObjFieldByName(pKK, "ID").(int8) != 100 {
t.Fatal("value is not ok")
}
if GetObjFieldByName(pKK, "Name").(string) != "world" {
t.Fatal("value is not ok")
}
t.Log(utils.Format4Output(pKK, false))
}