42 lines
869 B
Go
Raw Normal View History

2021-06-27 13:31:30 -07:00
package persistence
import (
"errors"
2022-03-06 19:17:43 -08:00
"fmt"
"regexp"
"strings"
2021-06-27 13:31:30 -07:00
"os"
)
2021-07-24 12:28:30 -07:00
var NotInDatabase = errors.New("Not in database")
type ErrNotInDatabase struct {
Table string
Value interface{}
}
2022-03-06 18:09:43 -08:00
2021-07-24 12:28:30 -07:00
func (err ErrNotInDatabase) Error() string {
return fmt.Sprintf("Not in database: %s %q", err.Table, err.Value)
}
2021-06-27 13:31:30 -07:00
// DUPE 1
func file_exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
} else if errors.Is(err, os.ErrNotExist) {
return false
} else {
panic(err)
}
}
2022-03-06 19:17:43 -08:00
/**
* https://stackoverflow.com/questions/56616196/how-to-convert-camel-case-string-to-snake-case#56616250
*/
func ToSnakeCase(str string) string {
snake := regexp.MustCompile("(.)_?([A-Z][a-z]+)").ReplaceAllString(str, "${1}_${2}")
snake = regexp.MustCompile("([a-z0-9])_?([A-Z])").ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}