offer

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package offer provides the means to offer various permutations of data, content type and language to the content negotiation matching algorithm. It also provides Match, the result type from the matching algorithm.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Match

type Match struct {
	Type     string
	Subtype  string
	Language string
	Charset  string
	Vary     []string
	Data     data.Data
	Render   Processor
}

Match holds the best-matched offer after content negotiation and is used for response rendering.

func (Match) ApplyHeaders

func (m Match) ApplyHeaders(rw http.ResponseWriter) io.Writer

ApplyHeaders sets response headers so that the user agent is notified of the content negotiation decisons made. Four headers may be set, depending on context.

  • Content-Type is always set.
  • Content-Language is set when a language was selected.
  • Content-Encoding is set when the character set is being transcoded
  • Vary is set to list the accept headers that led to the three decisions above.

func (Match) String

func (m Match) String() string

type Offer

type Offer struct {
	// ContentType is the content type that is to be matched.
	// Wildcard values may be used.
	header.ContentType

	// Langs lists the language(s) provided by this offer.
	Langs []string
	// contains filtered or unexported fields
}

Offer holds information about one particular resource representation that can potentially provide an acceptable response.

func Of

func Of(processor Processor, contentType string) Offer

Of constructs an Offer easily, given a content type. If the content type is blank, it is assumed to be the full wildcard "*/*". Also, contentType can be a partial wildcard "type/*".

func (Offer) BuildMatch

func (o Offer) BuildMatch(lang string, acceptedCT header.MediaRange) *Match

BuildMatch implements the transition between a selected Offer and the resulting Match.

func (Offer) Data

func (o Offer) Data(lang string) data.Data

func (Offer) String

func (o Offer) String() string

String is merely for information purposes.

func (Offer) With

func (o Offer) With(d interface{}, language string, otherLanguages ...string) Offer

With attaches response data to an offer. The language parameter specifies what language (or language group) the offer will match. It can be "*" to match any. The method panics if it is blank. Other languages can also be specified, but these must not be "*" (or blank).

The data can be a value (struct, slice, etc) or a data.Data. It may also be nil, which means the method merely serves to add the language to the Offer's supported languages.

The original offer is unchanged.

type Offers

type Offers []Offer

Offers holds a slice of Offer.

func (Offers) Filter

func (offers Offers) Filter(typ, subtype string) Offers

Filter returns only the offers that match specified type and subtype.

type Processor

type Processor func(w http.ResponseWriter, req *http.Request, match Match, template string) error

Processor is a function that renders content according to the matched result.

Jump to

Keyboard shortcuts

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