dissectors

package
v0.0.0-...-0fdad32 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Coercable

type Coercable struct {
	// contains filtered or unexported fields
}

Coercable is a simple builder pattern to help transform the underlying value (if present) into the desired type. The logic aims to be relatively simple, while trying to handle the possible input variations. The underlying value types can be: string, []string, []interface{}, float64, bool

Because of the variations for values, some conversions are done to make this feature extensible. However, no effort has been made to support converting from any type to any other type.

func (*Coercable) AsBool

func (c *Coercable) AsBool() bool

AsBool converts the Coercable into a bool type. If this is impossible, then the zero value will be returned

func (*Coercable) AsBoolPtr

func (c *Coercable) AsBoolPtr() *bool

AsBoolPtr converts the Coercable into a *bool type. If the underlying value is nil, then this will return nil. if the underlying value is not nil, but also not a bool, then the zero value will be returned

func (*Coercable) AsInt64

func (c *Coercable) AsInt64() int64

AsInt64 converts the Coercable into an int64 type. If this is impossible, then the zero value will be returned

Note: if the underlying object is a []string, then the first value will be converted into an int64, then returned

func (*Coercable) AsInt64Slice

func (c *Coercable) AsInt64Slice() []int64

AsInt64Slice converts the Coercable into an []int64 type. If this is impossible, then the zero value will be returned

Note: if the underlying object is a []string, then each value will be converted into an int64, then returned

func (*Coercable) AsString

func (c *Coercable) AsString() string

AsString converts the Coercable into a string type. If this is impossible, then the zero value will be returned

Note: if the underlying object is a []string, then the first value will be returned

func (*Coercable) AsStringPtr

func (c *Coercable) AsStringPtr() *string

AsStringPtr converts the Coercable into a *string type. If the underlying value is nil, then this will return nil. if the underlying value is not nil, but also not a string, then the zero value will be returned

func (*Coercable) AsStringSlice

func (c *Coercable) AsStringSlice() []string

AsStringSlice converts the Coercable into an []string type. If this is impossible, then the zero value will be returned

func (*Coercable) AsTime

func (c *Coercable) AsTime() time.Time

AsTime converts the Coercable into a time.Time type. AsTime will look to parse an RFC3339 datetime string. If the string does not match the format, then it will not parse, and the zero value will be returned.

func (*Coercable) AsTimePtr

func (c *Coercable) AsTimePtr() *time.Time

AsTimePtr converts the Coercable into a time.Time type. If the underlying value is nil, then this will return nil. if the underlying value is not nil, but also not a time.Time, then the zero value will be returned

func (*Coercable) AsUnixTime

func (c *Coercable) AsUnixTime() time.Time

AsUnixTime converts the Coercable into a time.Time type. AsUnixTime will look to convert the integer (nanoseconds since 1970) into the appropriate time format. If this fails then the zero value will be returned.

func (*Coercable) OrDefault

func (c *Coercable) OrDefault(defaultValue interface{}) *Coercable

OrDefault provides a default value for a given convesion, if the conversion would have otherwise failed. This is really only useful for optional values. Note: the provided value will itself be coerced into the target type. If the type cannot be coerced into the target type then the appropriate zero value will be returned.

This is a non-terminal Coercable action

Examples:

assert(parsedJSON.FromBody("anInt").OrDefault(12).AsInt64(), int64(12))
assert(parsedJSON.FromBody("anInt").OrDefault("twelve").AsInt64(), 0)

func (*Coercable) Required

func (c *Coercable) Required() *Coercable

Required marks the field as required, which, on some coercion issue, will set the DissectedRequest.Error field, indicating that an error has occurred

By deafult, every field is Optional

type DissectedRequest

type DissectedRequest struct {
	Request *http.Request

	Error error
	// contains filtered or unexported fields
}

DissectedRequest stores a parsed JSON body, the map of URL substituted values, and query string values, as well as if any errors were encountered during processing.

Usage Example:

// Chi

func(w http.ResponseWriter, r *http.Request) {
  parsedRequest := DissectJSONRequest(r, GenerateUrlParamMap(r))
  input := service.RepeatWordInput{
		SomeString: parsedRequest.FromURL("someString").Required(true).AsString()
     Times: parsedRequest.FromQuery("times").OrDefault(2).AsInt64()
  }
  if parsedRequest.Error != nil {
     // process error
  }
  service.RepeatWord( input )
}

func DissectFormRequest

func DissectFormRequest(r *http.Request, urlParameters map[string]string) DissectedRequest

DissectFormRequest retrieves query string values from the provided request and parses a multipart form body, if present. Once this data is parsed, values can be retrieved by using FromQuery, FromURL, FromBody or FromFile. In the case of files, the particular keys need to be "registered" by passing values into the fileKeys parameter. Then they can be recalled by passing the same key

If an error is encountered while trying to parse the json, it will be stored in the DissectedRequest.Error field

func DissectJSONRequest

func DissectJSONRequest(r *http.Request, urlParameters map[string]string) DissectedRequest

DissectJSONRequest retrieves query string values from the provided request and parses a JSON body, if present. Once this data is parsed, values can be retrieved by using FromQuery, FromURL, or FromBody

If an error is encountered while trying to parse the json, it will be stored in the DissectedRequest.Error field

func DissectPlainRequest

func DissectPlainRequest(r *http.Request, urlParameters map[string]string) DissectedRequest

DissectPlainRequest retrieves query string values from the provided request. Once this data is parsed, values can be retrieved by using FromQuery, or FromURL

If an error is encountered while trying to parse the json, it will be stored in the DissectedRequest.Error field

func (*DissectedRequest) FromBody

func (m *DissectedRequest) FromBody(key string) *Coercable

FromBody attempts to retrieve a field from the parsed body. Returns a Coercable, which can then be transformed into the desired type. If an error has already been encountered, the resulting action is a no-op

func (*DissectedRequest) FromFile

func (m *DissectedRequest) FromFile(key string) multipart.File

FromFile retrieves the named file from a multipart form. The returned value is a *UploadedFile which can be used to retrieve the file or file header.

func (*DissectedRequest) FromQuery

func (m *DissectedRequest) FromQuery(key string) *Coercable

FromQuery attempts to retrieve a field from the query string. Returns a Coercable, which can then be transformed into the desired type. If an error has already been encountered, the resulting action is a no-op

Note: All values retrieved from the query string are string slices. Any method that would return a singular item instead returns the first element instead. Likewise, any method that returns a non-string will instead convert the field appropriately

func (*DissectedRequest) FromURL

func (m *DissectedRequest) FromURL(key string) *Coercable

FromURL attempts to retrieve a field from the provided url parameters (e.g. in POST /operation/{operation_id}, operation_id would be the URL parameter). Returns Coercable, which can then be transformed into the desired type. If an error has already been encountered, the resulting action is a no-op

Jump to

Keyboard shortcuts

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