argo

package module
v0.0.0-...-d4fae16 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2014 License: MIT Imports: 13 Imported by: 0

README

Argo

Build Status

A REST API in Go.

Given an aspect schema:

import (
    sql "github.com/aodin/aspect"
)

var usersDB = sql.Table("users",
    sql.Column("id", sql.Integer{}),
    sql.Column("name", sql.String{}),
    sql.Column("age", sql.Integer{}),
    sql.Column("password", sql.String{}),
    sql.PrimaryKey("id"),
)

Create a REST resource with the table and a aspect.Connection:

users := Resource(
    conn,
    Table(usersDB),
)

Happy Hacking,

aodin

Documentation

Index

Constants

View Source
const (
	GET     method = "GET"
	POST    method = "POST"
	PUT     method = "PUT"
	PATCH   method = "PATCH"
	DELETE  method = "DELETE"
	OPTIONS method = "OPTIONS"
)

Variables

This section is empty.

Functions

func FixValues

func FixValues(results ...sql.Values)

FixValues converts []byte types to string, since sql.Values objects JSON encode []byte as base64.

func InvalidName

func InvalidName(name string) error

Types

type API

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

func New

func New() *API

New creates a new API at root '/'

func NewSQL

func NewSQL(conn sql.Connection) *API

func (*API) Add

func (api *API) Add(resource *ResourceSQL) error

Add adds the SQL resource to the API using its name

func (*API) AddRest

func (api *API) AddRest(name string, resource Rest, keys ...string) error

AddRest adds the Rest-ful resource to the API

func (*API) Handle

func (api *API) Handle(w http.ResponseWriter, request *Request)

Handle makes an API implement a handler with an argo Request instance

func (*API) Prefix

func (api *API) Prefix() string

func (*API) ServeHTTP

func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http Handler interface for APIs

func (*API) SetPrefix

func (api *API) SetPrefix(prefix string) *API

type APIError

type APIError struct {
	Meta   []string          `json:"meta"`
	Fields map[string]string `json:"fields"`
	// contains filtered or unexported fields
}

APIError is an error structure with meta and field-specific errors

func HasRequired

func HasRequired(values sql.Values, keys ...string) *APIError

HasRequired confirms that all requested keys exist in the given values map.

func MetaError

func MetaError(code int, msg string, args ...interface{}) *APIError

MetaError returns an API error with a pre-set meta error

func NewError

func NewError(code int) *APIError

New creates a new empty API error scaffold

func ValidateUsing

func ValidateUsing(values sql.Values, types map[string]Validator) *APIError

func (*APIError) AddMeta

func (e *APIError) AddMeta(msg string, args ...interface{})

AddMeta appends a meta error

func (APIError) Code

func (e APIError) Code() int

Code returns the status code that should be written to the response

func (APIError) Error

func (e APIError) Error() string

Error implements the error interface

func (APIError) Exists

func (e APIError) Exists() bool

Exists returns true if there are either meta or fields errors

func (*APIError) SetField

func (e *APIError) SetField(field, msg string, args ...interface{})

SetField sets the error message for the field. Mutiple calls will overwrite previous messages for that field.

func (APIError) Write

func (e APIError) Write(w http.ResponseWriter, encoder Encoder)

Write writes the error to the response using the given encoder

type ClosingBuffer

type ClosingBuffer struct {
	*bytes.Buffer
}

func (ClosingBuffer) Close

func (cb ClosingBuffer) Close() (err error)

type ColumnField

type ColumnField struct {
	Name string
	// contains filtered or unexported fields
}

type Columns

type Columns map[string]sql.ColumnElem

Columns maintains a set of columns by name

func ColumnSet

func ColumnSet(columns ...sql.ColumnElem) Columns

func (Columns) Add

func (set Columns) Add(c sql.ColumnElem) error

func (Columns) Has

func (set Columns) Has(name string) bool

func (Columns) Remove

func (set Columns) Remove(name string) error

func (Columns) Selectable

func (set Columns) Selectable() []sql.ColumnElem

Selectable implement's aspect's Selectable interface so the set can be queried directly.

