Skip to content

Commit

Permalink
Replace go-funk with lo
Browse files Browse the repository at this point in the history
  • Loading branch information
koho authored Feb 22, 2024
1 parent 10db5b0 commit 4b61e30
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 43 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/lxn/win v0.0.0-20210218163916-a377121e959e
github.com/miekg/dns v1.1.58
github.com/samber/lo v1.39.0
github.com/thoas/go-funk v0.9.3
golang.org/x/sys v0.17.0
golang.org/x/text v0.14.0
gopkg.in/ini.v1 v1.67.0
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand All @@ -133,8 +132,6 @@ github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 h1:89CEmDvlq/F7S
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b h1:fj5tQ8acgNUr6O8LEplsxDhUIe2573iLkJc+PqnzZTI=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
github.com/xtaci/lossyconn v0.0.0-20200209145036-adba10fffc37 h1:EWU6Pktpas0n8lLQwDsRyZfmkPeRbdgPtW609es+/9E=
Expand Down Expand Up @@ -260,7 +257,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
9 changes: 5 additions & 4 deletions pkg/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"fmt"
"os"
"slices"
"strings"

"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/config/v1"
frputil "github.com/fatedier/frp/pkg/util/util"
"github.com/thoas/go-funk"
"github.com/samber/lo"
"gopkg.in/ini.v1"

