Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
must/v2: Use generics to cut down on generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
misha-ridge committed Jan 17, 2022
1 parent c1f5e77 commit 61f57c4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 573 deletions.
103 changes: 0 additions & 103 deletions generator/generator.go

This file was deleted.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/ridge/must
module github.com/ridge/must/v2

go 1.14
go 1.18
28 changes: 25 additions & 3 deletions must.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package must is a library to shorten handling of impossible conditions
//
// must.OK(os.Unsetenv("FOO"))
// bytes := must.Bytes(json.Marshal(dataStructureDefinedInCode))
// bytes := must.OK1(json.Marshal(dataStructureDefinedInCode))
// defer must.Do(f.Close)
//
// is
Expand Down Expand Up @@ -30,15 +30,37 @@
//
package must

//go:generate go run ./generator types_gen.go

// OK panics on error
func OK(err error) {
if err != nil {
panic(err)
}
}

// OK1 panics on error, returns the first argument otherwise
func OK1[T any](t T, err error) T {
OK(err)
return t
}

// OK2 panics on error, returns the first arguments otherwise
func OK2[T1, T2 any](t1 T1, t2 T2, err error) (T1, T2) {
OK(err)
return t1, t2
}

// OK3 panics on error, returns the first arguments otherwise
func OK3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3, err error) (T1, T2, T3) {
OK(err)
return t1, t2, t3
}

// OK4 panics on error, returns the first arguments otherwise
func OK4[T1, T2, T3, T4 any](t1 T1, t2 T2, t3 T3, t4 T4, err error) (T1, T2, T3, T4) {
OK(err)
return t1, t2, t3, t4
}

// Do calls the function and panics on error.
//
// For use primarily in defer statements.
Expand Down
92 changes: 44 additions & 48 deletions must_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package must

import (
"errors"
"os"
"reflect"
"testing"
"time"
)

func capturePanic(f func()) (ret interface{}) {
Expand All @@ -28,76 +25,75 @@ func TestOK(t *testing.T) {
}
}

func TestDo(t *testing.T) {
func TestOK1(t *testing.T) {
e := errors.New("")

ret := capturePanic(func() {
Do(func() error {
return e
})
})
if ret != e {
t.Errorf("Do({return e}) did not panic with the returned error")
if capturePanic(func() { OK1(10, e) }) != e {
t.Errorf("OK1(..., non-nil-error) did not panic with the passed error")
}

ret = capturePanic(func() {
Do(func() error {
return nil
})
})
if ret != nil {
t.Errorf("Do({return nil}) panicked")
if OK1(10, nil) != 10 {
t.Errorf("OK1(10, nil) did not return first argument")
}
}

func TestInt(t *testing.T) {
func TestOK2(t *testing.T) {
e := errors.New("")

if capturePanic(func() { Int(10, e) }) != e {
t.Errorf("Int(10, non-nil-error) did not panic with the passed error")
if capturePanic(func() { OK2(10, 'c', e) }) != e {
t.Errorf("OK2(..., non-nil-error) did not panic with the passed error")
}

if Int(10, nil) != 10 {
t.Errorf("Int(10, nil) did not return 10")
v1, v2 := OK2(10, 'c', nil)
if v1 != 10 || v2 != 'c' {
t.Errorf("OK2(10, 'c', nil) did not return first arguments")
}
}

type fakeFileInfo struct {
}

func (fakeFileInfo) Name() string {
return ""
}
func TestOK3(t *testing.T) {
e := errors.New("")

func (fakeFileInfo) Size() int64 {
return 0
}
if capturePanic(func() { OK3(10, 'c', 7.0, e) }) != e {
t.Errorf("OK3(..., non-nil-error) did not panic with the passed error")
}

func (fakeFileInfo) Mode() os.FileMode {
return 0
v1, v2, v3 := OK3(10, 'c', 7.0, nil)
if v1 != 10 || v2 != 'c' || v3 != 7.0 {
t.Errorf("OK3(10, 'c', 7.0f, nil) did not return first arguments")
}
}

func (fakeFileInfo) ModTime() time.Time {
return time.Time{}
}
func TestOK4(t *testing.T) {
e := errors.New("")

func (fakeFileInfo) IsDir() bool {
return false
}
if capturePanic(func() { OK4(10, 'c', 7.0, "h", e) }) != e {
t.Errorf("OK4(..., non-nil-error) did not panic with the passed error")
}

func (fakeFileInfo) Sys() interface{} {
return nil
v1, v2, v3, v4 := OK4(10, 'c', 7.0, "h", nil)
if v1 != 10 || v2 != 'c' || v3 != 7.0 || v4 != "h" {
t.Errorf("OK4(10, 'c', 7.0, \"h\", nil) did not return first arguments")
}
}

func TestOSFileInfos(t *testing.T) {
func TestDo(t *testing.T) {
e := errors.New("")
fis := []os.FileInfo{fakeFileInfo{}}

if capturePanic(func() { OSFileInfos(fis, e) }) != e {
t.Errorf("OSFileInfos(fis, non-nil-error) did not panic with the passed error")
ret := capturePanic(func() {
Do(func() error {
return e
})
})
if ret != e {
t.Errorf("Do({return e}) did not panic with the returned error")
}

if !reflect.DeepEqual(OSFileInfos(fis, nil), fis) {
t.Errorf("OSFileInfos(fis, nil) did not return fis")
ret = capturePanic(func() {
Do(func() error {
return nil
})
})
if ret != nil {
t.Errorf("Do({return nil}) panicked")
}
}
Loading

0 comments on commit 61f57c4

Please sign in to comment.