Skip to content

Commit

Permalink
rename op.MustValue to op.MustResult
Browse files Browse the repository at this point in the history
  • Loading branch information
mkideal committed Aug 20, 2024
1 parent 7136ccc commit dcb5d46
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
28 changes: 18 additions & 10 deletions op/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,31 @@ func Must(err error) {
}
}

// MustValue panics if err is not nil, otherwise it returns value.
// Result returns err if it is not nil, otherwise it returns value.
func Result(value any, err error) any {
if err != nil {
return err
}
return value
}

// MustResult panics if err is not nil, otherwise it returns value.
// It is a convenient way to handle errors in a single line.
func MustValue[T any](value T, err error) T {
func MustResult[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}

// MustResult2 panics if err is not nil, otherwise it returns value1 and value2.
func MustResult2[T1, T2 any](value1 T1, value2 T2, err error) (T1, T2) {
if err != nil {
panic(err)
}
return value1, value2
}

// ReverseCompare returns a comparison function that reverses the order of the original comparison function.
func ReverseCompare[T any](cmp func(T, T) int) func(T, T) int {
return func(x, y T) int {
Expand All @@ -162,11 +178,3 @@ func Identity[T any](v T) func() T {
return v
}
}

// Result returns err if it is not nil, otherwise it returns value.
func Result(value any, err error) any {
if err != nil {
return err
}
return value
}
28 changes: 23 additions & 5 deletions op/op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,36 @@ func TestMust(t *testing.T) {
op.Must(fmt.Errorf("error"))
}

func TestMustValue(t *testing.T) {
func TestMustResult(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Must did not panic")
}
}()
op.MustValue(42, fmt.Errorf("error"))
result := op.MustResult(42, fmt.Errorf("error"))
if result != 42 {
t.Errorf("MustResult(42, error) = %v, want 42", result)
}
}

func TestMustResult_no_panic(t *testing.T) {
if got := op.MustResult(42, nil); got != 42 {
t.Errorf("MustResult(42, nil) = %v, want 42", got)
}
}

func TestMustResult2(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Must did not panic")
}
}()
op.MustResult2(42, "hello", fmt.Errorf("error"))
}

func TestMustValue_no_panic(t *testing.T) {
if got := op.MustValue(42, nil); got != 42 {
t.Errorf("MustValue(42, nil) = %v, want 42", got)
func TestMustResult2_no_panic(t *testing.T) {
if got1, got2 := op.MustResult2(42, "hello", nil); got1 != 42 || got2 != "hello" {
t.Errorf("MustResult2(42, \"hello\", nil) = (%v, %v), want (42, \"hello\")", got1, got2)
}
}

Expand Down
12 changes: 6 additions & 6 deletions service/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ func TestConfig_ProcessTemplate(t *testing.T) {
TemplateUUID: op.Addr(types.Bool(true)),
TemplateRefs: op.Addr(types.Bool(true)),
TemplateOptions: op.Addr(types.Bool(true)),
Refs: types.NewRawObject(op.MustValue(json.Marshal(refs{
Refs: types.NewRawObject(op.MustResult(json.Marshal(refs{
A: "{{.Name}}-A",
B: "B",
}))),
Options: types.NewRawObject(op.MustValue(json.Marshal(options{
Options: types.NewRawObject(op.MustResult(json.Marshal(options{
C: "C",
D: "{{.Name}}-D",
}))),
Expand All @@ -294,11 +294,11 @@ func TestConfig_ProcessTemplate(t *testing.T) {
TemplateUUID: op.Addr(types.Bool(true)),
TemplateRefs: op.Addr(types.Bool(true)),
TemplateOptions: op.Addr(types.Bool(true)),
Refs: types.NewRawObject(op.MustValue(json.Marshal(refs{
Refs: types.NewRawObject(op.MustResult(json.Marshal(refs{
A: "TestName-A",
B: "B",
}))),
Options: types.NewRawObject(op.MustValue(json.Marshal(options{
Options: types.NewRawObject(op.MustResult(json.Marshal(options{
C: "C",
D: "TestName-D",
}))),
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestConfig_ProcessTemplate(t *testing.T) {
{
Name: "Component1",
TemplateRefs: op.Addr(types.Bool(true)),
Refs: types.NewRawObject(op.MustValue(json.Marshal(map[string]string{
Refs: types.NewRawObject(op.MustResult(json.Marshal(map[string]string{
"A": "{{.NameXXX}}-A",
"B": "B",
}))),
Expand All @@ -373,7 +373,7 @@ func TestConfig_ProcessTemplate(t *testing.T) {
{
Name: "Component1",
TemplateOptions: op.Addr(types.Bool(true)),
Options: types.NewRawObject(op.MustValue(json.Marshal(map[string]string{
Options: types.NewRawObject(op.MustResult(json.Marshal(map[string]string{
"A": "{{.NameXXX}}-A",
"B": "B",
}))),
Expand Down

0 comments on commit dcb5d46

Please sign in to comment.