Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version v2.3.0 #2392

Merged
merged 5 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions cmd/gf/internal/cmd/cmd_fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type cFix struct {

type cFixInput struct {
g.Meta `name:"fix"`
Path string `name:"path" brief:"directory path, it uses current working directory in default"`
}

type cFixOutput struct{}
Expand All @@ -32,12 +33,15 @@ type cFixItem struct {
func (c cFix) Index(ctx context.Context, in cFixInput) (out *cFixOutput, err error) {
mlog.Print(`start auto fixing...`)
defer mlog.Print(`done!`)
err = c.doFix()
if in.Path == "" {
in.Path = gfile.Pwd()
}
err = c.doFix(in)
return
}

func (c cFix) doFix() (err error) {
version, err := c.getVersion()
func (c cFix) doFix(in cFixInput) (err error) {
version, err := c.getVersion(in)
if err != nil {
mlog.Fatal(err)
}
Expand Down Expand Up @@ -83,10 +87,10 @@ func (c cFix) doFixV23(version string) error {
return gfile.ReplaceDirFunc(replaceFunc, ".", "*.go", true)
}

func (c cFix) getVersion() (string, error) {
func (c cFix) getVersion(in cFixInput) (string, error) {
var (
err error
path = "go.mod"
path = gfile.Join(in.Path, "go.mod")
version string
)
if !gfile.Exists(path) {
Expand All @@ -95,7 +99,7 @@ func (c cFix) getVersion() (string, error) {
err = gfile.ReadLines(path, func(line string) error {
array := gstr.SplitAndTrim(line, " ")
if len(array) > 0 {
if array[0] == gfPackage {
if gstr.HasPrefix(array[0], gfPackage) {
version = array[1]
}
}
Expand Down
51 changes: 33 additions & 18 deletions cmd/gf/internal/cmd/cmd_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type cUp struct {
}

const (
gfPackage = `github.com/gogf/gf/v2`
gfPackage = `github.com/gogf/gf/`
cUpEg = `
gf up
gf up -a
Expand All @@ -46,20 +46,19 @@ type cUpInput struct {
type cUpOutput struct{}

func (c cUp) Index(ctx context.Context, in cUpInput) (out *cUpOutput, err error) {
defer mlog.Print(`done!`)
defer func() {
if err == nil {
mlog.Print(`done!`)
}
}()

if in.All {
in.Cli = true
in.Fix = true
}
if err = c.doUpgradeVersion(ctx); err != nil {
if err = c.doUpgradeVersion(ctx, in); err != nil {
return nil, err
}
if in.Fix {
if err = c.doAutoFixing(ctx); err != nil {
return nil, err
}
}
//if in.Cli {
// if err = c.doUpgradeCLI(ctx); err != nil {
// return nil, err
Expand All @@ -68,34 +67,49 @@ func (c cUp) Index(ctx context.Context, in cUpInput) (out *cUpOutput, err error)
return
}

func (c cUp) doUpgradeVersion(ctx context.Context) (err error) {
func (c cUp) doUpgradeVersion(ctx context.Context, in cUpInput) (err error) {
mlog.Print(`start upgrading version...`)

type Package struct {
Name string
Version string
}

var (
dir = gfile.Pwd()
temp string
path = gfile.Join(dir, "go.mod")
)
for {
if gfile.Exists(path) {
var packages []string
var packages []Package
err = gfile.ReadLines(path, func(line string) error {
line = gstr.Trim(line)
if gstr.HasPrefix(line, gfPackage) {
pkg := gstr.Explode(" ", line)[0]
packages = append(packages, pkg)
array := gstr.SplitAndTrim(line, " ")
packages = append(packages, Package{
Name: array[0],
Version: array[1],
})
}
return nil
})
if err != nil {
return
}
for _, pkg := range packages {
mlog.Printf(`upgrading %s`, pkg)
command := fmt.Sprintf(`go get -u %s@latest`, pkg)
mlog.Printf(`upgrading "%s" from "%s" to "latest"`, pkg.Name, pkg.Version)
command := fmt.Sprintf(`go get -u %s@latest`, pkg.Name)
if err = gproc.ShellRun(ctx, command); err != nil {
return
}
mlog.Print()
}
if in.Fix {
if err = c.doAutoFixing(ctx, dir); err != nil {
return err
}
mlog.Print()
}
return
}
Expand All @@ -110,12 +124,13 @@ func (c cUp) doUpgradeVersion(ctx context.Context) (err error) {

func (c cUp) doUpgradeCLI(ctx context.Context) (err error) {
mlog.Print(`start upgrading cli...`)

return
}

func (c cUp) doAutoFixing(ctx context.Context) (err error) {
mlog.Print(`start auto fixing...`)
err = cFix{}.doFix()
func (c cUp) doAutoFixing(ctx context.Context, dirPath string) (err error) {
mlog.Printf(`auto fixing path "%s"...`, dirPath)
err = cFix{}.doFix(cFixInput{
Path: dirPath,
})
return
}
5 changes: 4 additions & 1 deletion cmd/gf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func main() {
if err != nil {
panic(err)
}
command.Run(ctx)
err = command.RunWithError(ctx)
if err != nil {
panic(err)
}
}

// zsh alias "git fetch" conflicts checks.
Expand Down
131 changes: 131 additions & 0 deletions container/gvar/gvar_vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gvar

import (
"github.com/gogf/gf/v2/util/gconv"
)

// Vars is a slice of *Var.
type Vars []*Var

// Strings converts and returns `vs` as []string.
func (vs Vars) Strings() (s []string) {
for _, v := range vs {
s = append(s, v.String())
}
return s
}

// Interfaces converts and returns `vs` as []interface{}.
func (vs Vars) Interfaces() (s []interface{}) {
for _, v := range vs {
s = append(s, v.Val())
}
return s
}

// Float32s converts and returns `vs` as []float32.
func (vs Vars) Float32s() (s []float32) {
for _, v := range vs {
s = append(s, v.Float32())
}
return s
}

// Float64s converts and returns `vs` as []float64.
func (vs Vars) Float64s() (s []float64) {
for _, v := range vs {
s = append(s, v.Float64())
}
return s
}

// Ints converts and returns `vs` as []Int.
func (vs Vars) Ints() (s []int) {
for _, v := range vs {
s = append(s, v.Int())
}
return s
}

// Int8s converts and returns `vs` as []int8.
func (vs Vars) Int8s() (s []int8) {
for _, v := range vs {
s = append(s, v.Int8())
}
return s
}

// Int16s converts and returns `vs` as []int16.
func (vs Vars) Int16s() (s []int16) {
for _, v := range vs {
s = append(s, v.Int16())
}
return s
}

// Int32s converts and returns `vs` as []int32.
func (vs Vars) Int32s() (s []int32) {
for _, v := range vs {
s = append(s, v.Int32())
}
return s
}

// Int64s converts and returns `vs` as []int64.
func (vs Vars) Int64s() (s []int64) {
for _, v := range vs {
s = append(s, v.Int64())
}
return s
}

// Uints converts and returns `vs` as []uint.
func (vs Vars) Uints() (s []uint) {
for _, v := range vs {
s = append(s, v.Uint())
}
return s
}

// Uint8s converts and returns `vs` as []uint8.
func (vs Vars) Uint8s() (s []uint8) {
for _, v := range vs {
s = append(s, v.Uint8())
}
return s
}

// Uint16s converts and returns `vs` as []uint16.
func (vs Vars) Uint16s() (s []uint16) {
for _, v := range vs {
s = append(s, v.Uint16())
}
return s
}

// Uint32s converts and returns `vs` as []uint32.
func (vs Vars) Uint32s() (s []uint32) {
for _, v := range vs {
s = append(s, v.Uint32())
}
return s
}

// Uint64s converts and returns `vs` as []uint64.
func (vs Vars) Uint64s() (s []uint64) {
for _, v := range vs {
s = append(s, v.Uint64())
}
return s
}

// Scan converts `vs` to []struct/[]*struct.
func (vs Vars) Scan(pointer interface{}, mapping ...map[string]string) error {
return gconv.Structs(vs.Interfaces(), pointer, mapping...)
}
60 changes: 60 additions & 0 deletions container/gvar/gvar_z_unit_vars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gvar_test

import (
"testing"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
)

func TestVars(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var vs = gvar.Vars{
gvar.New(1),
gvar.New(2),
gvar.New(3),
}
t.AssertEQ(vs.Strings(), []string{"1", "2", "3"})
t.AssertEQ(vs.Interfaces(), []interface{}{1, 2, 3})
t.AssertEQ(vs.Float32s(), []float32{1, 2, 3})
t.AssertEQ(vs.Float64s(), []float64{1, 2, 3})
t.AssertEQ(vs.Ints(), []int{1, 2, 3})
t.AssertEQ(vs.Int8s(), []int8{1, 2, 3})
t.AssertEQ(vs.Int16s(), []int16{1, 2, 3})
t.AssertEQ(vs.Int32s(), []int32{1, 2, 3})
t.AssertEQ(vs.Int64s(), []int64{1, 2, 3})
t.AssertEQ(vs.Uints(), []uint{1, 2, 3})
t.AssertEQ(vs.Uint8s(), []uint8{1, 2, 3})
t.AssertEQ(vs.Uint16s(), []uint16{1, 2, 3})
t.AssertEQ(vs.Uint32s(), []uint32{1, 2, 3})
t.AssertEQ(vs.Uint64s(), []uint64{1, 2, 3})
})
}

func TestVars_Scan(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type User struct {
Id int
Name string
}
var vs = gvar.Vars{
gvar.New(g.Map{"id": 1, "name": "john"}),
gvar.New(g.Map{"id": 2, "name": "smith"}),
}
var users []User
err := vs.Scan(&users)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].Id, 1)
t.Assert(users[0].Name, "john")
t.Assert(users[1].Id, 2)
t.Assert(users[1].Name, "smith")
})
}
8 changes: 4 additions & 4 deletions contrib/nosql/redis/redis_group_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (r GroupHash) HMSet(ctx context.Context, key string, fields map[string]inte
// will return a list of nil values.
//
// https://redis.io/commands/hmget/
func (r GroupHash) HMGet(ctx context.Context, key string, fields ...string) ([]*gvar.Var, error) {
func (r GroupHash) HMGet(ctx context.Context, key string, fields ...string) (gvar.Vars, error) {
v, err := r.redis.Do(ctx, "HMGet", append([]interface{}{key}, gconv.Interfaces(fields)...)...)
return v.Vars(), err
}
Expand All @@ -178,7 +178,7 @@ func (r GroupHash) HKeys(ctx context.Context, key string) ([]string, error) {
// HVals return all values in the hash stored at key.
//
// https://redis.io/commands/hvals/
func (r GroupHash) HVals(ctx context.Context, key string) ([]*gvar.Var, error) {
func (r GroupHash) HVals(ctx context.Context, key string) (gvar.Vars, error) {
v, err := r.redis.Do(ctx, "HVals", key)
return v.Vars(), err
}
Expand All @@ -188,7 +188,7 @@ func (r GroupHash) HVals(ctx context.Context, key string) ([]*gvar.Var, error) {
// so the length of the reply is twice the size of the hash.
//
// https://redis.io/commands/hgetall/
func (r GroupHash) HGetAll(ctx context.Context, key string) (map[string]*gvar.Var, error) {
func (r GroupHash) HGetAll(ctx context.Context, key string) (*gvar.Var, error) {
v, err := r.redis.Do(ctx, "HGetAll", key)
return v.MapStrVar(), err
return v, err
}
Loading