data

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

package data provides wrappers for response data, optionally including response headers such as ETag and Cache-Control.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContentAndApplyExtraHeaders added in v0.9.0

func GetContentAndApplyExtraHeaders(rw http.ResponseWriter, d Data, template, language string) (interface{}, error)

GetContentAndApplyExtraHeaders applies all lazy functions to produce the resulting content to be rendered; this value is returned. It also sets any extra response headers.

Types

type Data

type Data interface {
	// Content returns the data as a value that can be processed by encoders such as "encoding/json"
	Content(template, language string) (interface{}, error)

	// Headers returns response headers relating to the data (optional)
	Headers() map[string]interface{}
}

type Value

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

Value is a simple implementation of Data.

func Lazy

func Lazy(fn func(template, language string) (interface{}, error)) Value

Lazy wraps a function that supplies a data value, but only when it is needed.

func Of

func Of(v interface{}) Value

Of wraps a data value.

func (Value) Content

func (v Value) Content(template, language string) (result interface{}, err error)

func (Value) ETag added in v0.9.0

func (v Value) ETag(fn func(interface{}) string, weak ...bool) Value

ETag computes and sets the entity tag header on the response using some hash value. This is used for efficient conditional requests, possibly avoiding network traffic. The parameter fn evaluates the hash lazily based on the content of this Data (which may also be evaluated lazily).

func (Value) Expires added in v0.9.0

func (v Value) Expires(at time.Time) Value

Expires sets the time at which the response becomes stale. MaxAge takes precedence.

func (Value) Headers

func (v Value) Headers() map[string]interface{}

func (Value) LastModified added in v0.9.0

func (v Value) LastModified(at time.Time) Value

LastModified sets the time at which the content was last modified. This allows for conditional requests, possibly avoiding network traffic. ETag takes precedence.

func (Value) MaxAge added in v0.9.0

func (v Value) MaxAge(max time.Duration) Value

MaxAge sets the max-age header on the response. This is used to allow caches to avoid repeating the request until the max age has expired, after which time the resource is considered stale.

func (Value) NoCache added in v0.9.0

func (v Value) NoCache() Value

NoCache sets cache control headers to prevent the response being cached.

func (Value) With

func (v Value) With(hdr string, value interface{}, others ...interface{}) Value

With returns a copy of v with extra headers attached. These are passed in as key+value pairs. The header names should be in normal form, e.g. "Last-Modified" instead of "last-modified", but this is not mandatory. The values are simple strings, numbers etc. Or they can be func(interface{}) string, in which case they will be called using the result of Content.

Jump to

Keyboard shortcuts

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