Skip to content
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
3 changes: 1 addition & 2 deletions ai/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ func (r *conversation) runMiddlewarePipeline(ctx context.Context, prompt contrac
next := destination
activeMiddlewares := slices.Clone(r.middlewares)

for i := len(activeMiddlewares) - 1; i >= 0; i-- {
middleware := activeMiddlewares[i]
for _, middleware := range slices.Backward(activeMiddlewares) {
nextHandler := next
next = contractsai.Next(func(ctx context.Context, prompt contractsai.AgentPrompt) (contractsai.AgentResponse, error) {
return middleware.Handle(ctx, prompt, nextHandler)
Expand Down
6 changes: 3 additions & 3 deletions ai/failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ai
import (
"context"
"regexp"
"sort"
"slices"
"strings"

contractsai "github.com/goravel/framework/contracts/ai"
Expand Down Expand Up @@ -53,7 +53,7 @@ func newFailoverProvider(providers []resolvedProvider) contractsai.Provider {
return providers[0].provider
}

return &failoverProvider{providers: append([]resolvedProvider(nil), providers...)}
return &failoverProvider{providers: slices.Clone(providers)}
}

func (e *failoverError) Error() string {
Expand Down Expand Up @@ -215,7 +215,7 @@ func NewFailoverRules(provider string, patterns map[contractsai.FailoverReason][
reasons = append(reasons, string(reason))
}
}
sort.Strings(reasons)
slices.Sort(reasons)

var rules []failoverRule
for _, reasonValue := range reasons {
Expand Down
9 changes: 2 additions & 7 deletions ai/media_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ai
import (
pathpkg "path"
"regexp"
"slices"
"strings"

contractsfilesystem "github.com/goravel/framework/contracts/filesystem"
Expand Down Expand Up @@ -69,13 +70,7 @@ func storeContent(content []byte, name, path, disk string, nameRequired error) (
}

func hasParentPathSegment(path string) bool {
for _, segment := range strings.Split(path, "/") {
if segment == ".." {
return true
}
}

return false
return slices.Contains(strings.Split(path, "/"), "..")
}

func isAbsoluteStoragePath(path string) bool {
Expand Down
3 changes: 2 additions & 1 deletion ai/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ai
import (
"bytes"
"context"
"slices"
"strings"

contractsai "github.com/goravel/framework/contracts/ai"
Expand Down Expand Up @@ -251,5 +252,5 @@ func cloneTranscriptionSegments(segments []contractsai.TranscriptionSegment) []c
return nil
}

return append([]contractsai.TranscriptionSegment(nil), segments...)
return slices.Clone(segments)
}
30 changes: 10 additions & 20 deletions cache/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ func (s *MemoryTestSuite) TestDecrementWithConcurrent() {
failed atomic.Int64
)
for range 1000 {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
// Assertions belong on the test goroutine, and err is shared.
if _, err := s.memory.Decrement("decrement_concurrent", 1); err != nil {
failed.Add(1)
}
}()
})
}

wg.Wait()
Expand Down Expand Up @@ -126,22 +124,18 @@ func (s *MemoryTestSuite) TestFlushWhileReading() {

var wg sync.WaitGroup
for range 50 {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for range 500 {
s.memory.Get("test-flush-while-reading")
}
}()
})
}
for range 10 {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for range 200 {
s.memory.Flush()
}
}()
})
}
wg.Wait()

Expand Down Expand Up @@ -229,17 +223,15 @@ func (s *MemoryTestSuite) TestAddWithConcurrent() {
start = make(chan struct{})
)
for range 8 {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
// Contend on the same expired item: a goroutine released after
// a winner has replaced it leaves through the "not expired"
// branch, which proves nothing about the swap.
<-start
if s.memory.Add(key, "goravel", NoExpiration) {
added.Add(1)
}
}()
})
}
close(start)
wg.Wait()
Expand Down Expand Up @@ -383,14 +375,12 @@ func (s *MemoryTestSuite) TestIncrementWithConcurrent() {
failed atomic.Int64
)
for range 1000 {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
// Assertions belong on the test goroutine, and err is shared.
if _, err := s.memory.Increment("increment_concurrent", 1); err != nil {
failed.Add(1)
}
}()
})
}

wg.Wait()
Expand Down
8 changes: 3 additions & 5 deletions console/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ func TestRun_Concurrent(t *testing.T) {

var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
wg.Go(func() {
_ = cliApp.Call("test1")
_ = cliApp.Call("test2")
wg.Done()
}()
})
}

wg.Wait()
Expand Down Expand Up @@ -698,7 +696,7 @@ type filterTestCommand struct {
signature string
}

