split files and make error checking functions more flexible

This commit is contained in:
Diego Fernando Carrión 2023-10-30 11:50:29 +01:00
parent faba797beb
commit f0598d8d8c
No known key found for this signature in database
GPG Key ID: 811EF2E03998BFC4
3 changed files with 77 additions and 67 deletions

51
errors.go Normal file
View File

@ -0,0 +1,51 @@
package sugar
import (
"fmt"
"os"
)
var (
ErrorWriter = os.Stderr
ErrorPrefix = "Error:"
ErrorFmt = "%s %+v"
StandardExitCode = 1
PanicFunc = func(err error) {
panic(err)
}
ErrorPrintFunc = func(err error) {
fmt.Fprintf(ErrorWriter, ErrorFmt, ErrorPrefix, err)
}
ExitFunc = func(code int) {
os.Exit(code)
}
)
// Check takes an error and prints it if it is not nil; but will continue.
func Check(err error) {
if err != nil {
ErrorPrintFunc(err)
}
}
// CheckPanic takes an error and will panic if it is not nil.
func CheckPanic(err error) {
if err != nil {
PanicFunc(err)
}
}
// CheckExit takes an error and will print it and exit if it is not nil.
// If no exit code is provided, the StandardExitCode will be used.
// Any exit codes beyond the first one one provided will be discarded.
func CheckExit(err error, code ...int) {
if err != nil {
ErrorPrintFunc(err)
exitCode := StandardExitCode
if len(code) > 0 {
exitCode = code[0]
}
ExitFunc(exitCode)
}
}

View File

@ -1,11 +1,6 @@
// Package sugar provides convenience functions for keeping code clean and less repetitive. // Package sugar provides convenience functions for keeping code clean and less repetitive.
package sugar package sugar
import (
"fmt"
"os"
)
/* /*
* Literal Addressers * Literal Addressers
*/ */
@ -104,65 +99,3 @@ func StrPtr(v string) *string {
func BoolPtr(v bool) *bool { func BoolPtr(v bool) *bool {
return &v return &v
} }
/*
* Error Handling
*/
// Check takes an error and prints it if it is not nil; but will continue.
func Check(err error) {
if err != nil {
fmt.Printf("%+v", err)
}
}
// CheckLog takes an error and a format function for leveraging a custom printer like a logger.
func CheckLog(err error, fn func(format string, a ...interface{})) {
if err != nil {
fn("%+v", err)
}
}
// CheckPanic takes an error and will panic if it is not nil.
func CheckPanic(err error) {
if err != nil {
panic(err)
}
}
// CheckExit takes an error and will print it and exit with status 1 if it is not nil.
func CheckExit(err error) {
if err != nil {
fmt.Printf("%+v", err)
os.Exit(1)
}
}
/*
* Sets
*/
// StrSet a set type for strings.
type StrSet map[string]struct{}
// Add adds a new key to the set if it does not already exist.
func (s *StrSet) Add(str string) {
(*s)[str] = struct{}{}
}
// Has checks if the given string is present in the set.
func (s *StrSet) Has(str string) bool {
if _, ok := (*s)[str]; ok {
return true
}
return false
}
// AddHas behaves like Add, but returns true if the key already existed.
func (s *StrSet) AddHas(str string) bool {
if ok := s.Has(str); ok {
return ok
}
s.Add(str)
return false
}

26
sets.go Normal file
View File

@ -0,0 +1,26 @@
package sugar
// StrSet a set type for strings.
type StrSet map[string]struct{}
// Add adds a new key to the set if it does not already exist.
func (s *StrSet) Add(str string) {
(*s)[str] = struct{}{}
}
// Has checks if the given string is present in the set.
func (s *StrSet) Has(str string) bool {
if _, ok := (*s)[str]; ok {
return true
}
return false
}
// AddHas behaves like Add, but returns true if the key already existed.
func (s *StrSet) AddHas(str string) bool {
if ok := s.Has(str); ok {
return ok
}
s.Add(str)
return false
}