Skip to content

Commit c9256fc

Browse files
committed
Merge branch 'fix/version' of github.com:pubgo/funk into fix/version
2 parents 83b32e5 + 51ec515 commit c9256fc

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

config/envs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55

66
"github.com/pubgo/funk/env"
7-
"github.com/samber/lo"
7+
"github.com/pubgo/funk/strutil"
88
)
99

1010
type EnvSpecMap map[string]*EnvSpec
@@ -13,14 +13,14 @@ type EnvSpec struct {
1313
Name string `yaml:"name"`
1414
Description string `yaml:"description"`
1515
Default string `yaml:"default"`
16+
Value string `yaml:"value"`
1617
Required bool `yaml:"required"`
1718
Example string `yaml:"example"`
1819
}
1920

2021
func initEnv(envMap EnvSpecMap) {
2122
for name, cfg := range envMap {
22-
envData := env.Get(name)
23-
envData = strings.TrimSpace(lo.Ternary(envData != "", envData, cfg.Default))
23+
envData := strings.TrimSpace(strutil.FirstNotEmpty(env.Get(name), cfg.Value, cfg.Default))
2424
if cfg.Required && envData == "" {
2525
panic("env " + cfg.Name + " is required")
2626
}

env/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func Key(key string) string {
121121

122122
func LoadFiles(files ...string) (r result.Error) {
123123
files = lo.Filter(files, func(item string, index int) bool { return pathutil.IsExist(item) })
124-
if result.Catch(&r, godotenv.Load(files...)) {
124+
if result.Catch(&r, godotenv.Overload(files...)) {
125125
return
126126
}
127127

log/impl.slog.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log/slog"
66

7+
"github.com/pubgo/funk/log/slogutil"
78
"github.com/rs/zerolog"
89
slogcommon "github.com/samber/slog-common"
910
)
@@ -56,7 +57,11 @@ func (s slogImpl) Handle(ctx context.Context, r slog.Record) error {
5657
}
5758

5859
r.Attrs(func(attr slog.Attr) bool {
59-
evt.Any(attr.Key, attr.Value.Any())
60+
if fn, ok := attr.Value.Any().(slogutil.LogFunc); ok {
61+
evt.Func(fn)
62+
} else {
63+
evt.Any(attr.Key, attr.Value.Any())
64+
}
6065
return true
6166
})
6267

log/slogutil/util.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package slogutil
2+
3+
import (
4+
"log/slog"
5+
6+
"github.com/rs/zerolog"
7+
)
8+
9+
type LogFunc func(evt *zerolog.Event)
10+
11+
func Func(fn LogFunc) slog.Attr {
12+
return slog.Any("func", fn)
13+
}

log/z_slog_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package log_test
2+
3+
import (
4+
"log/slog"
5+
"testing"
6+
7+
"github.com/pubgo/funk/log"
8+
"github.com/pubgo/funk/log/slogutil"
9+
"github.com/rs/zerolog"
10+
)
11+
12+
func TestSlog(t *testing.T) {
13+
slog.SetDefault(slog.New(log.NewSlog(log.GetLogger(""))))
14+
slog.Info("ok")
15+
slog.Info("ok", slogutil.Func(func(evt *zerolog.Event) {
16+
evt.Str("record", "ok")
17+
}))
18+
}

0 commit comments

Comments
 (0)