utils

package
v0.0.0-...-d6c5cac Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: Apache-2.0 Imports: 31 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DateLayout      = "2006-01-02"
	DateTimeLayout  = "2006-01-02 15:04:05"
	BuildTimeLayout = "2006.0102.150405"
	TimestampLayout = "20060102150405"
)

Variables

View Source
var Debug = true
View Source
var (
	DefaultGormLogger = GormLogger{log.New(os.Stdout, "[ORM] ", 0)}
)
View Source
var FlagVarConfFile string
View Source
var FlagVarDBFile string
View Source
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
	if len(values) > 1 {
		var (
			sql             string
			formattedValues []string
			level           = values[0]
			currentTime     = time.Now().Format("2006/01/02 15:04:05")
		)

		messages = []interface{}{currentTime}

		if level == "sql" {

			messages = append(messages, fmt.Sprintf("[%.2fms]", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))

			for _, value := range values[4].([]interface{}) {
				indirectValue := reflect.Indirect(reflect.ValueOf(value))
				if indirectValue.IsValid() {
					value = indirectValue.Interface()
					if t, ok := value.(time.Time); ok {
						formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
					} else if b, ok := value.([]byte); ok {
						if str := string(b); isPrintable(str) {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
						} else {
							formattedValues = append(formattedValues, "'<binary>'")
						}
					} else if r, ok := value.(driver.Valuer); ok {
						if value, err := r.Value(); err == nil && value != nil {
							formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
						} else {
							formattedValues = append(formattedValues, "NULL")
						}
					} else {
						formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
					}
				} else {
					formattedValues = append(formattedValues, "NULL")
				}
			}

			if numericPlaceHolderRegexp.MatchString(values[3].(string)) {
				sql = values[3].(string)
				for index, value := range formattedValues {
					placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
					sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
				}
			} else {
				formattedValuesLength := len(formattedValues)
				for index, value := range sqlRegexp.Split(values[3].(string), -1) {
					sql += value
					if index < formattedValuesLength {
						sql += formattedValues[index]
					}
				}
			}

			messages = append(messages, sql)
			messages = append(messages, fmt.Sprintf("\r\n%v", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned"))
		} else {
			messages = append(messages, values[2:]...)
		}
	}

	return
}
View Source
var StartTime = time.Now()

Functions

func CWD

func CWD() string

func CloseLogWriter

func CloseLogWriter()

func Conf

func Conf() *ini.File

func ConfFile

func ConfFile() string

func ConfFileDev

func ConfFileDev() string

func Copy

func Copy(src, dest string) error

Copy copies src to dest, doesn't matter if src is a directory or a file

func DBFile

func DBFile() string

func DBFileDev

func DBFileDev() string

func DataDir

func DataDir() string

func DeepCopy

func DeepCopy(dst, src interface{}) error

func EXEName

func EXEName() string

func Ellipsis

func Ellipsis(text string, length int) string

func EnsureDir

func EnsureDir(dir string) (err error)

func ErrorLogFilename

func ErrorLogFilename() string

func Exist

func Exist(path string) bool

func ExpandHomeDir

func ExpandHomeDir(path string) string

func GetLogWriter

func GetLogWriter() io.Writer

func GetRequestHostname

func GetRequestHostname(r *http.Request) (hostname string)

func GetRequestHref

func GetRequestHref(r *http.Request) string

func HasChinese

func HasChinese(str string) bool

func HomeDir

func HomeDir() string

func IsPortInUse

func IsPortInUse(port int) bool

func LocalIP

func LocalIP() string

func Log

func Log(msg ...interface{})

func LogDir

func LogDir() string

func Logf

func Logf(format string, msg ...interface{})

func MD5

func MD5(str string) string

func Open

func Open(url string) error

func PauseExit

func PauseExit()

func PauseGo

func PauseGo(msg ...interface{})

func RedirectStderr

func RedirectStderr() (err error)

RedirectStderr to the file passed in

func ReloadConf

func ReloadConf() *ini.File

func SaveToConf

func SaveToConf(section string, kvmap map[string]string) error

func ShortID

func ShortID() string

func UpTime

func UpTime() time.Duration

func UpTimeString

func UpTimeString() string

func WorkInDir

func WorkInDir(f func(), dir string)

Types

type DateTime

type DateTime time.Time

func (DateTime) MarshalJSON

func (dt DateTime) MarshalJSON() ([]byte, error)

func (*DateTime) Scan

func (dt *DateTime) Scan(v interface{}) error

func (DateTime) String

func (dt DateTime) String() string

func (*DateTime) UnmarshalJSON

func (dt *DateTime) UnmarshalJSON(data []byte) (err error)

func (DateTime) Value

func (dt DateTime) Value() (driver.Value, error)

type GormLogger

type GormLogger struct {
	*log.Logger
}

Logger default logger

func (GormLogger) Print

func (logger GormLogger) Print(values ...interface{})

Print format & print log

type PageForm

type PageForm struct {
	Start int    `form:"start"`
	Limit int    `form:"limit"`
	Q     string `form:"q"`
	Sort  string `form:"sort"`
	Order string `form:"order"`
}

func NewPageForm

func NewPageForm() *PageForm

func (PageForm) String

func (p PageForm) String() string

type PageResult

type PageResult struct {
	Total int         `json:"total"`
	Rows  interface{} `json:"rows"`
}

func NewPageResult

func NewPageResult(rows interface{}) *PageResult

func (*PageResult) Slice

func (pr *PageResult) Slice(start, limit int) *PageResult

func (*PageResult) Sort

func (pr *PageResult) Sort(by, order string) *PageResult

type StatikFileSystem

type StatikFileSystem struct {
	http.FileSystem
}

func (*StatikFileSystem) Exists

func (s *StatikFileSystem) Exists(prefix string, filepath string) bool

type StringArray

type StringArray string

func (StringArray) MarshalJSON

func (r StringArray) MarshalJSON() ([]byte, error)

Jump to

Keyboard shortcuts

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