"github.com/koho/frpmgr/pkg/consts"
Expand Down Expand Up @@ -366,7 +367,7 @@ func (conf *ClientConfig) DeleteItem(index int) {

func (conf *ClientConfig) AddItem(item interface{}) bool {
if proxy, ok := item.(*Proxy); ok {
if !funk.Contains(conf.Proxies, func(p *Proxy) bool { return p.Name == proxy.Name }) {
if !slices.ContainsFunc(conf.Proxies, func(p *Proxy) bool { return p.Name == proxy.Name }) {
conf.Proxies = append(conf.Proxies, proxy)
return true
}
Expand Down Expand Up @@ -444,7 +445,7 @@ func (conf *ClientConfig) Complete(read bool) {
proxy.Complete()
// Check proxy status
if read && len(conf.Start) > 0 {
proxy.Disabled = !funk.Subset(proxy.GetAlias(), conf.Start)
proxy.Disabled = !lo.Every(conf.Start, proxy.GetAlias())
}
}
if !read {
Expand Down Expand Up @@ -485,7 +486,7 @@ func (conf *ClientConfig) gatherStart() []string {

// CountStart returns the number of enabled proxies.
func (conf *ClientConfig) CountStart() int {
return len(funk.Filter(conf.Proxies, func(proxy *Proxy) bool { return !proxy.Disabled }).([]*Proxy))
return len(lo.Filter(conf.Proxies, func(proxy *Proxy, i int) bool { return !proxy.Disabled }))
}

// NewProxyFromIni creates a proxy object from ini section
Expand Down
4 changes: 2 additions & 2 deletions ui/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ui
import (
"os"
"path/filepath"
"slices"
"sync"

"github.com/lxn/walk"
"github.com/thoas/go-funk"

"github.com/koho/frpmgr/pkg/config"
"github.com/koho/frpmgr/pkg/consts"
Expand Down Expand Up @@ -146,7 +146,7 @@ func deleteConf(conf *Conf) bool {

// Check whether a config exists with the given name
func hasConf(name string) bool {
return funk.Contains(confList, func(e *Conf) bool { return e.Name == name })
return slices.ContainsFunc(confList, func(e *Conf) bool { return e.Name == name })
}

// ConfBinder is the view model of the current selected config
Expand Down
14 changes: 7 additions & 7 deletions ui/confview.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/thoas/go-funk"
"github.com/samber/lo"

"github.com/koho/frpmgr/i18n"
"github.com/koho/frpmgr/pkg/config"
Expand Down Expand Up @@ -311,7 +311,7 @@ checkName:
newPath := PathOfConf(baseName + ".conf")
if _, err := os.Stat(newPath); err == nil {
if rename {
suffix = "_" + funk.RandomString(4)
suffix = "_" + lo.RandomString(4, lo.AlphanumericCharset)
goto checkName
}
return newPath, false
Expand Down Expand Up @@ -534,10 +534,10 @@ func (cv *ConfView) onExport() {
dlg.FilePath += ".zip"
}

files := funk.Map(confList, func(conf *Conf) string {
files := lo.Map(confList, func(conf *Conf, i int) string {
return conf.Path
})
if err := util.ZipFiles(dlg.FilePath, files.([]string)); err != nil {
if err := util.ZipFiles(dlg.FilePath, files); err != nil {
showError(err, cv.Form())
}
}
Expand All @@ -563,18 +563,18 @@ func (cv *ConfView) onNATDiscovery() {
// reset config listview with selected name
func (cv *ConfView) reset(selectName string) {
// Make sure `sel` is a valid index
sel := funk.MaxInt([]int{cv.listView.CurrentIndex(), 0})
sel := max(cv.listView.CurrentIndex(), 0)
// Refresh the whole config list
// The confList will be sorted
cv.model = NewConfListModel(confList)
cv.listView.SetModel(cv.model)
if selectName != "" {
if idx := funk.IndexOf(cv.model.items, func(conf *Conf) bool { return conf.Name == selectName }); idx >= 0 {
if idx := slices.IndexFunc(cv.model.items, func(conf *Conf) bool { return conf.Name == selectName }); idx >= 0 {
sel = idx
}
}
// Make sure the final selected index is valid
if selectIdx := funk.MinInt([]int{sel, len(cv.model.items) - 1}); selectIdx >= 0 {
if selectIdx := min(sel, len(cv.model.items)-1); selectIdx >= 0 {
cv.listView.SetCurrentIndex(selectIdx)
}
}
Expand Down
14 changes: 7 additions & 7 deletions ui/editclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/thoas/go-funk"
"github.com/samber/lo"

"github.com/koho/frpmgr/i18n"
"github.com/koho/frpmgr/pkg/config"
Expand Down Expand Up @@ -94,9 +94,9 @@ func (cd *EditClientDialog) View() Dialog {
},
)
dlg.Layout = VBox{Margins: Margins{Left: 7, Top: 9, Right: 7, Bottom: 9}}
minWidth := int(funk.Sum(funk.Map(pages, func(page TabPage) int {
minWidth := lo.Sum(lo.Map(pages, func(page TabPage, i int) int {
return calculateStringWidth(page.Title.(string)) + 19
})) + 70)
})) + 70
dlg.MinSize = Size{Width: minWidth, Height: 380}
return dlg
}
Expand Down Expand Up @@ -470,13 +470,13 @@ func (cd *EditClientDialog) onSave() {
// Rename old log files
// The service should be stopped first
cd.shutdownService(true)
util.RenameFiles(logs, funk.Map(funk.Zip(logs, dates), func(t funk.Tuple) string {
if t.Element2 == "" {
util.RenameFiles(logs, lo.Map(dates, func(item string, i int) string {
if item == "" {
return newConf.LogFile
} else {
return filepath.Join(filepath.Dir(newConf.LogFile), baseName+"."+t.Element2.(string)+ext)
return filepath.Join(filepath.Dir(newConf.LogFile), baseName+"."+item+ext)
}
}).([]string))
}))
}
}
} else if cd.hasConf(newConf.Name) {
Expand Down
11 changes: 6 additions & 5 deletions ui/editproxy.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ui

import (
"slices"
"strings"

"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/thoas/go-funk"
"github.com/samber/lo"

"github.com/koho/frpmgr/i18n"
"github.com/koho/frpmgr/pkg/config"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (pd *EditProxyDialog) View() Dialog {
Children: []Widget{
LineEdit{AssignTo: &pd.nameView, Text: Bind("Name", consts.ValidateNonEmpty)},
PushButton{Text: i18n.SprintfLSpace("Random"), Image: loadResourceIcon(consts.IconRefresh, 16), OnClicked: func() {
rs := funk.RandomString(8)
rs := lo.RandomString(8, lo.AlphanumericCharset)
if strings.HasPrefix(pd.nameView.Text(), consts.RangePrefix) {
rs = consts.RangePrefix + rs
}
Expand Down Expand Up @@ -158,9 +159,9 @@ func (pd *EditProxyDialog) View() Dialog {
},
)
dlg.Layout = VBox{Margins: Margins{Left: 7, Top: 9, Right: 7, Bottom: 9}}
minWidth := int(funk.Sum(funk.Map(pages, func(page TabPage) int {
minWidth := lo.Sum(lo.Map(pages, func(page TabPage, i int) int {
return calculateStringWidth(page.Title.(string)) + 20
})) + 20)
})) + 20
// Keep a better aspect ratio
if minWidth < 350 {
minWidth += 30
Expand Down Expand Up @@ -464,7 +465,7 @@ func (pd *EditProxyDialog) onSave() {

func (pd *EditProxyDialog) hasProxy(name string) bool {
if conf := getCurrentConf(); conf != nil {
if funk.Contains(conf.Data.Items(), func(proxy *config.Proxy) bool { return proxy.Name == name }) {
if slices.ContainsFunc(conf.Data.Items().([]*config.Proxy), func(proxy *config.Proxy) bool { return proxy.Name == name }) {
showWarningMessage(pd.Form(), i18n.Sprintf("Proxy already exists"), i18n.Sprintf("The proxy name \"%s\" already exists.", name))
return true
}
Expand Down
6 changes: 3 additions & 3 deletions ui/logpage.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package ui

import (
"slices"
"sort"
"sync"
"time"

"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/thoas/go-funk"

"github.com/koho/frpmgr/i18n"
"github.com/koho/frpmgr/pkg/util"
Expand Down Expand Up @@ -193,14 +193,14 @@ func (lp *LogPage) onVisibleChanged() {
}
// Switch to current config log first
if conf := getCurrentConf(); conf != nil {
if i := funk.IndexOf(lp.nameModel.items, func(c *Conf) bool { return c.Name == conf.Name }); i >= 0 {
if i := slices.IndexFunc(lp.nameModel.items, func(c *Conf) bool { return c.Name == conf.Name }); i >= 0 {
lp.nameView.SetCurrentIndex(i)
return
}
}
// Select previous config log
if preName != "" {
if i := funk.IndexOf(lp.nameModel.items, func(c *Conf) bool { return c.Name == preName }); i >= 0 {
if i := slices.IndexFunc(lp.nameModel.items, func(c *Conf) bool { return c.Name == preName }); i >= 0 {
lp.nameView.SetCurrentIndex(i)
return
}
Expand Down
8 changes: 4 additions & 4 deletions ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/lxn/walk"
"github.com/thoas/go-funk"
"github.com/samber/lo"

"github.com/koho/frpmgr/pkg/config"
"github.com/koho/frpmgr/pkg/util"
Expand Down Expand Up @@ -76,10 +76,10 @@ func NewProxyModel(conf *Conf) *ProxyModel {
m := new(ProxyModel)
m.conf = conf
m.data = conf.Data.(*config.ClientConfig)
m.items = funk.Map(m.data.Proxies, func(p *config.Proxy) ProxyItem {
m.items = lo.Map(m.data.Proxies, func(p *config.Proxy, i int) ProxyItem {
pi := ProxyItem{Proxy: p, DisplayLocalIP: p.LocalIP, DisplayLocalPort: p.LocalPort}
// Combine subdomain and custom domains to form a list of domains
pi.Domains = strings.Join(funk.FilterString([]string{p.SubDomain, p.CustomDomains}, func(s string) bool {
pi.Domains = strings.Join(lo.Filter([]string{p.SubDomain, p.CustomDomains}, func(s string, i int) bool {
return strings.TrimSpace(s) != ""
}), ",")
// Show bind address and server name for visitor
Expand All @@ -89,7 +89,7 @@ func NewProxyModel(conf *Conf) *ProxyModel {
pi.DisplayLocalPort = p.BindPort
}
return pi
}).([]ProxyItem)
})
return m
}

Expand Down
6 changes: 3 additions & 3 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/lxn/win"
"github.com/thoas/go-funk"
"github.com/samber/lo"
"golang.org/x/sys/windows"

"github.com/koho/frpmgr/i18n"
Expand Down Expand Up @@ -202,11 +202,11 @@ func calculateHeadColumnTextWidth(widgets []Widget, columns int) int {

// calculateStringWidth returns the estimated display width of the given string
func calculateStringWidth(str string) int {
return int(funk.Sum(funk.Map(util.RuneSizeInString(str), func(s int) int {
return lo.Sum(lo.Map(util.RuneSizeInString(str), func(s int, i int) int {
// For better estimation, reduce size for non-ascii character
if s > 1 {
return s - 1
}
return s
})) * 6)
})) * 6
}
7 changes: 4 additions & 3 deletions ui/urlimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/thoas/go-funk"
"github.com/samber/lo"

"github.com/koho/frpmgr/i18n"
"github.com/koho/frpmgr/pkg/consts"
Expand Down Expand Up @@ -83,8 +83,9 @@ func (ud *URLImportDialog) onImport() {
return
}
urls := strings.Split(ud.viewModel.URLs, "\n")
urls = funk.FilterString(funk.Map(urls, strings.TrimSpace).([]string), func(s string) bool {
return s != ""
urls = lo.FilterMap(urls, func(s string, i int) (string, bool) {
s = strings.TrimSpace(s)
return s, s != ""
})
if len(urls) == 0 {
showWarningMessage(ud.Form(),
Expand Down

0 comments on commit 4b61e30

Please sign in to comment.