Documentation ¶
Overview ¶
Package pinned is a proof-of-concept, date based versioning system for Go APIs.
Index ¶
- Variables
- func NewContext(ctx context.Context, v *Version) context.Context
- type Action
- type Change
- type Version
- type VersionManager
- func (vm *VersionManager) Add(v *Version) error
- func (vm *VersionManager) Apply(version *Version, obj Versionable) (map[string]interface{}, error)
- func (vm *VersionManager) Latest() *Version
- func (vm *VersionManager) Parse(r *http.Request) (*Version, error)
- func (vm *VersionManager) Versions() []string
- type Versionable
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidVersion means the version supplied is not included // in the versions supplied to the VersionManager or it is malformed. ErrInvalidVersion = errors.New("invalid version") // ErrNoVersionSupplied means no version was supplied. ErrNoVersionSupplied = errors.New("no version supplied") // ErrVersionDeprecated means the version is deprecated. ErrVersionDeprecated = errors.New("version is deprecated") )
Functions ¶
Types ¶
type Action ¶
Action represents an action to take on a object in order to make it compatible. An action takes an interface as input and returns an updated interface.
type Change ¶
type Change struct { // Description of the change made. Used for documentation. Description string // Actions are a map of object type to Action. The object type // is determined using the reflect pkg. Actions map[string]Action }
Change represents a backwards-incompatible change and the actions required to make it compatible.
type Version ¶
type Version struct { Date string Changes []*Change Deprecated bool // contains filtered or unexported fields }
Version represents a version change. It is pinned to a specific time. It contains a list of Changes. Changes are executed in-order.
Example ¶
package main import ( "github.com/sjkaliski/pinned" ) var version = &pinned.Version{} func main() { // userNameFieldChange is an action which "undoes" the change // made in this new version. userNameFieldChange := func(mapping map[string]interface{}) map[string]interface{} { mapping["full_name"] = mapping["name"] delete(mapping, "name") return mapping } // Version sets the date and lists a set of changes to be executed to // make the current API compatible with requests pinned to this version. version = &pinned.Version{ Date: "2018-02-11", Changes: []*pinned.Change{ { Description: "Renames `user.full_name` to `user.name`", // Actions are executed by type. In this example "User" // references any object of type User. Actions: map[string]pinned.Action{ "User": userNameFieldChange, }, }, }, } }
Output:
func FromContext ¶
FromContext returns the Version in the context.
type VersionManager ¶
type VersionManager struct { Layout string Header string Query string // contains filtered or unexported fields }
VersionManager represents a list of versions.
Example ¶
package main import ( "github.com/sjkaliski/pinned" ) var vm = pinned.VersionManager{} func main() { vm = pinned.VersionManager{ Header: "Example API", } }
Output:
func (*VersionManager) Apply ¶
func (vm *VersionManager) Apply(version *Version, obj Versionable) (map[string]interface{}, error)
Apply processes a Versionable object by applying all changes between the latest version and the version requested. The altered object is returned.
Concretely, if the supplied version is two versions behind the latest, the changes in those two versions are applied sequentially to the object. This essentially "undoes" the changes made to the API so that the object is structured according to the specified version.
func (*VersionManager) Latest ¶
func (vm *VersionManager) Latest() *Version
Latest returns the most current active version.
func (*VersionManager) Parse ¶
func (vm *VersionManager) Parse(r *http.Request) (*Version, error)
Parse evaluates an http.Request object to determine an API version. It inspects the query parameters and request headers. Whichever is most recent is the version to use.
func (*VersionManager) Versions ¶
func (vm *VersionManager) Versions() []string
Versions returns a list of all versions as strings.
type Versionable ¶
type Versionable interface { // Data returns the object as it stands in the latest version. Data() map[string]interface{} }
Versionable represents an object that is subject to versioning. An object must implement Versionable in order to be affected.