Skip to content

Commit

Permalink
feat: add fn package
Browse files Browse the repository at this point in the history
  • Loading branch information
miaolz123 committed Sep 24, 2020
1 parent eb8a667 commit e997a40
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 10 deletions.
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "github.com/gxxgle/go-utils/config"
_ "github.com/gxxgle/go-utils/conver"
_ "github.com/gxxgle/go-utils/db"
_ "github.com/gxxgle/go-utils/fn"
_ "github.com/gxxgle/go-utils/http"
_ "github.com/gxxgle/go-utils/json"
_ "github.com/gxxgle/go-utils/log"
Expand Down
22 changes: 22 additions & 0 deletions fn/name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fn

import (
"fmt"
"reflect"
"runtime"
"strings"
)

func Name(v interface{}) (string, error) {
value := reflect.ValueOf(v)
if value.Kind() != reflect.Func {
return "", fmt.Errorf("%T is not func type", v)
}

name := runtime.FuncForPC(value.Pointer()).Name()
if i := strings.LastIndex(name, "."); i != -1 {
return strings.TrimSuffix(name[i+1:], "-fm"), nil
}

return "", fmt.Errorf("not found func name")
}
46 changes: 46 additions & 0 deletions fn/name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fn

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

type s struct{}

func f1(int) {}
func f2(...int) {}
func F3(int) {}
func F4(...int) {}
func (s) f1(int) {}
func (s) f2(...int) {}
func (s) F3(int) {}
func (s) F4(...int) {}

func TestName(t *testing.T) {
type check struct {
input interface{}
expected string
err error
}

checks := []check{
{input: f1, expected: "f1", err: nil},
{input: f2, expected: "f2", err: nil},
{input: F3, expected: "F3", err: nil},
{input: F4, expected: "F4", err: nil},
{input: s{}.f1, expected: "f1", err: nil},
{input: s{}.f2, expected: "f2", err: nil},
{input: s{}.F3, expected: "F3", err: nil},
{input: s{}.F4, expected: "F4", err: nil},
{input: 1, expected: "", err: fmt.Errorf("int is not func type")},
{input: true, expected: "", err: fmt.Errorf("bool is not func type")},
}

for _, c := range checks {
output, err := Name(c.input)
require.Equal(t, c.expected, output)
require.Equal(t, c.err, err)
}
}
24 changes: 24 additions & 0 deletions fn/timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fn

import (
"context"
"time"
)

func Timeout(ctx context.Context, fn func() error, timeout time.Duration) error {
errChan := make(chan error)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

go func() {
errChan <- fn()
close(errChan)
}()

select {
case err := <-errChan:
return err
case <-ctx.Done():
return ctx.Err()
}
}
60 changes: 60 additions & 0 deletions fn/timeout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fn

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
)

type stringErr string

func (e stringErr) Error() string {
return string(e)
}

func TestTimeout(t *testing.T) {
genFunc := func(d time.Duration, err error) func() error {
return func() error {
time.Sleep(d)
return err
}
}

type check struct {
input func() error
timeout time.Duration
expected error
}

checks := []check{
{
input: genFunc(time.Millisecond, nil),
timeout: time.Millisecond * 2,
expected: nil,
},
{
input: genFunc(time.Millisecond, stringErr("func err")),
timeout: time.Millisecond * 2,
expected: stringErr("func err"),
},
{
input: genFunc(time.Millisecond*2, stringErr("func err")),
timeout: time.Millisecond,
expected: context.DeadlineExceeded,
},
}

for i, c := range checks {
err := Timeout(context.Background(), c.input, c.timeout)
require.Equal(t, c.expected, err, "#%d check", i)
}

ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * 50)
cancel()
}()
require.Equal(t, context.Canceled, Timeout(ctx, genFunc(time.Minute, nil), time.Hour))
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ go 1.13

require (
github.com/bsm/redislock v0.5.0
github.com/doug-martin/goqu/v9 v9.9.0
github.com/doug-martin/goqu/v9 v9.10.0
github.com/go-playground/validator/v10 v10.3.0
github.com/go-redis/redis/v7 v7.4.0
github.com/go-resty/resty/v2 v2.3.0
github.com/go-sql-driver/mysql v1.5.0
github.com/json-iterator/go v1.1.10
github.com/patrickmn/go-cache v0.0.0-20191004192108-46f407853014
github.com/phuslu/log v1.0.40
github.com/phuslu/log v1.0.44
github.com/robfig/cron/v3 v3.0.1
github.com/shopspring/decimal v1.2.0
github.com/stretchr/testify v1.6.1
github.com/tidwall/gjson v1.6.1
golang.org/x/net v0.0.0-20200904194848-62affa334b73
golang.org/x/net v0.0.0-20200923182212-328152dc79b1
xorm.io/xorm v1.0.5
)
17 changes: 10 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20200206145737-bbfc9a55622e/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/doug-martin/goqu/v9 v9.9.0 h1:dF0Wcn6O/ccuK0w8U62Wa0HQskWOgex8IjyPBzujzNg=
github.com/doug-martin/goqu/v9 v9.9.0/go.mod h1:zx5/YoiHux3wn7477GnI3PXzKyKpLKu32Teo9U4yCFE=
github.com/doug-martin/goqu/v9 v9.10.0 h1:ggTSAwshc5nubbFN7Q8Or1/Xzv+x8YTLCyv6CpBb9DM=
github.com/doug-martin/goqu/v9 v9.10.0/go.mod h1:zx5/YoiHux3wn7477GnI3PXzKyKpLKu32Teo9U4yCFE=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
Expand Down Expand Up @@ -69,8 +69,8 @@ github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/patrickmn/go-cache v0.0.0-20191004192108-46f407853014 h1:ukphaVZ+JD1N6LJM5wnD/DKmn0nq3J9u+ytBCG9zQpI=
github.com/patrickmn/go-cache v0.0.0-20191004192108-46f407853014/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/phuslu/log v1.0.40 h1:qnjyjItJVopmDxiglMZKEA2OSqGrh/+Y+I2VXhnDR6E=
github.com/phuslu/log v1.0.40/go.mod h1:BSFbUpGk1VoydawCb63LBAGwv/q6qbaItgrBpehbPsk=
github.com/phuslu/log v1.0.44 h1:Qv/8fAyKqKTAq7D21NLLee+OlVW4jFx96Du5qzjXEmc=
github.com/phuslu/log v1.0.44/go.mod h1:BSFbUpGk1VoydawCb63LBAGwv/q6qbaItgrBpehbPsk=
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/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
Expand All @@ -80,8 +80,9 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/tidwall/gjson v1.6.1 h1:LRbvNuNuvAiISWg6gxLEFuCe72UKy5hDqhxW/8183ws=
Expand All @@ -104,8 +105,8 @@ golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200923182212-328152dc79b1 h1:Iu68XRPd67wN4aRGGWwwq6bZo/25jR6uu52l/j2KkUE=
golang.org/x/net v0.0.0-20200923182212-328152dc79b1/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -132,6 +133,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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=
xorm.io/builder v0.3.7 h1:2pETdKRK+2QG4mLX4oODHEhn5Z8j1m8sXa7jfu+/SZI=
xorm.io/builder v0.3.7/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
xorm.io/xorm v1.0.5 h1:LRr5PfOUb4ODPR63YwbowkNDwcolT2LnkwP/TUaMaB0=
Expand Down

0 comments on commit e997a40

Please sign in to comment.