myapi

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2021 License: MIT Imports: 11 Imported by: 0

README

MyAPI

A rest API framework aimed at isolating clientside errors in a single layer so developers can focus on cleaner code in other layers. WIP

Documentation

Index

Constants

This section is empty.

Variables

Group PropertyGroup type variable.

Functions

This section is empty.

Types

type CustomRule

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

CustomRule a rule type that allows users to define their own rule.

func NewCustomRule

func NewCustomRule(n string, typ Type, v func(interface{}) (bool, string)) CustomRule

NewCustomRule creates a new custom rule that can be applied to properties.

func (*CustomRule) SetDescription

func (cr *CustomRule) SetDescription(desc string)

SetDescription sets the rules description.

type EnumRule

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

EnumRule checks to see if the property value is within a set of valid values.

func NewEnumRule

func NewEnumRule(members []interface{}, t Type) (EnumRule, error)

NewEnumRule checks to see if all members provided are the same type, if so, it will return a valid EnumRule. if not, it will return a blank rule and a error. type is inferred by checking the first member of the array.

type Middleware

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

Middleware represents a middleware function that uses http.Handler

func NewMiddleware

func NewMiddleware(name string, handler func(http.Handler) http.Handler) Middleware

NewMiddleware returns a new middleware with the name and handler provided.

func (*Middleware) SetDescription

func (m *Middleware) SetDescription(desc string)

SetDescription set the description of the middleware

type MyAPI

type MyAPI struct {
	Name string

	Routes []Route

	Port string
	// contains filtered or unexported fields
}

MyAPI ...

func NewMyAPI

func NewMyAPI(name string, port string, cors bool) MyAPI

NewMyAPI creates a new MyAPI instance.

func (*MyAPI) StartServer

func (m *MyAPI) StartServer() error

StartServer starts the server using the port and managed routers.

func (*MyAPI) StartTestServer

func (m *MyAPI) StartTestServer() (*httptest.Server, error)

StartTestServer starts the server using httptest.NewServer() instead of http.ListenAndServe for testing purposes.

func (*MyAPI) UseMiddleware

func (m *MyAPI) UseMiddleware(mw Middleware)

UseMiddleware applies the middleware provided to the router. this middleware is used for all routes.

func (*MyAPI) UseRoute

func (m *MyAPI) UseRoute(r Route) error

UseRoute applies an individual route to the api that is not part of any subrouter.

func (*MyAPI) UseSubrouter

func (m *MyAPI) UseSubrouter(sr SubRouter) error

UseSubrouter applies a SubRouter to the api.

type ObjectProperty

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

ObjectProperty represents a property that would be an object type in json instead of a basic type. contains a group of properties that will validate the items contained in the json object.

func NewObjectProperty

func NewObjectProperty(name string, slice bool) ObjectProperty

NewObjectProperty creates a new Object Property with the name provided and sets the slice

func (*ObjectProperty) UsePropertyGroup

func (o *ObjectProperty) UsePropertyGroup(pg PropertyGroup)

UsePropertyGroup sets the property group on the object.

type Property

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

Property represents a single property in a request body.

func NewProperty

func NewProperty(name string, typ Type) Property

NewProperty creates a property with a blank rule set.

func (*Property) AddRules

func (p *Property) AddRules(rules ...Rule) error

AddRules will take the rules provided and add them to the Property, checking if they are valid first. If not, it will print a msg stating it has been ignored.

type PropertyGroup

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

PropertyGroup wrapper used to be sure property names are unique when applied to a route.

func NewPropertyGroup

func NewPropertyGroup() PropertyGroup

NewPropertyGroup creates a PropertyGroup with no properties.

func PropsFromType

func PropsFromType(t reflect.Type) PropertyGroup

PropsFromType receives a reflect.Type of a struct and returns a propertygroup based of the field name and types of the struct useful for creating propertygroups that dont need any specific rules applied to them. will panic if the provided type's kind is not a struct.

func (*PropertyGroup) AddProperties

func (pg *PropertyGroup) AddProperties(props ...Props) error

AddProperties attempts to add properties to PropertyGroup. It will throw an error if any Properties have conflicting names or aliases.

type Props

type Props interface {
	// contains filtered or unexported methods
}

Props the interface that should represent a single property in a json object. currently Property and Object Property implement it.

type RegexRule

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

RegexRule checks to see if the propety value of a property matches the provided regex string.

func NewRegexRule

func NewRegexRule(str string) (RegexRule, error)

NewRegexRule accepts a string, confirms it's a valid regex pattern, and returns a rule if the pattern is valid. If it is not, it will return a blank rule and an error noting that the regex is invalid.

type Route

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

Route describes a single route in the API.

func NewRoute

func NewRoute(path string, Handler http.HandlerFunc, method ...string) Route

NewRoute takes in all neccesay parts of a route: path method and handler function. other fields can be set via other functions.

func (*Route) AddProperty

func (r *Route) AddProperty(p ...Props) error

AddProperty adds all provided properties to the route. this will throw an error if a property name is duplicated.

func (*Route) AddPropertyGroup

func (r *Route) AddPropertyGroup(pg PropertyGroup)

AddPropertyGroup adds an entire propertygroup to the route properties note: this will replace any current properties on the route.

func (*Route) SetDescription

func (r *Route) SetDescription(description string)

SetDescription sets the description field on the Route.

type Rule

type Rule interface {
	// contains filtered or unexported methods
}

Rule Interface that defines the common interfaced that should be used when implementing a rule type all implemented rules can assume that the provided value is already an appropriate type, because the value will have been type checked before and invalid ones will not make it to the rule. NOTE: rules cannot be applied to the Object Property Type.

type SubRouter

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

SubRouter a grouping of routes. all routes added to the subrouter will be prefixed with the prefix string.

func NewSubRouter

func NewSubRouter(p string) SubRouter

NewSubRouter returns an empty subrouter with the provided prefix

func (*SubRouter) AddRoute

func (sr *SubRouter) AddRoute(r Route)

AddRoute adds the route provided to the router.

func (*SubRouter) UseMiddleware

func (sr *SubRouter) UseMiddleware(mw Middleware)

UseMiddleware applies the middleware to the provided

type Type

type Type reflect.Type

Type used as a wrapper for reflect.Types in case I wish to change how types are defined in the package. variables to be used with property are exported with this Type.

var Boolean Type = reflect.TypeOf(true)

Boolean boolean type variable.

var Float Type = reflect.TypeOf(1.12)

Float float type variable.

var Int Type = reflect.TypeOf(1)

Int int type variable.

var String Type = reflect.TypeOf("")

String string type variable.

Jump to

Keyboard shortcuts

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