Skip to content

Commit

Permalink
fix: update result
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Jan 26, 2024
1 parent f9786f6 commit b19df34
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 1,540 deletions.
18 changes: 12 additions & 6 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package env

import (
"fmt"
"log"
"os"
"strconv"
"strings"

"github.com/a8m/envsubst"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/result"
)

var trim = strings.TrimSpace

func Set(key, value string) {
func Set(key, value string) error {
k, v, ok := Normalize(fmt.Sprintf("%s=%s", key, value))
assert.If(!ok, "env key is incorrect")
assert.Must(os.Setenv(Key(k), v))
return os.Setenv(Key(k), v)
}

func Get(names ...string) string {
Expand Down Expand Up @@ -50,6 +52,7 @@ func GetBoolVal(val *bool, names ...string) {

v, err := strconv.ParseBool(dt)
if err != nil {
log.Printf("env: failed to parse string to bool, err=%v\n", err)
return
}

Expand All @@ -64,6 +67,7 @@ func GetIntVal(val *int, names ...string) {

v, err := strconv.Atoi(dt)
if err != nil {
log.Printf("env: failed to parse string to int, err=%v\n", err)
return
}

Expand All @@ -78,6 +82,7 @@ func GetFloatVal(val *float64, names ...string) {

v, err := strconv.ParseFloat(dt, 32)
if err != nil {
log.Printf("env: failed to parse string to float, err=%v\n", err)
return
}

Expand All @@ -88,12 +93,13 @@ func Lookup(key string) (string, bool) {
return os.LookupEnv(Key(key))
}

func Delete(key string) {
assert.Must(os.Unsetenv(Key(key)))
func Delete(key string) error {
return os.Unsetenv(Key(key))
}

func Expand(value string) string {
return assert.Must1(envsubst.String(value))
func Expand(value string) result.Result[string] {

return result.Of(envsubst.String(value))
}

func Map() map[string]string {
Expand Down
9 changes: 4 additions & 5 deletions generic/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func ExtractFrom[T, E any](set []T, fn func(T) E) []E {
for i := range set {
r[i] = fn(set[i])
}

return r
}

Expand All @@ -104,7 +103,6 @@ func Filter[T any](set []T, criteria func(T) bool) []T {
r = append(r, set[i])
}
}

return r
}

Expand All @@ -118,7 +116,6 @@ func FilterInPlace[T any](set []T, criteria func(T) bool) []T {
i--
}
}

return set
}

Expand All @@ -130,7 +127,6 @@ func Delete[T comparable](set []T, value T) []T {
break
}
}

return set
}

Expand All @@ -142,7 +138,6 @@ func DeleteAll[T comparable](set []T, value T) []T {
i--
}
}

return set
}

Expand Down Expand Up @@ -189,3 +184,7 @@ func IsNil(err interface{}) bool {

return v.IsZero()
}

func Init[T any](fn func() T) T {
return fn()
}
2 changes: 2 additions & 0 deletions log/aaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Logger interface {

type StdLogger interface {
Printf(format string, v ...interface{})
Logf(format string, v ...interface{})
Print(v ...interface{})
Log(v ...interface{})
Println(v ...interface{})
}
14 changes: 13 additions & 1 deletion log/impl.log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"context"
"encoding/json"
"fmt"

Check failure on line 6 in log/impl.log.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/org/project (goimports)
"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ func (l *loggerImpl) WithName(name string) Logger {
}