func (f *filterTestCommand) Signature() string { return f.signature }
func (f *filterTestCommand) Signature() string { return f.signature }
func (f *filterTestCommand) Description() string { return f.signature }
func (f *filterTestCommand) Extend() command.Extend {
return command.Extend{}
Expand Down
2 changes: 1 addition & 1 deletion console/cli_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (r *CliContext) NewLine(times ...int) {
if len(times) > 0 && times[0] > 0 {
numLines = times[0]
}
for i := 0; i < numLines; i++ {
for range numLines {
color.Default().Println()
}
}
Expand Down
9 changes: 4 additions & 5 deletions console/cli_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/support/color"
"github.com/goravel/framework/support/convert"
)

func TestArgumentString(t *testing.T) {
Expand Down Expand Up @@ -2094,10 +2093,10 @@ func TestTable(t *testing.T) {
{
name: "compact style using boolean flags",
option: console.TableOption{
BorderTop: convert.Pointer(false),
BorderBottom: convert.Pointer(false),
BorderLeft: convert.Pointer(false),
BorderRight: convert.Pointer(false),
BorderTop: new(false),
BorderBottom: new(false),
BorderLeft: new(false),
BorderRight: new(false),
},
contains: []string{"ID", "Name", "│"},
},
Expand Down
28 changes: 17 additions & 11 deletions console/cli_helper.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package console

import (
"cmp"
"context"
"fmt"
"io"
"os"
"slices"
"sort"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -162,11 +164,8 @@ func findAlternatives(name string, collection []string) (result []string) {
sortedAlternatives = append(sortedAlternatives, scoredItem{item, score})
}
}
sort.Slice(sortedAlternatives, func(i, j int) bool {
if sortedAlternatives[i].score == sortedAlternatives[j].score {
return sortedAlternatives[i].name < sortedAlternatives[j].name
}
return sortedAlternatives[i].score < sortedAlternatives[j].score
slices.SortFunc(sortedAlternatives, func(a, b scoredItem) int {
return cmp.Or(cmp.Compare(a.score, b.score), strings.Compare(a.name, b.name))
})
for _, item := range sortedAlternatives {
result = append(result, item.name)
Expand All @@ -186,8 +185,8 @@ func getFlagDefaultText(flag cli.DocGenerationFlag) string {

func getFlagName(flag cli.Flag) string {
names := flag.Names()
sort.Slice(names, func(i, j int) bool {
return len(names[i]) < len(names[j])
slices.SortFunc(names, func(a, b string) int {
return cmp.Compare(len(a), len(b))
})
prefixed := cli.FlagNamePrefixer(names, "")
// If there is no short name, add some padding to align flag name.
Expand Down Expand Up @@ -384,10 +383,17 @@ func sortVisibleFlags(cmd *cli.Command) []cli.Flag {
for i := range globalFlags {
globalFlagNames[getFlagName(globalFlags[i])] = struct{}{}
}
sort.Slice(flags, func(i, j int) bool {
_, isGlobalI := globalFlagNames[getFlagName(flags[i])]
_, isGlobalJ := globalFlagNames[getFlagName(flags[j])]
return !isGlobalI && isGlobalJ
// Stable sort keeps the by-name order from above inside each group.
slices.SortStableFunc(flags, func(a, b cli.Flag) int {
_, isGlobalA := globalFlagNames[getFlagName(a)]
_, isGlobalB := globalFlagNames[getFlagName(b)]
switch {
case !isGlobalA && isGlobalB:
return -1
case isGlobalA && !isGlobalB:
return 1
}
return 0
})

return flags
Expand Down
6 changes: 3 additions & 3 deletions console/service_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package console

import (
"sort"
"slices"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -50,7 +50,7 @@ func signaturesOf(commands []contractsconsole.Command) []string {
for _, c := range commands {
sigs = append(sigs, c.Signature())
}
sort.Strings(sigs)
slices.Sort(sigs)
return sigs
}

Expand Down Expand Up @@ -107,7 +107,7 @@ func TestServiceProviderBootAllowlistApplies(t *testing.T) {
artisan.EXPECT().Register(mock.MatchedBy(func(commands []contractsconsole.Command) bool {
got := signaturesOf(commands)
want := []string{"list", "make:command"}
sort.Strings(want)
slices.Sort(want)
if len(got) != len(want) {
return false
}
Expand Down
15 changes: 7 additions & 8 deletions console/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/pterm/pterm"

"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/support/convert"
)

var (
Expand Down Expand Up @@ -40,13 +39,13 @@ var (

StyleFunc: DefaultTableStyleFunc,

BorderTop: convert.Pointer(true),
BorderBottom: convert.Pointer(true),
BorderLeft: convert.Pointer(true),
BorderRight: convert.Pointer(true),
BorderHeader: convert.Pointer(true),
BorderColumn: convert.Pointer(true),
BorderRow: convert.Pointer(false),
BorderTop: new(true),
BorderBottom: new(true),
BorderLeft: new(true),
BorderRight: new(true),
BorderHeader: new(true),
BorderColumn: new(true),
BorderRow: new(false),
}

GlobalHuhTheme = func() *huh.Theme {
Expand Down
3 changes: 3 additions & 0 deletions contracts/translation/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Option struct {
Locale string
}

// Bool returns a pointer to the value.
//
// Deprecated: Use new(value) instead, Bool will be removed in a future version.
func Bool(value bool) *bool {
return &value
}
7 changes: 2 additions & 5 deletions crypt/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ func Benchmark_EncryptString(b *testing.B) {
b.Fatal(err)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := aes.EncryptString("Goravel")
if err != nil {
b.Error(err)
Expand All @@ -101,9 +99,8 @@ func Benchmark_DecryptString(b *testing.B) {
if err != nil {
b.Error(err)
}
b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := aes.DecryptString(payload)
if err != nil {
b.Error(err)
Expand Down
Loading
Loading