type Decoder

type Decoder interface {
	Decode(io.Reader) (sql.Values, *APIError)
}

Decoder is the common decoding interface

func GetDecoder

func GetDecoder(r *http.Request) Decoder

GetDecoder matches the request Content-Type header with a Decoder.

type Encoder

type Encoder interface {
	Encode(interface{}) []byte // Our responses should never error
	MediaType() string
}

Encoder is the common encoding interface

func GetEncoder

func GetEncoder(r *http.Request) Encoder

GetEncoder matches the request Accept-Encoding header with an Encoder. TODO this could be done with routes / headers / auth

type EqualsFilter

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

func (EqualsFilter) Filter

func (s EqualsFilter) Filter(v string) sql.Clause

type Field

type Field interface {
	IsRequired() bool
	Validate(interface{}) (interface{}, error)
}

type Fields

type Fields map[string]ColumnField

Fields maintains a set of fields by name

func FieldsFromColumns

func FieldsFromColumns(columns ...sql.ColumnElem) Fields

func (Fields) Add

func (set Fields) Add(f ColumnField) error

func (Fields) Has

func (set Fields) Has(name string) bool

func (Fields) Remove

func (set Fields) Remove(name string) error

type Filter

type Filter interface {
	Filter(string) sql.Clause
}

type Handle

type Handle interface {
	Rest
}

Handle is an alias for Rest

type Include

type Include interface {
	Query(sql.Connection, sql.Values) error
	QueryAll(sql.Connection, []sql.Values) error
}

type JSON

type JSON struct{}

JSON implements JSON encoding and decoding

func (JSON) Decode

func (c JSON) Decode(data io.Reader) (sql.Values, *APIError)

func (JSON) Encode

func (c JSON) Encode(i interface{}) []byte

func (JSON) MediaType

func (c JSON) MediaType() string

type ManyElem

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

ManyElem is the internal representation of an included Many resource. The included table must be connected by a Foreign key to the parent resource table.

func Many

func Many(name string, table *sql.TableElem) ManyElem

Many creates a new Many respresentation of the given table at the given name

func (ManyElem) AsMap

func (elem ManyElem) AsMap(key, value string) ManyElem

AsMap converts the list of many elements into a map of the given key: value.

func (ManyElem) DetailOnly

func (elem ManyElem) DetailOnly() ManyElem

DetailOnly will attach the ManyElem to only the detail views of the API.

func (ManyElem) Exclude

func (elem ManyElem) Exclude(names ...string) ManyElem

Exclude removes the given fields by name from the included ManyElem.

func (ManyElem) Modify

func (elem ManyElem) Modify(resource *ResourceSQL) error

Modify implements the Modifier resource that allows an element to modify a resource. It will add the ManyElem to the list of included elements for the given resource.

func (ManyElem) Query

func (elem ManyElem) Query(conn sql.Connection, values sql.Values) error

Query is the database query method used for single result detail methods.

func (ManyElem) QueryAll

func (elem ManyElem) QueryAll(c sql.Connection, values []sql.Values) error

QueryAll is the database query method used for building a many relationship with many tables.

type ManyToManyElem

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

func ManyToMany

func ManyToMany(name string, table, through *sql.TableElem) ManyToManyElem

func (ManyToManyElem) DetailOnly

func (elem ManyToManyElem) DetailOnly() ManyToManyElem

func (ManyToManyElem) Exclude

func (elem ManyToManyElem) Exclude(names ...string) ManyToManyElem

Exclude removes fields on the element table from the query

func (ManyToManyElem) IncludeThrough

func (elem ManyToManyElem) IncludeThrough(names ...string) ManyToManyElem

IncludeThrough adds fields from the through table to the query

func (ManyToManyElem) Modify

func (elem ManyToManyElem) Modify(resource *ResourceSQL) error

func (ManyToManyElem) Query

func (elem ManyToManyElem) Query(c sql.Connection, values sql.Values) error

Query is the database query method used for single result detail methods.

func (ManyToManyElem) QueryAll

