Documentation ¶
Index ¶
- Constants
- Variables
- func EnablePostman()
- func ModelDataFaker(schema *schema.Schema) interface{}
- func OnAfterCreate(fn func(obj any, c *Context) error)
- func OnAfterDelete(fn func(obj any, c *Context) error)
- func OnAfterGet(fn func(obj any, c *Context) error)
- func OnAfterSave(fn func(obj any, c *Context) error)
- func OnAfterUpdate(fn func(obj any, c *Context) error)
- func OnBeforeCreate(fn func(obj any, c *Context) error)
- func OnBeforeDelete(fn func(obj any, c *Context) error)
- func OnBeforeSave(fn func(obj any, c *Context) error)
- func OnBeforeUpdate(fn func(obj any, c *Context) error)
- func PrettyJson(v interface{}) string
- func SetDefaultPermissionHandler(handler func(permissions Permissions, context *Context) bool)
- func SetPostmanAuthorization(_type AuthType, _value ...string)
- func SetPrefix(prefix string)
- type API
- type App
- type AuthType
- type Condition
- type Context
- func (context *Context) AddValidationErrors(errs ...error)
- func (context *Context) ApplyFilters(query *gorm.DB) (*gorm.DB, *Error)
- func (context *Context) CreateIndirectObject() reflect.Value
- func (context *Context) CreateIndirectSlice() reflect.Value
- func (context *Context) Error(err error, code int) *Error
- func (context *Context) FindByPrimaryKey(input interface{}) (bool, *Error)
- func (context *Context) GetDBO() *gorm.DB
- func (context *Context) HandleError(error *Error)
- func (context *Context) Override(value any)
- func (context *Context) PrepareResponse() *Pagination
- func (context *Context) RestPermission(permission Permission, object reflect.Value) bool
- func (context *Context) SetCondition(condition string, op string, value any)
- func (context *Context) SetResponse(response interface{})
- func (context *Context) Validate(ptr any) error
- func (context *Context) ValidateNonZeroFields(ptr any) error
- type Controller
- type Dictionary
- type DisableAggregate
- type DisableCreate
- type DisableDelete
- type DisableList
- type DisableSet
- type DisableUpdate
- type Endpoint
- type Error
- type Feature
- type Field
- type Filter
- type Handler
- func (h Handler) Aggregate(context *Context) *Error
- func (Handler) All(context *Context) *Error
- func (Handler) BatchCreate(context *Context) *Error
- func (h Handler) BatchDelete(context *Context) *Error
- func (Handler) BatchUpdate(context *Context) *Error
- func (Handler) Create(context *Context) *Error
- func (Handler) Delete(context *Context) *Error
- func (Handler) Get(context *Context) *Error
- func (Handler) ModelInfo(context *Context) *Error
- func (Handler) Paginate(context *Context) *Error
- func (h Handler) Set(context *Context) *Error
- func (Handler) Update(context *Context) *Error
- type Info
- type KeyValue
- type Method
- type Pagination
- func (p *Pagination) Create(model *gorm.DB, i interface{}, request *evo.Request) error
- func (p *Pagination) GetOffset() int
- func (p *Pagination) GetPage() int
- func (p *Pagination) SetCurrentPage(page int)
- func (p *Pagination) SetLast()
- func (p *Pagination) SetLimit(limit int)
- func (p *Pagination) SetPageRange()
- func (p *Pagination) SetPages()
- type Permission
- type Permissions
- type Resource
- type ValidationError
Constants ¶
const ( ContainOperator = "contains" NotNullOperator = "notnull" IsNullOperator = "isnull" InOperator = "in" )
ContainOperator represents the string value "contains" which is used as an operator for containment operations. Examples of containment operations could be checking if a string contains a specific substring or if an array contains a specific element. This constant is used to indicate the containment operator in code logic. NotNullOperator represents the string value "notnull" which is used as an operator for checking if a value is not null. This constant is used to indicate the not null operator in code logic. IsNullOperator represents the string value "isnull" which is used as an operator for checking if a value is null. This constant is used to indicate the is null operator in code logic. InOperator represents the string
const ( MethodGET Method = "GET" MethodPOST Method = "POST" MethodPatch Method = "PATCH" MethodPUT Method = "PUT" MethodDELETE Method = "DELETE" PermissionsModelInfo Permission = "VIEW+MODEL_INFO" PermissionCreate Permission = "CREATE" PermissionUpdate Permission = "UPDATE" PermissionBatchCreate Permission = "BATCH+CREATE" PermissionBatchUpdate Permission = "BATCH+UPDATE" PermissionDelete Permission = "DELETE" PermissionBatchDelete Permission = "BATCH+DELETE" PermissionViewGet Permission = "VIEW+GET" PermissionViewAll Permission = "VIEW+ALL" PermissionAggregate Permission = "VIEW+AGGREGATE" PermissionViewPagination Permission = "VIEW+PAGINATION" PermissionSet Permission = "SET" )
Variables ¶
var ErrorColumnNotExist = NewError("column does not exists", 500)
ErrorColumnNotExist represents an error indicating that a column does not exist.
var ErrorHandlerNotFound = NewError("handler not found", 404)
var ErrorObjectNotExist = NewError("object does not exists", 404)
ErrorObjectNotExist represents an error indicating that the object does not exist.
var ErrorPermissionDenied = NewError("permission denied", 403)
var ErrorUnsafe = NewError("unsafe request", 400)
var Prefix = "/admin/rest"
Functions ¶
func EnablePostman ¶
func EnablePostman()
func ModelDataFaker ¶
func OnAfterCreate ¶
func OnAfterDelete ¶
func OnAfterGet ¶
func OnAfterSave ¶
func OnAfterUpdate ¶
func OnBeforeCreate ¶
func OnBeforeDelete ¶
func OnBeforeSave ¶
func OnBeforeUpdate ¶
func PrettyJson ¶
func PrettyJson(v interface{}) string
func SetDefaultPermissionHandler ¶
func SetDefaultPermissionHandler(handler func(permissions Permissions, context *Context) bool)
func SetPostmanAuthorization ¶
Types ¶
type AuthType ¶
type AuthType string
const ( AuthTypeNone AuthType = "none" AuthTypeBasic AuthType = "basic" AuthTypeBearer AuthType = "bearer" AuthTypeDigest AuthType = "digest" AuthTypeEdgeGrid AuthType = "edgegrid" AuthTypeHawk AuthType = "hawk" AuthTypeOAuth1 AuthType = "oauth1" AuthTypeOAuth2 AuthType = "oauth2" AuthTypeNTLM AuthType = "ntlm" AuthTypeHeader AuthType = "header" )
type Context ¶
type Context struct { Request *evo.Request Object reflect.Value Sample interface{} Action *Endpoint Response *Pagination Schema *schema.Schema Conditions []Condition Code int // contains filtered or unexported fields }
Context represents the context of an HTTP request. It contains information about the request, the object being processed, the sample data, the action to be performed, the response, and the schema.
func (*Context) AddValidationErrors ¶
func (*Context) ApplyFilters ¶
ApplyFilters applies filters to the query based on the request parameters in the context. It modifies the
func (*Context) CreateIndirectObject ¶
CreateIndirectObject is a method of the Context type that returns a new indirect reflect.Value of the context Object's type.
func (*Context) CreateIndirectSlice ¶
CreateIndirectSlice returns a new indirect reflect value of a slice of the type of the Object field in the Context.
func (*Context) FindByPrimaryKey ¶
FindByPrimaryKey is a method that searches for a record in the database based on the primary key values provided. The method takes an input parameter, which can be a struct or a
func (*Context) HandleError ¶
HandleError is a method of the Context type that sets the error message in the Response field and marks the Response as unsuccessful. It takes an error parameter.
func (*Context) PrepareResponse ¶
func (context *Context) PrepareResponse() *Pagination
PrepareResponse is a method of the Context type. It returns the response object of the context. If the response is not successful (success flag is false), it sets the data, size, page, total, total pages, and offset fields of the response to 0. It then returns the response object.
func (*Context) RestPermission ¶
func (context *Context) RestPermission(permission Permission, object reflect.Value) bool
func (*Context) SetCondition ¶
func (*Context) SetResponse ¶
func (context *Context) SetResponse(response interface{})
SetResponse is a method of the Context type that sets the response data. It takes a response interface{} as a parameter and marshals it to JSON. If the response is nil, it returns immediately. If the response is not a slice, it marshals it as a single element slice. Otherwise, it marshals the response as is. If there is an error during marshaling, it returns immediately without setting the response. Note: The response is set to the `context.Request` using the `JSON` method.
func (*Context) ValidateNonZeroFields ¶
type Controller ¶
type Controller struct{}
Controller represents a controller type.
func (Controller) ModelsHandler ¶
func (c Controller) ModelsHandler(request *evo.Request) interface{}
ModelsHandler returns all registered models.
func (Controller) PostmanHandler ¶
func (c Controller) PostmanHandler(request *evo.Request) any
type Dictionary ¶
Dictionary represents a collection of key-value pairs with a generic value type.
func (*Dictionary[T]) ContainsValue ¶
func (d *Dictionary[T]) ContainsValue(v T) (bool, KeyValue[T])
ContainsValue checks if a value exists in the dictionary and returns the KeyValue if found.
func (*Dictionary[T]) Delete ¶
func (d *Dictionary[T]) Delete(key string) error
Delete removes a key (and its associated value) from the dictionary.
func (*Dictionary[T]) Has ¶
func (d *Dictionary[T]) Has(key string) bool
Has checks if a key exists in the dictionary.
func (*Dictionary[T]) Set ¶
func (d *Dictionary[T]) Set(key string, v T)
Set sets a key to a value in the dictionary, updating if it already exists.
type DisableAggregate ¶
type DisableAggregate struct{}
DisableAggregate is a flag to disable aggregation endpoints.
type DisableCreate ¶
type DisableCreate struct{}
DisableCreate is a flag to disable the creation of new objects.
type DisableDelete ¶
type DisableDelete struct{}
DisableDelete is a flag to disable the deletion of existing objects
type DisableUpdate ¶
type DisableUpdate struct{}
DisableUpdate is a flag to disable update of existing objects.
type Endpoint ¶
type Endpoint struct { Name string `json:"name"` Label string `json:"-"` Method Method `json:"method"` URL string `json:"-"` PKUrl bool `json:"pk_url"` AbsoluteURI string `json:"url"` Description string `json:"description"` Handler func(context *Context) *Error `json:"-"` Resource *Resource `json:"-"` URLParams []Filter `json:"-"` Batch bool `json:"batch"` AcceptData bool `json:"accept_data"` Filterable bool `json:"filterable"` Pagination bool `json:"pagination"` PostmanCollection postman.Collection `json:"-"` }
func (*Endpoint) GenerateDescription ¶
func (*Endpoint) RegisterRouter ¶
func (action *Endpoint) RegisterRouter()
type Feature ¶
type Feature struct { DisableCreate bool DisableUpdate bool DisableList bool DisableDelete bool DisableSet bool DisableAggregate bool API bool }
Feature struct represents a set of features that can be enabled or disabled. Each feature has a corresponding boolean field that indicates whether it is enabled or disabled.
func GetFeatures ¶
func GetFeatures(v interface{}) Feature
type Field ¶
type Field struct { Name string `json:"label"` FieldName string `json:"-"` DBName string `json:"name,omitempty"` Type string `json:"type,omitempty"` Default string `json:"default,omitempty"` PK bool `json:"pk,omitempty"` }
Field represents a field in a data structure. It contains metadata about the field, such as its name, database name, type, default value, and whether it is a primary key.
type Filter ¶
type Filter struct { Title string `json:"title,omitempty"` Type string `json:"type,omitempty"` Options Dictionary[string] `json:"options,omitempty"` Name string `json:"name,omitempty"` Filter string `json:"-"` }
Filter represents a filter for data retrieval. It contains the following properties:
- Title: the title of the filter. - Type: the type of the filter. - Options: dictionary of options for the filter. - Name: the name of the filter. - Filter: the filter condition to be applied.
type Handler ¶
type Handler struct { }
func (Handler) All ¶
All queries the database and retrieves all objects based on the given context. It applies filters, handles OnBefore and OnAfter events, and sets the response. It returns an error if any occurred during the process.
func (Handler) BatchCreate ¶
func (Handler) BatchDelete ¶
BatchDelete delete multiple objects in the database
func (Handler) BatchUpdate ¶
func (Handler) Create ¶
Create takes a Context as input and creates a new object. It uses the context's Request and DBO to perform the creation. The object to be created is retrieved from the context's Object field. The object is parsed from the request's body using the BodyParser method. The object can optionally implement the OnBeforeCreate method, which is called OnBefore the creation. The object can optionally implement the ValidateCreate method, which is called to validate the object OnBefore creation. The object is then created in the database using the DBO's Create method. If the object implements the OnAfterCreate method, it is called OnAfter the creation. The created object is set as the data in the context's Response field. Returns an error if any error occurs during the creation process.
func (Handler) Delete ¶
Delete deletes an object from the database. It takes a Context pointer as a parameter. It returns an error if an error occurs during the deletion process.
func (Handler) Get ¶
Get is a function that retrieves an object from the context. It performs pre- and post-get operations on the object if they are implemented. It finds the object by its primary key, sets it as the response data in the context, and returns nil if successful. If the object does not exist, it returns an error of type ErrorObjectNotExist. It returns an error if any operation fails.
func (Handler) Paginate ¶
Paginate applies pagination to a database query based on the context provided. It modifies the context's response object with the paginated data.
func (Handler) Set ¶
Set updates the collection by creating new items that don't already exist and removing any items that are not present in the provided list.
func (Handler) Update ¶
Update updates an object in the database based on the provided context. It retrieves the database object, checks if it exists in the database, parses the request body to update the object, and executes the updates on the database. It also calls the OnBeforeUpdate and ValidateUpdate methods if they are implemented by the object to perform any necessary operations OnBefore and OnAfter the update. Finally, it sets the updated object as the response data in the context.
type Info ¶
type Info struct { Name string `json:"name,omitempty"` ID string `json:"id,omitempty"` Fields []Field `json:"fields,omitempty"` Endpoints []*Endpoint `json:"endpoints,omitempty"` }
Info represents a structured information object.
It contains the following fields: - Name: The name of the object - ID: The ID of the object - Fields: An array of Field objects that represent the fields of the object - Endpoints: An array of Endpoint objects that represent the endpoints associated with the object.
type Pagination ¶
type Pagination struct { Records int `json:"records,omitempty"` // Total rows Pages int `json:"pages,omitempty"` // total number of pages Limit int `json:"limit,omitempty"` // number of rows per page First int `json:"first,omitempty"` // First Page Last int `json:"last,omitempty"` // Last Page PageRange []int `json:"page_range,omitempty"` // Range of visible pages Data interface{} `json:"data,omitempty"` Total int64 `json:"total"` Offset int `json:"offset"` TotalPages int `json:"total_pages"` Page int `json:"current_page"` Size int `json:"size"` Success bool `json:"success"` Error string `json:"error"` Type string `json:"type"` ValidationError []ValidationError `json:"validation_error"` }
Pagination represents a utility type for handling pagination in Go.
Fields: - Records: Total number of rows. - CurrentPage: Current page loaded. - Pages: Total number of pages. - Limit: Number of rows per page. - First: First page. - Last: Last page. - PageRange: Range of visible pages.
Methods: - SetCurrentPage: Sets the current page based on the provided value. If the value is 0, the current page is set to 1. - SetLimit: Sets the limit of rows per page. If the value is 0, the limit is set to the minimum limit of 10. If the limit is less than the minimum limit, it is set to the minimum
func (*Pagination) Create ¶
func (p *Pagination) Create(model *gorm.DB, i interface{}, request *evo.Request) error
func (*Pagination) GetOffset ¶
func (p *Pagination) GetOffset() int
GetOffset calculates the offset for paginating the data based on the current page and limit
func (*Pagination) GetPage ¶
func (p *Pagination) GetPage() int
GetPage returns the current page of the pagination struct
func (*Pagination) SetCurrentPage ¶
func (p *Pagination) SetCurrentPage(page int)
SetCurrentPage sets the value of CurrentPage in the Pagination struct. If the input page is not equal to zero, p.CurrentPage will be set to the input page. Otherwise, p.CurrentPage will be set to 1.
func (*Pagination) SetLast ¶
func (p *Pagination) SetLast()
SetLast sets the value of the Last page in the pagination struct. It calculates the value by adding the current offset to the limit. If the calculated value is greater than the total number of records, it sets the Last page to the total number of records.
func (*Pagination) SetLimit ¶
func (p *Pagination) SetLimit(limit int)
SetLimit sets the limit per page for the pagination struct. The limit must be between 10 and 100 (inclusive). If the limit is 0, it will be set to 10. If the limit is less than 10
func (*Pagination) SetPageRange ¶
func (p *Pagination) SetPageRange()
SetPageRange sets the value of PageRange in the Pagination struct. It determines the range of visible pages based on the current page and the total number of pages.
func (*Pagination) SetPages ¶
func (p *Pagination) SetPages()
SetPages sets the total number of pages in the pagination struct based on the number of records and the limit per page. If the number of records is 0, it sets the number of pages to 1. If there is no remainder when dividing the number of records by the limit, it sets the number of pages to the integer division. Otherwise, it sets the number of pages to the integer division plus 1. If the number of pages is 0, it sets it to 1. After setting the number of pages, it calls the SetLast and SetPageRange methods to update the last page indicator and the range of visible pages respectively.
type Permission ¶
type Permission string
func (Permission) ToPermissions ¶
func (p Permission) ToPermissions() Permissions
type Permissions ¶
type Permissions []string
func (Permissions) Has ¶
func (p Permissions) Has(perms ...string) bool
type Resource ¶
type Resource struct { Instance any `json:"-"` PrimaryFieldDBNames []string `json:"primary_key"` Actions []*Endpoint `json:"actions"` Schema *schema.Schema `json:"-"` Type reflect.Type `json:"-"` Ref reflect.Value `json:"-"` Table string `json:"table"` Path string `json:"path"` Name string `json:"model"` Feature Feature `json:"feature"` PostmanGroup *postman.Item `json:"-"` }
Resource represents a resource in an API. It holds information about the object, actions, path, schema, table, name, model, JavaScript model, and parameters of the resource.