Skip to content

Commit

Permalink
💥 refactor: rename the package stdutil to goinfo, and remove some rep…
Browse files Browse the repository at this point in the history
…eated util func
  • Loading branch information
inhere committed Jul 18, 2023
1 parent 0155b13 commit 592b2a1
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 550 deletions.
34 changes: 34 additions & 0 deletions goinfo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# GoInfo

`goutil/goinfo` provide some useful info for golang.

> Github: https://github.com/gookit/goutil
## Install

```bash
go get github.com/gookit/goutil/goinfo
```

## Go docs

- [Go docs](https://pkg.go.dev/github.com/gookit/goutil)

## Usage

```go
gover := goinfo.GoVersion() // eg: "1.15.6"

```

## Testings

```shell
go test -v ./goinfo/...
```

Test limit by regexp:

```shell
go test -v -run ^TestSetByKeys ./goinfo/...
```
17 changes: 9 additions & 8 deletions stdutil/gofunc.go → goinfo/gofunc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stdutil
package goinfo

import (
"reflect"
Expand All @@ -10,7 +10,7 @@ import (

// FullFcName struct.
type FullFcName struct {
// FullName eg: github.com/gookit/goutil/stdutil.PanicIf
// FullName eg: github.com/gookit/goutil/goinfo.PanicIf
FullName string
pkgPath string
pkgName string
Expand All @@ -32,13 +32,13 @@ func (ffn *FullFcName) Parse() {
ffn.pkgPath += ffn.pkgName
}

// PkgPath string get. eg: github.com/gookit/goutil/stdutil
// PkgPath string get. eg: github.com/gookit/goutil/goinfo
func (ffn *FullFcName) PkgPath() string {
ffn.Parse()
return ffn.pkgPath
}

// PkgName string get. eg: stdutil
// PkgName string get. eg: goinfo
func (ffn *FullFcName) PkgName() string {
ffn.Parse()
return ffn.pkgName
Expand All @@ -59,8 +59,8 @@ func (ffn *FullFcName) String() string {
//
// eg:
//
// // OUTPUT: github.com/gookit/goutil/stdutil.PanicIf
// stdutil.FuncName(stdutil.PkgName)
// // OUTPUT: github.com/gookit/goutil/goinfo.PanicIf
// goinfo.FuncName(goinfo.PkgName)
func FuncName(fn any) string {
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
}
Expand All @@ -75,12 +75,13 @@ func CutFuncName(fullFcName string) (pkgPath, shortFnName string) {
//
// Usage:
//
// fullFcName := stdutil.FuncName(fn)
// pgkName := stdutil.PkgName(fullFcName)
// fullFcName := goinfo.FuncName(fn)
// pgkName := goinfo.PkgName(fullFcName)
func PkgName(fullFcName string) string {
for {
lastPeriod := strings.LastIndex(fullFcName, ".")
lastSlash := strings.LastIndex(fullFcName, "/")

if lastPeriod > lastSlash {
fullFcName = fullFcName[:lastPeriod]
} else {
Expand Down
47 changes: 47 additions & 0 deletions goinfo/gofunc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package goinfo_test

import (
"testing"

"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/goinfo"
"github.com/gookit/goutil/testutil/assert"
)

func TestFuncName(t *testing.T) {
name := goinfo.FuncName(goinfo.PkgName)
assert.Eq(t, "github.com/gookit/goutil/goinfo.PkgName", name)
}

func TestPkgName(t *testing.T) {
name := goinfo.PkgName(goinfo.FuncName(goinfo.GetCallerInfo))
assert.Eq(t, "github.com/gookit/goutil/goinfo", name)
}

func TestFullFcName_Parse(t *testing.T) {
fullName := goinfo.FuncName(goinfo.GetCallerInfo)

ffn := goinfo.FullFcName{FullName: fullName}
ffn.Parse()
assert.Eq(t, fullName, ffn.String())
assert.Eq(t, "goinfo", ffn.PkgName())
assert.Eq(t, "GetCallerInfo", ffn.FuncName())
assert.Eq(t, "github.com/gookit/goutil/goinfo", ffn.PkgPath())
dump.P(ffn)

st := goinfo.FullFcName{}
fullName = goinfo.FuncName(st.FuncName)

ffn = goinfo.FullFcName{FullName: fullName}
ffn.Parse()
assert.Eq(t, "(*FullFcName).FuncName-fm", ffn.FuncName())
dump.P(ffn)
}

func TestCutFuncName(t *testing.T) {
fullName := goinfo.FuncName(goinfo.GetCallerInfo)

pkgPath, funcName := goinfo.CutFuncName(fullName)
assert.Eq(t, "GetCallerInfo", funcName)
assert.Eq(t, "github.com/gookit/goutil/goinfo", pkgPath)
}
9 changes: 9 additions & 0 deletions goinfo/goinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Package goinfo provide some standard util functions for go.
package goinfo

import "runtime"

// GoVersion get go runtime version. eg: "1.18.2"
func GoVersion() string {
return runtime.Version()[2:]
}
12 changes: 12 additions & 0 deletions goinfo/goinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package goinfo_test

import (
"testing"

"github.com/gookit/goutil/goinfo"
"github.com/gookit/goutil/testutil/assert"
)

func TestGoVersion(t *testing.T) {
assert.NotEmpty(t, goinfo.GoVersion())
}
7 changes: 4 additions & 3 deletions stdutil/stack.go → goinfo/stack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stdutil
package goinfo

import (
"path"
Expand Down Expand Up @@ -42,7 +42,7 @@ func GetCallStacks(all bool) []byte {
//
// returns:
//
// github.com/gookit/goutil/stdutil_test.someFunc2(),stack_test.go:26
// github.com/gookit/goutil/goinfo_test.someFunc2(),stack_test.go:26
func GetCallerInfo(skip int) string {
skip++ // ignore current func
cs := GetCallersInfo(skip, skip+1)
Expand All @@ -62,6 +62,7 @@ func SimpleCallersInfo(skip, num int) []string {

// GetCallersInfo returns an array of strings containing
// the func name, file and line number of each stack frame leading.
//
// NOTICE: max should > skip
func GetCallersInfo(skip, max int) []string {
var (
Expand Down Expand Up @@ -93,7 +94,7 @@ func GetCallersInfo(skip, max int) []string {
if strings.ContainsRune(file, '/') {
name = fc.Name()
file = path.Base(file)
// eg: github.com/gookit/goutil/stdutil_test.someFunc2(),stack_test.go:26
// eg: github.com/gookit/goutil/goinfo_test.someFunc2(),stack_test.go:26
callers = append(callers, name+"(),"+file+":"+strconv.Itoa(line))
}

Expand Down
45 changes: 45 additions & 0 deletions goinfo/stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package goinfo_test

import (
"fmt"
"testing"

"github.com/gookit/goutil/goinfo"
"github.com/gookit/goutil/testutil/assert"
)

func TestGetCallStacks(t *testing.T) {
msg := goinfo.GetCallStacks(false)
fmt.Println(string(msg))

fmt.Println("-------------full stacks-------------")
msg = goinfo.GetCallStacks(true)
fmt.Println(string(msg))
}

func TestGetCallersInfo(t *testing.T) {
cs := someFunc1()
assert.Len(t, cs, 1)
assert.Contains(t, cs[0], "goutil/goinfo_test.someFunc1(),stack_test.go")

cs = someFunc2()
assert.Len(t, cs, 1)
assert.Contains(t, cs[0], "goutil/goinfo_test.someFunc2(),stack_test.go")

loc := someFunc3()
assert.NotEmpty(t, loc)
assert.Contains(t, loc, "goutil/goinfo_test.someFunc3(),stack_test.go")
// dump.P(cs)
}

func someFunc1() []string {
return goinfo.GetCallersInfo(1, 2)
}

func someFunc2() []string {
return goinfo.SimpleCallersInfo(1, 1)
}

func someFunc3() string {
return goinfo.GetCallerInfo(1)
}
28 changes: 0 additions & 28 deletions stdutil/README.md

This file was deleted.

Loading

0 comments on commit 592b2a1

Please sign in to comment.