A golang code generator for errors from the definitions
MIT License
errgen
is a code generator. This library generates functions that return error according to the definition described in the struct
.
And the function can receive variables as the function parameters. The parameters will be bound into placeholders that are compatible with sprintf.
Please refer to the synopsis for a concrete example.
$ go get -u github.com/moznion/go-errgen/cmd/errgen
And this library requires goimports
that is on executable path.
Usage of errgen:
-out-file string
[optional] the output destination path of the generated code (default "<snake_case_struct_name>_errmsg_gen.go")
-prefix string
[optional] prefix of error type (default "ERR-")
-type string
[mandatory] struct type name of source of error definition
-version
show version and revision
Define errors and configure with go:generate
:
package mypkg
//go:generate errgen -type=myErrors
type myErrors struct {
FooErr error `errmsg:"this is FOO error"`
BarErr error `errmsg:"this is BAR error [%d, %s]" vars:"hoge int, fuga string"`
}
And execute go generate:
$ go generate ./...
Then it generates my_errors_errmsg_gen.go
. That has the following contents:
// This package was auto generated.
// DO NOT EDIT BY YOUR HAND!
package mypkg
import "errors"
import "fmt"
func FooErr() error {
return errors.New(`[ERR-1] this is FOO error`)
}
func BarErr(hoge int, fuga string) error {
return fmt.Errorf(`[ERR-2] this is BAR error [%d, %s]`, hoge, fuga)
}
func MyErrorsList() []string {
return []string{
`[ERR-1] this is FOO error`,
`[ERR-2] this is BAR error [%d, %s]`,
}
}
example:
type myErrors struct {
FooErr error `errmsg:"this is FOO error [%d, %s]" vars:"hoge int, fuga string"`
BarErr error `errmsg:"this is BAR error" obsoleted:"true"`
}
errmsg
sprintf
style placeholder
vars
parameter togethervars
sprintf
style placeholders of errmsg
obsoleted
obsoleted
parameter to exclude an error message from the target of code generationA map doesn't ensure the order of members. That is a bit inconvenient to generate error messages with a prefix that contains the serial number.
It's a good point. But I (i.e. moznion) don't want to consider the unbalanced slice members; unbalanced means odd number of members.
The MIT License (MIT)
Copyright © 2019 moznion, http://moznion.net/ <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.