internal

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StructTag Annotation strings
	AnnotationNeuron = "neuron"

	// type tag
	AnnotationPrimary      = "primary"
	AnnotationPrimaryFull  = "primary_key"
	AnnotationPrimaryFullS = "primarykey"
	AnnotationID           = "id"

	AnnotationClientID = "client-id"
	AnnotationLanguage = "langtag"

	// attributes
	AnnotationAttribute     = "attr"
	AnnotationAttributeFull = "attribute"

	AnnotationRelation     = "relation"
	AnnotationRelationFull = "relationship"

	AnnotationFilterKey = "filterkey"

	AnnotationForeignKey      = "foreign"
	AnnotationForeignKeyFull  = "foreign_key"
	AnnotationForeignKeyFullS = "foreignkey"

	AnnotationNestedField = "nested"

	// relation tag
	AnnotationManyToMany     = "many2many"
	AnnotationRelationNoSync = "nosync"
	AnnotationRelationSync   = "sync"

	AnnotationRelationRestrict = "restrict"
	AnnotationRelationNoAction = "no-action"
	AnnotationRelationCascade  = "cascade"
	AnnotationRelationSetNull  = "set-null"

	AnnotationDefault = "default"

	// name tag
	AnnotationName = "name"

	// flags tag
	AnnotationHidden      = "hidden"
	AnnotationISO8601     = "iso8601"
	AnnotationOmitEmpty   = "omitempty"
	AnnotationI18n        = "i18n"
	AnnotationFieldType   = "type"
	AnnotationFlags       = "flags"
	AnnotationNoFilter    = "nofilter"
	AnnotationNotSortable = "nosort"

	AnnotationSeperator         = ","
	AnnotationRelationSeperator = ":"
	AnnotationTagSeperator      = ";"
	AnnotationTagEqual          = '='

	AnnotationNestedSeperator = "."
	AnnotationOpenedBracket   = '['
	AnnotationClosedBracket   = ']'

	HeaderAcceptLanguage  = "Accept-Language"
	HeaderContentLanguage = "Content-Language"

	Iso8601TimeFormat = "2006-01-02T15:04:05Z"

	// MediaType is the identifier for the JSON API media type
	//
	// see http://jsonapi.org/format/#document-structure
	MediaType = "application/vnd.api+json"

	// KeyFirstPage is the key to the links object whose value contains a link to
	// the first page of data
	KeyFirstPage = "first"
	// KeyLastPage is the key to the links object whose value contains a link to
	// the last page of data
	KeyLastPage = "last"
	// KeyPreviousPage is the key to the links object whose value contains a link
	// to the previous page of data
	KeyPreviousPage = "prev"
	// KeyNextPage is the key to the links object whose value contains a link to
	// the next page of data
	KeyNextPage = "next"

	// KeyTotalPages is the key to the meta object whose value contains the total
	// pagination pages
	KeyTotalPages = "total-pages"

	// QueryParamPage is a JSON API query parameter used as for pagination.
	QueryParamPage = "page"

	// QueryParamPageNumber is a JSON API query parameter used in a page based
	// pagination strategy in conjunction with QueryParamPageSize
	QueryParamPageNumber = "page[number]"
	// QueryParamPageSize is a JSON API query parameter used in a page based
	// pagination strategy in conjunction with QueryParamPageNumber
	QueryParamPageSize = "page[size]"

	// QueryParamPageOffset is a JSON API query parameter used in an offset based
	// pagination strategy in conjunction with QueryParamPageLimit
	QueryParamPageOffset = "page[offset]"
	// QueryParamPageLimit is a JSON API query parameter used in an offset based
	// pagination strategy in conjunction with QueryParamPageOffset
	QueryParamPageLimit = "page[limit]"

	// QueryParamPageCursor is a JSON API query parameter used with a cursor-based
	// strategy
	QueryParamPageCursor = "page[cursor]"

	// QueryParamPageTotal is a JSON API query parameter used in pagination
	// It tells to API to add information about total-pages or total-count
	// (depending on the current strategy)
	QueryParamPageTotal = "page[total]"

	// QueryParamInclude
	QueryParamInclude = "include"

	// QueryParamSort
	QueryParamSort = "sort"

	// QueryParamFilter
	QueryParamFilter = "filter"

	// QueryParamFields is the const for the query fieldset key
	QueryParamFields = "fields"

	// Preset query Annotation
	QueryParamPreset = "preset"

	// QueryParamLanguage is a JSONAPI query parameter used in selecting a language tag for the
	// model.
	QueryParamLanguage = "lang"

	// QueryParamLinks is a JSONAPI query parameter used in displaying the links of the relationships
	QueryParamLinks = "links"

	IsPointerTime = "jsonapi:is-ptr-time"
)

constants defined for the internal neuron packages

Variables

View Source
var (
	ScopeIDCtxKey    = ScopeIDCtx{}
	ControllerCtxKey = ControllerKeyCtx{}
	TxStateCtxKey    = TransactionStateCtx{}
)

Context

