diff --git a/result/result.go b/result/result.go index e990bca..71471e4 100644 --- a/result/result.go +++ b/result/result.go @@ -3,6 +3,7 @@ package result import ( "encoding/json" "fmt" + "github.com/pubgo/funk/stack" "github.com/pubgo/funk/generic" @@ -51,15 +52,15 @@ type Result[T any] struct { e error } -func (r *Result[T]) WithErr(err error) Result[T] { +func (r Result[T]) WithErr(err error) Result[T] { return Err[T](err) } -func (r *Result[T]) WithVal(v T) Result[T] { +func (r Result[T]) WithVal(v T) Result[T] { return OK(v) } -func (r *Result[T]) Err(check ...func(err error) error) error { +func (r Result[T]) Err(check ...func(err error) error) error { if !r.IsErr() { return nil } @@ -71,18 +72,18 @@ func (r *Result[T]) Err(check ...func(err error) error) error { return r.e } -func (r *Result[T]) IsErr() bool { +func (r Result[T]) IsErr() bool { return r.e != nil || !generic.IsNil(r.e) } -func (r *Result[T]) OrElse(v T) T { +func (r Result[T]) OrElse(v T) T { if r.IsErr() { return v } return generic.DePtr(r.v) } -func (r *Result[T]) Unwrap(check ...func(err error) error) T { +func (r Result[T]) Unwrap(check ...func(err error) error) T { if !r.IsErr() { return generic.DePtr(r.v) } @@ -94,7 +95,7 @@ func (r *Result[T]) Unwrap(check ...func(err error) error) T { } } -func (r *Result[T]) Expect(format string, args ...any) T { +func (r Result[T]) Expect(format string, args ...any) T { if !r.IsErr() { return generic.DePtr(r.v) } @@ -105,7 +106,7 @@ func (r *Result[T]) Expect(format string, args ...any) T { }) } -func (r *Result[T]) String() string { +func (r Result[T]) String() string { if !r.IsErr() { return fmt.Sprintf("%v", *r.v) } @@ -113,7 +114,7 @@ func (r *Result[T]) String() string { return r.e.Error() } -func (r *Result[T]) MarshalJSON() ([]byte, error) { +func (r Result[T]) MarshalJSON() ([]byte, error) { if r.IsErr() { return nil, r.e } @@ -121,6 +122,6 @@ func (r *Result[T]) MarshalJSON() ([]byte, error) { return json.Marshal(generic.DePtr(r.v)) } -func (r *Result[T]) UnmarshalJSON(data []byte) error { - return json.Unmarshal(data, &r.v) +func (r Result[T]) UnmarshalJSON([]byte) error { + panic("unimplemented") } diff --git a/result/result_test.go b/result/result_test.go index e6c0da8..a4aa831 100644 --- a/result/result_test.go +++ b/result/result_test.go @@ -8,21 +8,21 @@ import ( type hello struct { Name string `json:"name"` - Jj func() } func TestName(t *testing.T) { var ok = result.OK(&hello{Name: "abc"}) okBytes := result.Of(json.Marshal(&ok)) data := string(okBytes.Expect("failed to encode json data")) + t.Log(data) if data != `{"name":"abc"}` { t.Log(data) t.Fatal("not match") } - var ok1 result.Result[*hello] + var ok1 hello if err := json.Unmarshal([]byte(data), &ok1); err != nil { t.Fatal(err) } - t.Log("ok", ok1.Unwrap().Name) + t.Log("ok", ok1.Name) }