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 19, 2024
1 parent a05b181 commit 2c86b58
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 12 additions & 11 deletions result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package result
import (
"encoding/json"
"fmt"

"github.com/pubgo/funk/stack"

"github.com/pubgo/funk/generic"
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -105,22 +106,22 @@ 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)
}

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
}

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")
}
6 changes: 3 additions & 3 deletions result/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 2c86b58

Please sign in to comment.