mustify

command module
v0.0.0-...-c5c9c53 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2020 License: MIT Imports: 1 Imported by: 0

README

mustify

GitHub Actions codecov GoDoc

mustify is CLI tool for generate MustXXX methods from go source which includes methods that return error.

In Go, there is a culture where a method that occur a panic instead of returning an error has Must as a prefix.
For example, regexp.MustCompile is the wrapper for regexp.Compile which occure panic if regexp.Compile return error. mustify generate the wrapper automatically from all functions which return error.

Usage

mustify [path]

Assume you have below functions in math.go

package lib

import (
	"errors"
	"math"
)

// Sqrt returns the square root of x
func Sqrt(x float64) (float64, error) {
	if x < 0 {
		return 0, errors.New("invalid value")
	}
	return math.Sqrt(x), nil
}

// Sqrt returns the square root of x without error
func SqrtWithoutError(x float64) float64 {
	return math.Sqrt(x)
}

// SumAndSub returns sum and sub of arguments
func SumAndSub(v1, v2 int) (int, int, error) {
	return v1 + v2, v1 - v2, nil
}

func unexportedFunc() error {
	return nil
}

Then execute mustify:

$ mustify math.go > must-math.go
$ cat lib/must-math.go
package lib

// MustSqrt returns the square root of x
func MustSqrt(x float64) float64 {
	_v0, _err := Sqrt(x)
	if _err != nil {
		panic(_err)
	}
	return _v0
}

// MustSumAndSub returns sum and sub of arguments
func MustSumAndSub(v1, v2 int) (int, int) {
	_v0, _v1, _err := SumAndSub(v1, v2)
	if _err != nil {
		panic(_err)
	}
	return _v0, _v1
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
cmd
option
Package option provides utilities of option handling
Package option provides utilities of option handling
Package selfupdate provides function to update binary Package util provides some utilities
Package selfupdate provides function to update binary Package util provides some utilities

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL