Documentation ¶
Overview ¶
Package defaults provides some global default values.
Package defaults provides some global default values.
Index ¶
- Variables
- func GetHTTPStatusCode(ctx context.Context, w http.ResponseWriter, r *http.Request) int
- func GetRemoteAddr(ctx context.Context, req interface{}) (netip.Addr, error)
- func GetRequestID(ctx context.Context, req interface{}) string
- func GetStructFieldName(sf reflect.StructField) (name, arg string)
- func HTTPIsResponded(ctx context.Context, w http.ResponseWriter, r *http.Request) bool
- func Now() time.Time
- func ValidateStruct(value interface{}) (err error)
- func ValidateWithRule(value interface{}, rule string) (err error)
- type Value
Constants ¶
This section is empty.
Variables ¶
var ( // HTTPIsRespondedFunc is used to report whether the http response is responded. // // For the default implementation, it will try check http.ResponseWriter: // 1. Check whether it implements the interface{ WroteHeader() bool } and return it. // 2. Check whether it implements the interface{ Unwrap() http.ResponseWriter } and retry 1. // 3. Return false instead. HTTPIsRespondedFunc = NewValueWithValidation(httpIsResponded, httpIsRespondedValidateFunc) // GetHTTPStatusCodeFunc is used to get the status code of the http response. // // For the default implementation, it will try check http.ResponseWriter: // 1. Check whether it implements the interface{ StatusCode() int } and return it. // 2. Check whether it implements the interface{ Unwrap() http.ResponseWriter } and retry 1. // 3. Return 200 instead. GetHTTPStatusCodeFunc = NewValueWithValidation(getHTTPStatusCode, getHTTPStatusCodeValidateFunc) )
var ( // GetRequestIDFunc is used to get the unique request session id. // // For the default implementation, it only detects req // and supports the types or interfaces: // // *http.Request // interface{ RequestID() string } // interface{ GetRequestID() string } // interface{ GetRequest() *http.Request } // interface{ GetHTTPRequest() *http.Request } // // For *http.Request, it will returns the header "X-Request-Id". // // Return "" instead if not found. GetRequestIDFunc = NewValueWithValidation(getRequestID, reqidValidateFunc) // GetRemoteAddrFunc is used to get the remote address. // // For the default implementation, it only detects req // and supports the types or interfaces: // // *http.Request // interface{ GetRequest() *http.Request } // interface{ GetHTTPRequest() *http.Request } // interface{ RemoteAddr() string } // interface{ RemoteAddr() net.IP } // interface{ RemoteAddr() net.Addr } // interface{ RemoteAddr() netip.Addr } GetRemoteAddrFunc = NewValueWithValidation(getRemoteAddr, raddrValidateFunc) )
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 ( // 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"), validateStructFieldName) )
Functions ¶
func GetHTTPStatusCode ¶ added in v0.4.0
GetHTTPStatusCode is the proxy of GetHTTPStatusCodeFunc to call the function.
func GetRemoteAddr ¶ added in v0.3.0
GetRemoteAddr is the proxy of GetRemoteAddrFunc 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 HTTPIsResponded ¶ added in v0.4.0
HTTPIsResponded is the proxy of HTTPIsRespondedFunc to call the function.
func ValidateStruct ¶
func ValidateStruct(value interface{}) (err error)
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 thread-safely.
It will panic if failing to validate the new value.