View Source
var (
	ErrUnexpectedType     = errors.New("models should be a struct pointer or slice of struct pointers")
	ErrExpectedSlice      = errors.New("models should be a slice of struct pointers")
	ErrNilValue           = errors.New("Nil value provided.")
	ErrValueNotAddresable = errors.New("Provided value is not addressable")

	// Unmarshal Errors
	ErrClientIDDisallowed = errors.New("Client id is disallowed for this model.")
	// ErrInvalidTime is returned when a struct has a time.Time type field, but
	// the JSON value was not a unix timestamp integer.
	ErrInvalidTime = errors.New("Only numbers can be parsed as dates, unix timestamps")
	// ErrInvalidISO8601 is returned when a struct has a time.Time type field and includes
	// "iso8601" in the tag spec, but the JSON value was not an ISO8601 timestamp string.
	ErrInvalidISO8601 = errors.New("Only strings can be parsed as dates, ISO8601 timestamps")
	// ErrUnknownFieldNumberType is returned when the JSON value was a float
	// (numeric) but the Struct field was a non numeric type (i.e. not int, uint,
	// float, etc)
	ErrUnknownFieldNumberType = errors.New("The struct field was not of a known number type")
	// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
	// the JSON value was of a different type
	ErrUnsupportedPtrType = errors.New("Pointer type in struct is not supported")
	// ErrInvalidType is returned when the given type is incompatible with the expected type.
	ErrInvalidType = errors.New("Invalid type provided") // I wish we used punctuation.

	// ErrBadJSONAPIID is returned when the Struct JSON API annotated "id" field
	// was not a valid numeric type.
	ErrBadJSONAPIID = errors.New(
		"id should be either string, int(8,16,32,64) or uint(8,16,32,64)")

	ErrModelNotMapped = errors.New("Unmapped model provided.")

	ErrFieldNotFound        = errors.New("Field not found")
	ErrFieldAlreadySelected = errors.New("Field already selected.")
)

Errors used in by the internal packages

ControllerCtxFlags - flags settable only for the needs of the controller

View Source
var ModelCtxFlags = []uint{
	flags.AllowClientID,
}

ModelCtxFlags - flags settable for model handler and controller

View Source
var Processes = map[string]struct{}{}

Processes is a validation struct helper for all packages

ScopeCtxFlags - flags settable on scope, endpoint, model handler and controller

View Source
var Verbose = flag.Bool("verbose-api", false, "Used to get verbose data")

Verbose used to get the verbose logs

Functions

func ConvertToSliceInterface

func ConvertToSliceInterface(i *interface{}) ([]interface{}, error)

ConvertToSliceInterface converts the interface value to a []interface{}

func SplitBracketParameter

func SplitBracketParameter(bracketed string) (values []string, err error)

SplitBracketParameter splits the parameters within the '[' and ']' brackets

Types

type BadModel

type BadModel struct {
	ID int `neuron:"typeprimary"`
}

BadModel model with the invalid tags

type Blog

type Blog struct {
	ID            int       `neuron:"type=primary"`
	Title         string    `neuron:"type=attr;name=title"`
	Posts         []*Post   `neuron:"type=relation;name=posts"`
	CurrentPost   *Post     `neuron:"type=relation;name=current_post"`
	CurrentPostID uint64    `neuron:"type=foreign;name=current_post_id"`
	CreatedAt     time.Time `neuron:"type=attr;name=created_at;flags=iso8601"`
	ViewCount     int       `neuron:"type=attr;name=view_count;flags=omitempty"`
}

Blog defines a test model containing multiple relationships to the Post

type Comment

type Comment struct {
	ID     int    `neuron:"type=primary"`
	PostID uint64 `neuron:"type=foreign;name=post_id"`
	Body   string `neuron:"type=attr;name=body"`
}

Comment is a test model related to the post

type ControllerKeyCtx

type ControllerKeyCtx struct{}

ControllerKeyCtx is the common struct used as a controller's key in the context

type ModelNonTagged

type ModelNonTagged struct {
	ID int
}

ModelNonTagged model without the struct tag

type Pet

type Pet struct {
	ID     int     `neuron:"type=primary"`
	Name   string  `neuron:"type=attr;name=name"`
	Owners []*User `neuron:"type=relation;name=owners;relation=many2many,sync,Pets"`
}

Pet defines a testing model that contains attribute and a relation many2many

type Post

type Post struct {
	ID            uint64     `neuron:"type=primary"`
	BlogID        int        `neuron:"type=foreign;name=blog_id"`
	Title         string     `neuron:"type=attr;name=title"`
	Body          string     `neuron:"type=attr;name=body"`
	Comments      []*Comment `neuron:"type=relation;name=comments;foreign=PostID"`
	LatestComment *Comment   `neuron:"type=relation;name=latest_comment;foreign=PostID"`
}

Post is a test model that is related to the blog and comments

type ScopeIDCtx

type ScopeIDCtx struct{}

ScopeIDCtx is the common struct used as a scope's ID in the context

type TransactionStateCtx

type TransactionStateCtx struct{}

TransactionStateCtx is the common struct used as a transaction state key in the scope's context

type User

type User struct {
	ID   int    `neuron:"type=primary"`
	Lang string `neuron:"type=attr;name=lang;flags=langtag"`
	Name string `neuron:"type=attr;name=name"`
	Pets []*Pet `neuron:"type=relation;name=pets;relation=many2many,sync,Owners"`
	// contains filtered or unexported fields
}

User defines a testing model with some private field attributes and many2many relationship

Directories

Path Synopsis
query

Jump to

Keyboard shortcuts

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