1// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net> 2// released under the MIT license 3 4package utils 5 6import ( 7 "errors" 8 "fmt" 9 "strings" 10 "time" 11) 12 13const ( 14 IRCv3TimestampFormat = "2006-01-02T15:04:05.000Z" 15) 16 17var ( 18 ErrInvalidParams = errors.New("Invalid parameters") 19) 20 21func StringToBool(str string) (result bool, err error) { 22 switch strings.ToLower(str) { 23 case "on", "true", "t", "yes", "y", "enabled": 24 result = true 25 case "off", "false", "f", "no", "n", "disabled": 26 result = false 27 default: 28 err = ErrInvalidParams 29 } 30 return 31} 32 33// Checks that a parameter can be passed as a non-trailing, and returns "*" 34// if it can't. See #697. 35func SafeErrorParam(param string) string { 36 if param == "" || param[0] == ':' || strings.IndexByte(param, ' ') != -1 { 37 return "*" 38 } 39 return param 40} 41 42type IncompatibleSchemaError struct { 43 CurrentVersion int 44 RequiredVersion int 45} 46 47func (err *IncompatibleSchemaError) Error() string { 48 return fmt.Sprintf("Database requires update. Expected schema v%d, got v%d", err.RequiredVersion, err.CurrentVersion) 49} 50 51func NanoToTimestamp(nanotime int64) string { 52 return time.Unix(0, nanotime).UTC().Format(IRCv3TimestampFormat) 53} 54 55func BoolDefaultTrue(value *bool) bool { 56 if value != nil { 57 return *value 58 } 59 return true 60} 61