defaults

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: Apache-2.0 Imports: 10 Imported by: 20

README

Defaults GoDoc License

Provide some global default values.

Documentation

Overview

Package defaults provides some global default values.

Package defaults provides some global default values.

Index

Constants

This section is empty.

Variables

View Source
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)
)
View Source
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)
)
View Source
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.

View Source
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))
)
View Source
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

func GetHTTPStatusCode(ctx context.Context, w http.ResponseWriter, r *http.Request) int

GetHTTPStatusCode is the proxy of GetHTTPStatusCodeFunc to call the function.

func GetRemoteAddr added in v0.3.0

func GetRemoteAddr(ctx context.Context, req interface{}) (netip.Addr, error)

GetRemoteAddr is the proxy of GetRemoteAddrFunc to call the function.

func GetRequestID added in v0.3.0

func GetRequestID(ctx context.Context, req interface{}) string

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

func HTTPIsResponded(ctx context.Context, w http.ResponseWriter, r *http.Request) bool

HTTPIsResponded is the proxy of HTTPIsRespondedFunc to call the function.

func Now

func Now() time.Time

Now returns the current time by using TimeNow and TimeLocation.

func ValidateStruct

func ValidateStruct(value interface{}) (err error)

ValidateStruct uses Validator to validate the struct value if StructValidator is not nil.

func ValidateWithRule

func ValidateWithRule(value interface{}, rule string) (err error)

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 NewValue

func NewValue[T any](initial T) *Value[T]

NewValue returns a new Value with the initial value.

func NewValueWithValidation

func NewValueWithValidation[T any](initial T, validate func(T) error) *Value[T]

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]) Get

func (v *Value[T]) Get() T

Get returns the inner value.

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.

func (*Value[T]) Swap added in v0.4.0

func (v *Value[T]) Swap(new T) (old T)

Swap sets the value to new thread-safely and returns the old value.

It will panic if failing to validate the new value.

func (*Value[T]) Validate

func (v *Value[T]) Validate(value T) error

Validate validate whether the input value is valid.

Directories

Path Synopsis
Package assists provides some assisted functions.
Package assists provides some assisted functions.

Jump to

Keyboard shortcuts

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