func (elem ManyToManyElem) QueryAll(c sql.Connection, v []sql.Values) error

QueryAll is the database query method used for multiple result list methods.

type Meta

type Meta struct {
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
	// contains filtered or unexported fields
}

type Modifier

type Modifier interface {
	Modify(*ResourceSQL) error
}

type MultiResponse

type MultiResponse struct {
	Meta    Meta        `json:"meta"`
	Results interface{} `json:"results"`
}

type OptionalType

type OptionalType struct {
	sql.Type
}

OptionalType wraps a database field. It keeps the type's underlying validation while marking it as optional.

func MakeOptional

func MakeOptional(t sql.Type) OptionalType

func (OptionalType) IsRequired

func (opt OptionalType) IsRequired() bool

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) string

ByName returns the value of the first Param which key matches the given name If no matching Param is found, an empty string is returned.

type Request

type Request struct {
	*http.Request
	Encoding Encoder
	Decoding Decoder
	Params   Params
	Values   url.Values
}

func MockRequest

func MockRequest(b []byte, v url.Values, ids ...interface{}) *Request

func (*Request) Decode

func (r *Request) Decode(data io.Reader) (sql.Values, *APIError)

func (*Request) Get

func (r *Request) Get(key string) string

Get gets a GET parameter and ONLY a get parameter - never POST form data

func (*Request) QueryValues

func (r *Request) QueryValues() url.Values

type ResourceSQL

type ResourceSQL struct {
	Name string
	// contains filtered or unexported fields
}

ResourceSQL is the internal representation of a REST resource backed by SQL.

func Resource

func Resource(t TableElem, fields ...Modifier) *ResourceSQL

Resource created a new ResourceSQL from the given table and modifiers. Panic on errors.

func (*ResourceSQL) Delete

func (c *ResourceSQL) Delete(r *Request) (Response, *APIError)

func (*ResourceSQL) Get

func (c *ResourceSQL) Get(r *Request) (Response, *APIError)

func (*ResourceSQL) HasRequired

func (c *ResourceSQL) HasRequired(values sql.Values) *APIError

func (*ResourceSQL) List

func (c *ResourceSQL) List(r *Request) (Response, *APIError)

List returns the collection view of this sql resource.

func (*ResourceSQL) Patch

func (c *ResourceSQL) Patch(r *Request) (Response, *APIError)

func (*ResourceSQL) Post

func (c *ResourceSQL) Post(r *Request) (Response, *APIError)

func (*ResourceSQL) Validate

func (c *ResourceSQL) Validate(values sql.Values) *APIError

type Response

type Response interface{}

type Rest

type Rest interface {
	List(*Request) (Response, *APIError)
	Post(*Request) (Response, *APIError)
	Get(*Request) (Response, *APIError)
	Patch(*Request) (Response, *APIError)
	Delete(*Request) (Response, *APIError)
}

Rest is the common interface for REST-ful resources

type StringFilter

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

func (StringFilter) Filter

func (s StringFilter) Filter(v string) sql.Clause

type TableElem

type TableElem struct {
	Name string
	// contains filtered or unexported fields
}

TableElem wraps the given SQL table. The sql.TableElem is not directly added to the resource in case we want to provide options / modifiers before altering the ResourceSQL (see the Resource() constructor)

func FromTable

func FromTable(table *sql.TableElem) (elem TableElem)

func (TableElem) Exclude

func (elem TableElem) Exclude(names ...string) TableElem

Exclude removes the given field names from the selects

func (TableElem) Slugify

func (elem TableElem) Slugify() TableElem

Slugify makes the table name URL friendly, allowing only lowercase characters and dashes.

type Validator

type Validator interface {
	IsRequired() bool
	Validate(interface{}) (interface{}, error)
}

type YAML

type YAML struct{}

YAML implements YAML encoding and decoding

func (YAML) Decode

func (c YAML) Decode(data io.Reader) (sql.Values, *APIError)

func (YAML) Encode

func (c YAML) Encode(i interface{}) []byte

func (YAML) MediaType

func (c YAML) MediaType() string

Jump to

Keyboard shortcuts

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