Documentation
¶
Index ¶
- Constants
- Variables
- type DeletedAt
- type FilterOperator
- type FilterOption
- type IController
- type IModel
- type IModelAfterBind
- type IModelBeforeBind
- type IModelBeforeRender
- type IModelCreateClause
- type IModelDeleteClause
- type IModelFilterable
- type IModelListClause
- type IModelReadClause
- type IModelReadable
- type IModelSortable
- type IModelUpdatable
- type IModelUpdateClause
- type IRender
- type IResource
- type IResourceDatabase
- type IResourceGetter
- type IResourceInitializer
- type IResourceSetter
- type IService
- type IView
- type Resources
- type SortOption
Constants ¶
const ( ContextDataKey = "rest.data" ContextDataListOptions = ContextDataKey + ".list_options" ContextDataTotal = ContextDataKey + ".total" ContextDataRenderMimetype = ContextDataKey + ".render_mimetype" )
const ( SqliteConstraintNotNull = 1299 SqliteConstraintPrimaryKey = 1555 PostgreSQLNotNullViolation = "23502" PostgreSQLUniqueViolation = "23505" AWSEntityTooLarge = "EntityTooLarge" )
const ( // AcceptSliceCapacity contains the initial capacity for slices to store MIME types and weights. AcceptSliceCapacity = 10 // AcceptQualityWeight corresponds the default quality weight for MIME types. AcceptQualityWeight = 1.0 // AcceptQualityParameter is used to specify quality weight parameter in the header. AcceptQualityParameter = "q" // AcceptSeparator is the used Separator for multiple MIME types in the header. AcceptSeparator = "," // AcceptHeader is the HTTP header key for the Accept header. AcceptHeader = "Accept" // SecWebsocketProtocol is the HTTP header key for the Websocket sub-protocol. SecWebsocketProtocol = "Sec-Websocket-Protocol" )
const ( // ResourceID is the constant for the resource identifier in the path parameter. ResourceID = "id" // ResourcePathWithoutID is the constant for the base path without an ID. ResourcePathWithoutID = "/" // ResourcePathWithID is the constant for the path with an ID parameter. ResourcePathWithID = ResourcePathWithoutID + ":" + ResourceID )
Variables ¶
var ( ErrNotAuthorized = errors.New("not authorized to perform this operation") ErrNotFound = errors.New("resource not found") ErrDuplicatedKey = errors.New("duplicated key") )
var ( // FilterOperators is a map that holds a set of predefined filter operators. // This map is used to check if a given operator is valid. //nolint:gochecknoglobals // Maintain a set of predefined operators that are used throughout the application. FilterOperators = map[FilterOperator]struct{}{ Equals: {}, NotEquals: {}, GreaterThan: {}, GreaterThanOrEqual: {}, LessThan: {}, LessThanOrEqual: {}, Like: {}, Fuzzy: {}, } )
Functions ¶
This section is empty.
Types ¶
type DeletedAt ¶
DeletedAt represents a nullable time type that tracks soft deletion timestamps.
func (DeletedAt) DeleteClauses ¶
DeleteClauses returns the delete clauses for handling soft delete operations in delete statements
func (DeletedAt) QueryClauses ¶
QueryClauses returns the query clauses for handling soft delete operations in queries
type FilterOperator ¶
type FilterOperator string
FilterOperator represents the type of operation to perform for filtering.
const ( // Equals checks if the field value is equal to the specified value. Equals FilterOperator = "eq" // NotEquals checks if the field value is not equal to the specified value. NotEquals FilterOperator = "ne" // GreaterThan checks if the field value is greater than the specified value. GreaterThan FilterOperator = "gt" // GreaterThanOrEqual checks if the field value is greater than or equal to the specified value. GreaterThanOrEqual FilterOperator = "gte" // LessThan checks if the field value is less than the specified value. LessThan FilterOperator = "lt" // LessThanOrEqual checks if the field value is less than or equal to the specified value. LessThanOrEqual FilterOperator = "lte" // Like checks if the field value matches the specified pattern. Like FilterOperator = "like" // Fuzzy checks if the field value approximately matches the specified value. Fuzzy FilterOperator = "fuzzy" )
type FilterOption ¶
type FilterOption struct {
// Field is the name of the field to filter by.
Field string
// Value is the value to filter the field by.
Value string
// Operator is the operator to use for filtering (e.g., equals, less than).
Operator FilterOperator
}
FilterOption represents a single filter criterion for querying.
type IController ¶
type IController interface {
// IResourceGetter is embedded to include methods related to retrieving resources.
IResourceGetter
// IResourceSetter is embedded to include methods related to modifying resources.
IResourceSetter
// Create returns a gin.HandlerFunc that handles the creation of a new resource.
Create() gin.HandlerFunc
// Read returns a gin.HandlerFunc that handles reading a specific resource.
Read() gin.HandlerFunc
// List returns a gin.HandlerFunc that handles listing multiple resources.
List() gin.HandlerFunc
// Update returns a gin.HandlerFunc that handles updating an existing resource.
Update() gin.HandlerFunc
// Delete returns a gin.HandlerFunc that handles deleting a resource.
Delete() gin.HandlerFunc
}
IController defines the contract for a controller in a RESTful API. It includes methods for handling CRUD operations and embedding interfaces for resource getting and setting.
type IModel ¶
type IModel interface {
// GetID returns the unique identifier of the model.
GetID() string
// SetID sets the unique identifier for the model.
SetID(id string)
}
IModel defines the basic methods that a model should implement to interact with its ID.
type IModelAfterBind ¶
type IModelAfterBind interface {
// AfterBind is called after data has been bound to the model from a request context.
// This could be used to validate or modify the data before it is processed further,
// but also to ensure that readonly fields are not processed.
AfterBind(ctx *gin.Context) (err error)
}
IModelAfterBind defines the method that should be called after binding data to the model.
type IModelBeforeBind ¶
type IModelBeforeBind interface {
// BeforeBind is called before data is bound to the model from a request context.
// This could be used to set default values.
BeforeBind(ctx *gin.Context) (err error)
}
IModelBeforeBind defines the method that should be called before binding data to the model.
type IModelBeforeRender ¶
type IModelBeforeRender interface {
// BeforeRender is called before the model is rendered in a response context.
// This could be used to modify or mask sensitive data before it is sent to the client.
BeforeRender(ctx *gin.Context) (err error)
}
IModelBeforeRender defines the method that should be called before rendering the model.
type IModelCreateClause ¶
type IModelCreateClause interface {
// CreateClause returns a GORM []clause.Interface containing the query clause for creating records.
// The clause can be used to customize the CREATE operation in the database.
CreateClause(ctx *gin.Context) []clause.Interface
}
IModelCreateClause defines an interface for models that support create operations with custom clauses.
type IModelDeleteClause ¶
type IModelDeleteClause interface {
// DeleteClause returns a GORM []clause.Interface containing the query clause for deleting records.
// The clause can be used to customize the DELETE operation in the database.
DeleteClause(ctx *gin.Context) []clause.Interface
}
IModelDeleteClause defines an interface for models that support delete operations with custom clauses.
type IModelFilterable ¶
type IModelFilterable interface {
// Filterable returns a map of fields that can be used to filter queries on the model.
// This ensures that only displayed values are filtered, preventing exposure of sensitive data.
Filterable(ctx *gin.Context) map[string]struct{}
}
IModelFilterable defines the method that should return filterable fields for the model.
type IModelListClause ¶
type IModelListClause interface {
// ListClause returns a GORM []clause.Interface containing the query clause for listing multiple records.
// The clause can be used to customize the LIST operation in the database.
ListClause(ctx *gin.Context) []clause.Interface
}
IModelListClause defines an interface for models that support list operations with custom clauses.
type IModelReadClause ¶
type IModelReadClause interface {
// ReadClause returns a GORM []clause.Interface containing the query clause for reading single records.
// The clause can be used to customize the READ operation in the database.
ReadClause(ctx *gin.Context) []clause.Interface
}
IModelReadClause defines an interface for models that support read operations with custom clauses.
type IModelReadable ¶
type IModelReadable interface {
// Readable returns a slice of field names that are allowed to be read in the model.
Readable(ctx *gin.Context) []string
}
IModelReadable defines the method that should return readable fields for the model. This ensures that only specified fields can be read, preventing loss of sensitive data.
type IModelSortable ¶
type IModelSortable interface {
// Sortable returns a map of fields that can be used to sort queries on the model.
// This ensures that only displayed values are sorted, preventing exposure of sensitive data.
Sortable(ctx *gin.Context) map[string]struct{}
}
IModelSortable defines the method that should return sortable fields for the model.
type IModelUpdatable ¶
type IModelUpdatable interface {
// Updatable returns a slice of field names that are allowed to be updated in the model.
Updatable(ctx *gin.Context) []string
}
IModelUpdatable defines the method that should return updatable fields for the model. This ensures that only specified fields can be updated, preventing modification of sensitive data.
type IModelUpdateClause ¶
type IModelUpdateClause interface {
// UpdateClause returns a GORM []clause.Interface containing the query clause for updating records.
// The clause can be used to customize the UPDATE operation in the database.
UpdateClause(ctx *gin.Context) []clause.Interface
}
IModelUpdateClause defines an interface for models that support update operations with custom clauses.
type IRender ¶
type IRender interface {
// Render is embedded from gin package, providing the basic render functionality.
render.Render
// MimeType returns the MIME type of the rendered content.
MimeType() string
}
IRender defines an interface for rendering HTTP responses with a specific MIME type.
type IResource ¶
type IResource interface {
IResourceInitializer
IResourceGetter
}
IResource defines the interface that all resources must implement, combining initialization and retrieval capabilities.
type IResourceDatabase ¶
type IResourceDatabase interface {
IResource
// Database returns the gorm.DB instance associated with this resource for database operations.
Database() *gorm.DB
}
IResourceDatabase extends IResource with database access capabilities.
type IResourceGetter ¶
type IResourceGetter interface {
// Name returns the name of the resource.
Name() string
// Router returns the gin.IRouter associated with this resource.
Router() gin.IRouter
// Resource retrieves an IResourceGetter by name, allowing for nested resources.
Resource() IResourceGetter
// ResourceOf retrieves an IResourceGetter by name, allowing for nested resources.
ResourceOf(name string) IResourceGetter
// Controller returns the controller component of the resource.
Controller() IController
// Service returns the service component of the resource.
Service() IService
// View returns the view component of the resource.
View() IView
}
IResourceGetter provides methods to retrieve information about a resource and its components.
type IResourceInitializer ¶
type IResourceInitializer interface {
// SetResources assigns the provided Resources object to the resource.
// Additionally, it is responsible for initializing resource references in controllers, services, and views.
SetResources(*Resources)
// SetRouter sets the gin.IRouter for handling HTTP requests related to this resource.
// The router is established by REST as a pre-configured group, incorporating the resource's designated path.
SetRouter(router gin.IRouter)
// Register registers the resource's routes and handlers with the router.
// Must be called after SetRouter
Register()
}
IResourceInitializer provides methods to set up a resource's dependencies and register it with the router.
type IResourceSetter ¶
type IResourceSetter interface {
// SetResource assigns the provided IResource to this setter.
SetResource(IResource)
}
IResourceSetter provides a method to set an IResource.
type IService ¶
type IService interface {
// IResourceGetter is embedded to include methods related to retrieving resources.
IResourceGetter
// IResourceSetter is embedded to include methods related to modifying resources.
IResourceSetter
// Create handles the creation of a new resource.
Create(ctx *gin.Context, entity any) (err error)
// Read retrieves an existing resource.
Read(ctx *gin.Context, entity any) (err error)
// List retrieves a collection of resources.
List(ctx *gin.Context, entities any) (err error)
// Update modifies an existing resource.
Update(ctx *gin.Context, entity any) (err error)
// Delete removes a resource.
Delete(ctx *gin.Context, entity any) (err error)
}
IService defines the contract for service operations in a RESTful API.
type IView ¶
type IView interface {
// IResourceGetter is embedded to include methods related to retrieving resources.
IResourceGetter
// IResourceSetter is embedded to include methods related to modifying resources.
IResourceSetter
// Create handles the creation of a new resource and returns a IRender object.
Create(ctx *gin.Context, entity any) IRender
// Read retrieves an existing resource and returns a IRender object.
Read(ctx *gin.Context, entity any) IRender
// List retrieves a collection of resources and returns a IRender object.
List(ctx *gin.Context, entities any) IRender
// Update retrieves an existing resource and returns a IRender object.
Update(ctx *gin.Context, entity any) IRender
// Delete retrieves an existing resource and returns a IRender object.
Delete(ctx *gin.Context, entity any) IRender
// SupportedMimeTypes returns a pointer to a map containing supported MIME types.
SupportedMimeTypes() map[string]any
}
IView defines the contract for view operations in a RESTful API, responsible for rendering responses.
type Resources ¶
Resources is a map that associates resource names with their corresponding IResource instances.
type SortOption ¶
type SortOption struct {
// Field is the name of the field to sort by.
Field string
// Descending indicates whether the sorting should be in descending order.
Descending bool
}
SortOption represents a single sort criterion for querying.