Skip to content

Commit

Permalink
Add Tools Module and add Ternary Operator Apis.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChangedenCZD committed Jan 13, 2022
1 parent 4632671 commit d56228b
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/guangzhou-meta/go-lib/tools

go 1.16

require github.com/stretchr/testify v1.7.0
11 changes: 11 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
97 changes: 97 additions & 0 deletions tools/ternary_operator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package tools

func TernaryOperator(condition bool, trueResult interface{}, falseResult interface{}) interface{} {
if condition {
return trueResult
}
return falseResult
}

func TernaryOperatorInt(condition bool, trueResult int, falseResult int) int {
return TernaryOperator(condition, trueResult, falseResult).(int)
}

func TernaryOperatorInt8(condition bool, trueResult int8, falseResult int8) int8 {
return TernaryOperator(condition, trueResult, falseResult).(int8)
}

func TernaryOperatorInt16(condition bool, trueResult int16, falseResult int16) int16 {
return TernaryOperator(condition, trueResult, falseResult).(int16)
}

func TernaryOperatorInt32(condition bool, trueResult int32, falseResult int32) int32 {
return TernaryOperator(condition, trueResult, falseResult).(int32)
}

func TernaryOperatorInt64(condition bool, trueResult int64, falseResult int64) int64 {
return TernaryOperator(condition, trueResult, falseResult).(int64)
}

func TernaryOperatorUInt(condition bool, trueResult uint, falseResult uint) uint {
return TernaryOperator(condition, trueResult, falseResult).(uint)
}

func TernaryOperatorUInt8(condition bool, trueResult uint8, falseResult uint8) uint8 {
return TernaryOperator(condition, trueResult, falseResult).(uint8)
}

func TernaryOperatorUInt16(condition bool, trueResult uint16, falseResult uint16) uint16 {
return TernaryOperator(condition, trueResult, falseResult).(uint16)
}

func TernaryOperatorUInt32(condition bool, trueResult uint32, falseResult uint32) uint32 {
return TernaryOperator(condition, trueResult, falseResult).(uint32)
}

func TernaryOperatorUInt64(condition bool, trueResult uint64, falseResult uint64) uint64 {
return TernaryOperator(condition, trueResult, falseResult).(uint64)
}

func TernaryOperatorFloat32(condition bool, trueResult float32, falseResult float32) float32 {
return TernaryOperator(condition, trueResult, falseResult).(float32)
}

func TernaryOperatorFloat64(condition bool, trueResult float64, falseResult float64) float64 {
return TernaryOperator(condition, trueResult, falseResult).(float64)
}

func TernaryOperatorString(condition bool, trueResult string, falseResult string) string {
return TernaryOperator(condition, trueResult, falseResult).(string)
}

type TernaryOperatorNestWrap struct {
condition TernaryOperatorNestConditionFunc
trueResult interface{}
falseResult interface{}
}

type TernaryOperatorNestConditionFunc func() bool

func TernaryOperatorNest(condition TernaryOperatorNestConditionFunc, trueResult interface{}, falseResult interface{}) *TernaryOperatorNestWrap {
return &TernaryOperatorNestWrap{
condition: condition,
trueResult: trueResult,
falseResult: falseResult,
}
}

func resolveTernaryOperatorNestResult(result interface{}) interface{} {
if r, ok := result.(*TernaryOperatorNestWrap); ok {
return r.Result()
}
return result
}

func (t *TernaryOperatorNestWrap) Result() interface{} {
if t.condition == nil {
return nil
}
return TernaryOperator(
t.condition(),
func() interface{} {
return resolveTernaryOperatorNestResult(t.trueResult)
},
func() interface{} {
return resolveTernaryOperatorNestResult(t.falseResult)
}).(func() interface{})()
}
86 changes: 86 additions & 0 deletions tools/ternary_operator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package tools

import (
"math/rand"
"testing"
"time"
)

import (
"github.com/stretchr/testify/assert"
)

func randomCondition() bool {
t := time.Now().UnixNano() / 1e6
return t%2 == 1
}

func TestTernaryOperator(t *testing.T) {
condition := randomCondition()
trueResult := "true"
falseResult := "false"
var actual string
if condition {
actual = trueResult
} else {
actual = falseResult
}
result := TernaryOperator(condition, trueResult, falseResult)
assert.Equal(t, actual, result)
}

func getNestRandomCondition(f int64, n int32) bool {
rand.Seed(time.Now().UnixNano() / f)
randomNum := time.Duration(rand.Int31n(n))
time.Sleep(randomNum * time.Millisecond)
return randomCondition()
}

func TestTernaryOperatorNest(t *testing.T) {
var condition0 bool
var condition1 bool
var condition2 bool

trueResult0 := "true"
falseResult0 := "false"

trueResult1 := 48
falseResult1 := 3.14

result := TernaryOperatorNest(
func() bool {
condition0 = getNestRandomCondition(1e6, 2)
return condition0
},
trueResult0,
TernaryOperatorNest(
func() bool {
condition1 = getNestRandomCondition(1e7, 4)
return condition1
},
trueResult1,
TernaryOperatorNest(
func() bool {
condition2 = getNestRandomCondition(1e6, 6)
return condition2
},
falseResult0,
falseResult1,
),
),
).Result()

if condition0 {
assert.Equal(t, trueResult0, result)
return
}
if condition1 {
assert.Equal(t, trueResult1, result)
return
}
if condition2 {
assert.Equal(t, falseResult0, result)
return
}
assert.Equal(t, falseResult1, result)
}

0 comments on commit d56228b

Please sign in to comment.