Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Oct 19, 2023
1 parent 2619272 commit 4ed902b
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 2 deletions.
55 changes: 55 additions & 0 deletions utils/xconv/conv_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package xconv

import (
"github.com/dobyte/due/v2/encoding/json"
"reflect"
)

func Json(any interface{}) string {
isJson := func(s string) bool {
l := len(s)
return l >= 2 && ((s[0] == '{' && s[l-1] == '}') || (s[0] == '[' && s[l-1] == ']'))
}

switch v := any.(type) {
case string:
if isJson(v) {
return v
}
case *string:
if isJson(*v) {
return *v
}
case []byte:
if s := BytesToString(v); isJson(s) {
return s
}
case *[]byte:
if s := BytesToString(*v); isJson(s) {
return s
}
default:
var (
rv = reflect.ValueOf(any)
kind = rv.Kind()
)

for kind == reflect.Ptr {
rv = rv.Elem()
kind = rv.Kind()
}

switch kind {
case reflect.String:
if s := rv.String(); isJson(s) {
return s
}
case reflect.Map, reflect.Array, reflect.Slice, reflect.Struct:
if b, err := json.Marshal(v); err == nil {
return BytesToString(b)
}
}
}

return ""
}
35 changes: 33 additions & 2 deletions utils/xconv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ func TestDuration(t *testing.T) {
}

func TestStrings(t *testing.T) {
any := []int64{1, 2, 3, 4}
t.Log(xconv.Strings(any))
a := []int64{1, 2, 3, 4}
t.Log(xconv.Strings(a))
}

func TestJson(t *testing.T) {
t.Log(xconv.Json("{}"))
t.Log(xconv.Json(`{"id":1,"name":"fuxiao"}`))
t.Log(xconv.Json("[]"))
t.Log(xconv.Json(`[{"id":1,"name":"fuxiao"}]`))
t.Log(xconv.Json(map[string]interface{}{
"id": 1,
"name": "fuxiao",
}))
t.Log(xconv.Json([]map[string]interface{}{{
"id": 1,
"name": "fuxiao",
}}))
t.Log(xconv.Json(struct {
ID int `json:"id"`
Name string `json:"name"`
}{
ID: 1,
Name: "fuxiao",
}))
t.Log(xconv.Json([]struct {
ID int `json:"id"`
Name string `json:"name"`
}{
{
ID: 1,
Name: "fuxiao",
},
}))
}
63 changes: 63 additions & 0 deletions utils/xrand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package xrand
import (
"github.com/dobyte/due/v2/log"
"github.com/dobyte/due/v2/utils/xtime"
"math"
"math/rand"
"strconv"
"strings"
)

const (
Expand Down Expand Up @@ -116,3 +119,63 @@ func Float64(min, max float64) float64 {

return min + rand.Float64()*(max-min)
}

// Lucky 根据概率抽取幸运值
func Lucky(probability float64, base ...float64) bool {
b := float64(100)
if len(base) > 0 {
b = base[0]
}

if probability >= b {
return true
}

str := strconv.FormatFloat(probability, 'f', -1, 64)
scale := float64(0)

if i := strings.IndexByte(str, '.'); i > 0 {
scale = math.Pow10(len(str) - i - 1)
}

return Int64(1, int64(b*scale)) <= int64(probability*scale)
}

// Weight 权重随机
func Weight(list []interface{}, fn func(v interface{}) float64) int {
if len(list) == 0 {
return -1
}

total := float64(0)
scale := float64(0)

for _, item := range list {
weight := fn(item)
str := strconv.FormatFloat(weight, 'f', -1, 64)

if i := strings.IndexByte(str, '.'); i > 0 {
scale = math.Max(scale, math.Pow10(len(str)-i-1))
}

total += weight
}

sum := int64(total * scale)

if sum == 0 {
return Int(1, len(list))
}

weight := Int64(1, sum)
acc := int64(0)

for i, item := range list {
acc += int64(fn(item) * scale)
if weight <= acc {
return i
}
}

return Int(1, len(list))
}
19 changes: 19 additions & 0 deletions utils/xrand/rand_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xrand_test

import (
"github.com/dobyte/due/v2/utils/xconv"
"github.com/dobyte/due/v2/utils/xrand"
"testing"
)
Expand All @@ -20,3 +21,21 @@ func Test_Int(t *testing.T) {
func Test_Float32(t *testing.T) {
t.Log(xrand.Float32(-50, 5))
}

func TestLucky(t *testing.T) {
t.Log(xrand.Lucky(50.201222))
t.Log(xrand.Lucky(0.201222))
t.Log(xrand.Lucky(50))
}

func TestWeight(t *testing.T) {
i := xrand.Weight([]interface{}{
50,
20.3,
39.7,
}, func(v interface{}) float64 {
return xconv.Float64(v)
})

t.Log(i)
}

0 comments on commit 4ed902b

Please sign in to comment.