func (l *loggerImpl) WithFields(m Map) Logger {
if len(m) == 0 {
if m == nil || len(m) == 0 {
return l
}

Expand Down Expand Up @@ -114,6 +115,17 @@ func (l *loggerImpl) Err(err error, ctxL ...context.Context) *zerolog.Event {
return nil
}

if err != nil {
if errJson, ok := err.(json.Marshaler); ok {
var errJsonBytes, _ = errJson.MarshalJSON()
if errJsonBytes != nil && len(errJsonBytes) > 0 {
return l.newEvent(ctxL, l.getLog().Error().Str("error", err.Error()).RawJSON("error_detail", errJsonBytes))
}
}

return l.newEvent(ctxL, l.getLog().Error().Str("error", err.Error()))
}

return l.newEvent(ctxL, l.getLog().Err(err))
}

Expand Down
8 changes: 8 additions & 0 deletions log/impl.std.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ func (s *stdLogImpl) Print(v ...interface{}) {
s.log.Info().Msg(fmt.Sprint(v...))
}

func (s *stdLogImpl) Log(v ...interface{}) {
s.log.Info().Msg(fmt.Sprint(v...))
}

func (s *stdLogImpl) Logf(format string, v ...interface{}) {
s.log.Info().Msgf(format, v...)
}

func (s *stdLogImpl) Println(v ...interface{}) {
s.log.Info().Msg(fmt.Sprint(v...))
}
16 changes: 11 additions & 5 deletions log/z_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package log_test

import (
"context"
"errors"
"fmt"
"github.com/pubgo/funk/errors"
"testing"

"github.com/rs/zerolog"
Expand All @@ -17,7 +18,12 @@ func TestName(t *testing.T) {
log.Print("world world")
log.Info().Str("hello", "world world").Msg("ok ok")
log.Warn().Str("hello", "world world").Msg("ok ok")
log.Err(errors.New("error")).Str("hello", "world world").Msg("ok ok")

var err = errors.WrapCaller(fmt.Errorf("test error"))
err = errors.Wrap(err, "next error")
err = errors.WrapTag(err, errors.T("event", "test event"), errors.T("test123", 123), errors.T("test", "hello"))
err = errors.Wrapf(err, "next error name=%s", "wrapf")
log.Err(err).Str("hello", "world world").Msg("ok ok")
log.GetLogger("test_app").Info().Str("hello", "world world").Msg("ok ok")
log.GetLogger("test_app").Info().Str("hello", "world world").Msg("ok ok")
log.GetLogger("test_app").
Expand Down Expand Up @@ -45,7 +51,7 @@ func TestSetLog(t *testing.T) {
//zerolog.SetGlobalLevel(zerolog.ErrorLevel)
log.SetLogger(generic.Ptr(zl.Output(zerolog.NewConsoleWriter())))
log.Debug().Msg("test")
log.Debug().Msg("test")
log.Debug().Msg("test")
log.Debug().Msg("test")
log.Info().Msg("test")
log.Warn().Msg("test")
log.Error().Msg("test")
}
27 changes: 0 additions & 27 deletions proto/commonpb/page_deepcopy.gen.go

This file was deleted.

2 changes: 0 additions & 2 deletions proto/commonpb/request-info_deepcopy.gen.go

This file was deleted.

27 changes: 0 additions & 27 deletions proto/commonpb/resource_deepcopy.gen.go

This file was deleted.

27 changes: 0 additions & 27 deletions proto/commonpb/response_deepcopy.gen.go

This file was deleted.

11 changes: 9 additions & 2 deletions result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"encoding/json"
"fmt"

"github.com/pubgo/funk/stack"

"github.com/pubgo/funk/generic"
"github.com/pubgo/funk/stack"
)

var _ error = (*Error)(nil)
Expand Down Expand Up @@ -125,3 +124,11 @@ func (r Result[T]) MarshalJSON() ([]byte, error) {
func (r Result[T]) UnmarshalJSON([]byte) error {
panic("unimplemented")
}

func Pipe[A, B any](a Result[A], fn func(t A) Result[B]) Result[B] {
if a.IsErr() {
return Err[B](a.Err())
}

return fn(a.Unwrap())
}
4 changes: 4 additions & 0 deletions result/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func TestName(t *testing.T) {
}
t.Log("ok", ok1.Name)
}

func TestPipe(t *testing.T) {

}
40 changes: 0 additions & 40 deletions websocket/ReadMe.md

This file was deleted.

Loading

0 comments on commit b19df34

Please sign in to comment.