Documentation ¶
Overview ¶
Package defaults provides some global default values.
Index ¶
- Variables
- func Exit(code int)
- func GetClientIP(ctx context.Context, req any) netip.Addr
- func GetRequestID(ctx context.Context, req any) string
- func GetStacks(skip int) []string
- func GetStructFieldName(sf reflect.StructField) (name, arg string)
- func HandlePanic(ctx context.Context, r any)
- func Now() time.Time
- func Recover(ctx context.Context)
- func TrimPkgFile(file string) string
- func ValidateStruct(value any) (err error)
- func ValidateWithRule(value any, rule string) (err error)
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // HeaderXRequestID is used by GetRequestIDFunc to try // to get the request id from the http request. HeaderXRequestID = "X-Request-Id" // GetRequestIDFunc is used to get the unique request session id. // // For the default implementation, it only supports the types and interfaces: // // *http.Request // interface{ RequestID() string } // interface{ GetRequestID() string } // // Or, return "". GetRequestIDFunc = NewValueWithValidation(getRequestID, fActxAifaceR1[string]("GetRequestID")) )
var ( // TrimPkgFileFunc is used to trim the prefix of the package file path. TrimPkgFileFunc = NewValueWithValidation(trimPkgFile, fA1R1Validation[string, string]("TrimPkgFile")) // GetStacksFunc is used to get the stacks of the function calling. GetStacksFunc = NewValueWithValidation(getStacks, fA1R1Validation[int, []string]("GetStacks")) )
var ( TimeFormat = NewValueWithValidation(time.RFC3339Nano, validateTimeFormat) TimeFormats = NewValueWithValidation([]string{time.RFC3339Nano, "2006-01-02 15:04:05"}, validateTimeFormats) TimeLocation = NewValueWithValidation(time.UTC, validateTimeLocation) TimeNowFunc = NewValueWithValidation(time.Now, validateTimeNow) )
Pre-define some global variables about time.
var ( // RuleValidator is used to validate whether a value conforms with the rule. // // Note: in general, it is used to validate a struct field with the tag rule. RuleValidator = NewValue(assists.RuleValidator(nil)) // StructValidator is used to validate whether a struct value is valid. StructValidator = NewValue(assists.StructValidator(nil)) )
var ( // ExitFunc is used to exit the program. // // Default: os.Exit ExitFunc = NewValueWithValidation(os.Exit, fA1Validation[int]("Exit")) )
var ( // GetClientIPFunc is used to get the client ip of the request. // // For the default implementation, it only supports the types or interfaces: // // *http.Request // interface{ ClientIP() netip.Addr } // interface{ ClientIP() net.IP } // interface{ ClientIP() string } // interface{ RemoteAddr() netip.Addr } // interface{ RemoteAddr() net.Addr } // interface{ RemoteAddr() string } // // Or, return nil. GetClientIPFunc = NewValueWithValidation(getClientIP, fActxAifaceR1[netip.Addr]("GetClientIP")) )
var ( // HandlePanicFunc is used to handle the panic value returned by recover(). HandlePanicFunc = NewValueWithValidation(handlePanic, fActxAiface("HandlePanic")) )
var ( // StructFieldNameFunc is used to get the obtain the name and arg // of the struct field, which needs to return a empty string for // the field name, that's "", if the field should be ignored. // // Example: // // StructFieldNameFunc.Set(func(sf reflect.StructField) (name string, arg string) { // value := sf.Tag.Get("json") // if index := strings.IndexByte(value, ','); index > -1 { // arg = strings.TrimSpace(value[index+1:]) // value = strings.TrimSpace(value[:index]) // } // // switch value { // case "-": // case "": // name = sf.Name // default: // name = value // } // // return // }) // StructFieldNameFunc = NewValueWithValidation(assists.StructFieldNameFuncWithTags("json"), fA1R2Validation[reflect.StructField, string, string]("StructFieldName")) )
Functions ¶
func Exit ¶ added in v0.13.0
func Exit(code int)
Exit is the proxy of ExitFunc to call the function to exit the program.
func GetClientIP ¶ added in v0.9.0
GetClientIP is the proxy of GetClientIPFunc to call the function.
func GetRequestID ¶ added in v0.3.0
GetRequestID is the proxy of GetRequestIDFunc to call the function.
func GetStructFieldName ¶
func GetStructFieldName(sf reflect.StructField) (name, arg string)
GetStructFieldName is the proxy of StructFieldNameFunc to call the function, just like StructFieldNameFunc.Get()(sf).
func HandlePanic ¶ added in v0.7.0
HandlePanic is the proxy of HandlePanicFunc to call the funciton.
func Recover ¶ added in v0.13.0
Recover is a convenient function to wrap and recover the panic if occurring, then call HandlePanic to handle it.
NOTICE: It must be called after defer, like
defer Recover(context.Background())
func TrimPkgFile ¶ added in v0.8.0
TrimPkgFile is the proxy of TrimPkgFileFunc to call the funciton.
Example ¶
srcfile := TrimPkgFile("/path/to/src/github.com/xgfone/go-defaults/srcfile.go") modfile := TrimPkgFile("/path/to/pkg/mod/github.com/xgfone/go-defaults/modfile.go") origfile := TrimPkgFile("/path/to/github.com/xgfone/go-defaults/modfile.go") fmt.Println(srcfile) fmt.Println(modfile) fmt.Println(origfile)
Output: github.com/xgfone/go-defaults/srcfile.go github.com/xgfone/go-defaults/modfile.go /path/to/github.com/xgfone/go-defaults/modfile.go
func ValidateStruct ¶
ValidateStruct uses Validator to validate the struct value if StructValidator is not nil.
func ValidateWithRule ¶
ValidateWithRule uses RuleValidator to validate a value conforms with the rule if RuleValidator is not nil.
Types ¶
type Value ¶
type Value[T any] struct { // contains filtered or unexported fields }
Value represents a common value.
func NewValueWithValidation ¶
NewValueWithValidation returns a new Value with the initial value and the validation.
validate may be nil, which is equal to always return nil.
func (*Value[T]) Set ¶
func (v *Value[T]) Set(new T)
Set sets the value to new.
It will panic if failing to validate the new value.