Documentation
¶
Index ¶
- Constants
- Variables
- func BindForm[T any](r *http.Request, out *T) error
- func DefaultFormHandler[T IModel](c *gin.Context, out *T) error
- func FlushAll(dst string, models ...interface{})
- func GenDetailTmpl(data interface{}, rootDir string)
- func GenFormTmpl(data interface{}, rootDir string)
- func GenLayout(fileName string, controllers []ICrudCtrl)
- func GenListTmpl(data interface{}, rootDir string)
- func NewRenderer() multitemplate.Renderer
- func Respond(c *gin.Context, data gin.H, templateName string)
- func RespondWithConfig(c *gin.Context, data gin.H, templateName string, conf IConfig)
- func ScaffoldIndex(r IRouter, fileName string, controllers ...ICrudCtrl) gin.IRoutes
- type BaseModel
- type Config
- func (self *Config) EnableLayoutOnNonHxRequest() bool
- func (self *Config) HasAPI() bool
- func (self *Config) HasUI() bool
- func (self *Config) LayoutDataFunc() func(c *gin.Context, data gin.H)
- func (self *Config) LayoutName() string
- func (self *Config) ScaffoldMap() IScaffoldMap
- func (self *Config) ScaffoldRootDir() string
- func (self *Config) ScaffoldStrategy() ScaffoldStrategy
- func (self *Config) SetAsDefault() *Config
- func (self *Config) String() string
- func (self *Config) TemplateDirs() []string
- func (self *Config) WithAPI(value bool) *Config
- func (self *Config) WithCommandLineArgs(args []string) *Config
- func (c *Config) WithEnableLayoutOnNonHxRequest(enableLayoutOnNonHxRequest bool) *Config
- func (c *Config) WithLayoutDataFunc(layoutDataFunc func(c *gin.Context, data gin.H)) *Config
- func (c *Config) WithLayoutName(layoutName string) *Config
- func (self *Config) WithScaffoldMap(scaffoldMap IScaffoldMap) *Config
- func (self *Config) WithScaffoldRootDir(value string) *Config
- func (self *Config) WithScaffoldStrategy(value ScaffoldStrategy) *Config
- func (c *Config) WithTemplateDirs(dirs ...string) *Config
- func (self *Config) WithUI(value bool) *Config
- type CrudCtrl
- func (self *CrudCtrl[T]) BasePath() string
- func (self *CrudCtrl[T]) Delete(c *gin.Context)
- func (self *CrudCtrl[T]) Details(c *gin.Context)
- func (self *CrudCtrl[T]) Form(c *gin.Context)
- func (self *CrudCtrl[T]) GetModelName() string
- func (self *CrudCtrl[T]) List(c *gin.Context)
- func (self *CrudCtrl[T]) OnRouter(r IRouter) *CrudCtrl[T]
- func (self *CrudCtrl[T]) Scaffold(scaffoldTmpl string, conf *ScaffoldDataModelConfigurator) *CrudCtrl[T]
- func (self *CrudCtrl[T]) ScaffoldDefaults() *CrudCtrl[T]
- func (self *CrudCtrl[T]) Upsert(c *gin.Context)
- func (self *CrudCtrl[T]) WithFormBinder(handler FormBinder[T]) *CrudCtrl[T]
- type FormBinder
- type IConfig
- type ICrudCtrl
- type IModel
- type IResponseCapabilities
- type IRouter
- type IScaffoldMap
- type ResponseCapabilities
- type ScaffoldDataModel
- type ScaffoldDataModelConfigurator
- type ScaffoldLayoutDataModel
- type ScaffoldMenuItem
- type ScaffoldStrategy
- type SearchArgs
Constants ¶
const ( CmdArgStrategyAlways = "always" CmdArgStrategyIfNotExists = "newonly" CmdArgStrategyNever = "never" )
const ( SEARCH_OP_EQUAL = "=" SEARCH_OP_NOT_EQUAL = "<>" SEARCH_OP_MATCHES_PATTERN = "~" SEARCH_OP_IN = "in" SEARCH_OP_NOT_IN = "not in" SEARCH_OP_LT = "<" SEARCH_OP_LTE = "<=" SEARCH_OP_GT = ">" SEARCH_OP_GTE = ">=" )
TODO: Complete this part of the code to allow search and pagination for the models
Variables ¶
Functions ¶
func DefaultFormHandler ¶
DefaultFormHandler is a default form binder that binds the form data to a model using the form field names as the model field names
func GenDetailTmpl ¶ added in v0.0.11
func GenDetailTmpl(data interface{}, rootDir string)
func GenFormTmpl ¶ added in v0.0.10
func GenFormTmpl(data interface{}, rootDir string)
func GenListTmpl ¶ added in v0.0.10
func GenListTmpl(data interface{}, rootDir string)
func NewRenderer ¶ added in v0.0.10
func NewRenderer() multitemplate.Renderer
func Respond ¶ added in v0.1.5
Respond is a function that renders a template with the given data If the request is request accepts application/json it will the data as json If the request is an Htmx request it will render the template with the data If the request is not an Htmx request it will use the layout to render the data
- The layout should be aware of the data that is passed to it and conditionally render that template
it uses the default hxConfig to render the template See `RenderWithConfig` for more control over the rendering
func RespondWithConfig ¶ added in v0.1.5
RespondWithConfig is a function that renders a template with the given data and the render configuration See Render for more information on the rendering See Config for more information on the configuration
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
func Setup ¶ added in v0.0.11
func Setup() *Config
NewConfig creates a new configuration crud configuration containing all the defaults
func (*Config) EnableLayoutOnNonHxRequest ¶
if true the layout will be used even if the request is not an Htmx request, otherwise the template will be rendered without the layout
func (*Config) HasAPI ¶ added in v0.1.5
HasAPI returns true if the configuration has the API enabled
func (*Config) LayoutDataFunc ¶
a function that is used to supply the layout with data
func (*Config) LayoutName ¶ added in v0.0.10
the layout to use on the templates for full page rendering
func (*Config) ScaffoldMap ¶ added in v0.0.11
func (self *Config) ScaffoldMap() IScaffoldMap
func (*Config) ScaffoldRootDir ¶ added in v0.0.10
where to create the templates
func (*Config) ScaffoldStrategy ¶ added in v0.0.10
func (self *Config) ScaffoldStrategy() ScaffoldStrategy
how to create the templates
func (*Config) SetAsDefault ¶ added in v0.0.11
SetAsDefault sets the current configuration as the default configuration
func (*Config) TemplateDirs ¶ added in v0.0.10
Which template directories to scan for templates
func (*Config) WithCommandLineArgs ¶ added in v0.0.10
WithCommandLineArgs sets the configuration from the command line arguments
func (*Config) WithEnableLayoutOnNonHxRequest ¶
if true the layout will be used even if the request is not an Htmx request, otherwise the template will be rendered without the layout
func (*Config) WithLayoutDataFunc ¶
WithLayoutDataFunc is function that is used to supply the layout with the data needed to render the layout
func (*Config) WithLayoutName ¶ added in v0.0.10
the layout to use on the templates for full page rendering
func (*Config) WithScaffoldMap ¶ added in v0.0.11
func (self *Config) WithScaffoldMap(scaffoldMap IScaffoldMap) *Config
WithScaffoldMap sets the scaffold map that will be used to generate the scaffolded templates
func (*Config) WithScaffoldRootDir ¶ added in v0.0.10
WithScaffoldRootDir sets the root directory where the scaffolded templates will be placed
func (*Config) WithScaffoldStrategy ¶ added in v0.0.10
func (self *Config) WithScaffoldStrategy(value ScaffoldStrategy) *Config
WithScaffoldStrategy sets the strategy to use when creating the scaffolded templates The default is ScaffoldCreateAlways, options are ScaffoldCreateAlways, ScaffoldCreateIfNotExist, ScaffoldCreateNever This option is not used at the moment
func (*Config) WithTemplateDirs ¶
WithTemplateDirs sets the template directories to scan for templates when setting up the renderer
type CrudCtrl ¶
type CrudCtrl[T IModel] struct { Db *gorm.DB Router IRouter Config IConfig ModelName string ModelKeyName string FormBinder FormBinder[T] }
CrudCtrl is a controller that implements the basic CRUD operations for a model with a gorm backend
func New ¶
New creates a new CRUD controller for the provided model
It uses the default configuration See `NewWithConfig` for more control over the configuration
func NewWithConfig ¶ added in v0.1.5
New creates a new CRUD controller for the provided model
It uses the provided configuration to define its behaviour
func (*CrudCtrl[T]) Delete ¶
Delete is a handler that deletes an item of the model it is a DELETE request
func (*CrudCtrl[T]) Details ¶
Details is a handler that shows the details of a single item of the model it is a GET request !Requires the template to be named as modelName-details.html where the modelName is lowercased model name
func (*CrudCtrl[T]) Form ¶
Form is a handler that shows the form for editing an item of the model it is a GET request !Requires the template to be named as modelName-edit.html where the modelName is lowercased model name
func (*CrudCtrl[T]) GetModelName ¶
Returns the Name of the model
func (*CrudCtrl[T]) List ¶
ROUTE HANDLERS List is a handler that lists all the items of the model it is a GET request !Requres the template to be named as modelName-list.html where the modelName is lowercased model name
func (*CrudCtrl[T]) Scaffold ¶ added in v0.0.10
func (self *CrudCtrl[T]) Scaffold(scaffoldTmpl string, conf *ScaffoldDataModelConfigurator) *CrudCtrl[T]
Creates and Flushes custom scaffold template
func (*CrudCtrl[T]) ScaffoldDefaults ¶ added in v0.0.11
func (*CrudCtrl[T]) Upsert ¶
Upsert is a handler that saves an item of the model it is a POST or PUT request depending on the presence of the id parameter !Requires the form fields to be named as the model field names(case sensitive) It redirects to the details page of the saved item
func (*CrudCtrl[T]) WithFormBinder ¶
func (self *CrudCtrl[T]) WithFormBinder(handler FormBinder[T]) *CrudCtrl[T]
WithFormBinder sets the form binder for the controller to be used when binding form data to the model on the POST and PUT requests. It is used in the Upsert method of the controller If not set, the default form binder is used which assumes that the form field names are the same as the model field names(case sensitive)
type FormBinder ¶
FormBinder is a function that binds the form data to a model
type IConfig ¶ added in v0.0.10
type IConfig interface {
// how to create the templates
ScaffoldStrategy() ScaffoldStrategy
// where to place the scaffolded templates
ScaffoldRootDir() string
// the scaffold map contains all the scaffold templates and is used to generate the model templates
ScaffoldMap() IScaffoldMap
// Which template directories to scan for templates
TemplateDirs() []string
// the layout to use on the templates for full page rendering
LayoutName() string
// a function that is used to supply the layout with data
LayoutDataFunc() func(c *gin.Context, data gin.H)
// if true the layout will be used if the request is not an Htmx request, otherwise the template will be rendered without the layout
EnableLayoutOnNonHxRequest() bool
// HasUI returns true if the response should be rendered as a UI
HasUI() bool
// HasAPI returns true if the response should be rendered as an API
HasAPI() bool
}
IConfig is an interface that defines the configuration for the crudex package
type ICrudCtrl ¶
type ICrudCtrl interface {
BasePath() string
GetModelName() string
List(c *gin.Context)
Details(c *gin.Context)
Form(c *gin.Context)
Upsert(c *gin.Context)
Delete(c *gin.Context)
}
ICrudCtrl is an interface that defines the basic CRUD operations for a model
type IResponseCapabilities ¶ added in v0.1.5
type IResponseCapabilities interface {
// HasUI returns true if the response should be rendered as a UI
HasUI() bool
// HasAPI returns true if the response should be rendered as an API
HasAPI() bool
// EnableLayoutOnNonHxRequest returns true if the layout should be used even if the request is not an Htmx request
EnableLayoutOnNonHxRequest() bool
}
IResponseCapabilities is an interface that defines the capabilities of the response
IConfig is already compliant with this interface
type IRouter ¶
type IRouter interface {
gin.IRoutes
Group(string, ...gin.HandlerFunc) *gin.RouterGroup
BasePath() string
}
IRouter is an interface that defines the router that is used to scaffold the model templates
It extends the gin.IRoutes interface but adds the BasePath method that returns the base path of the router.
Note: the `RouterGroup` struct has a `BasePath()` method already, so there is no need to implement it
type IScaffoldMap ¶ added in v0.0.11
type IScaffoldMap interface {
// All returns a map of scaffolded template functions
//
// These functions return the string representation of the scaffolded template (usually loaded from a file)
// They are used to scaffold the templates for any given model
All() map[string]func() string
// Get returns a scaffolded template function by name
Get(name string) func() string
// Returns the function map that is passed to the template engine when generating the templates from the scaffolded templates
FuncMap() template.FuncMap
// Export exports the scaffolded templates to the file system in the `scaffolds` directory.
//
// If the `forceIfExists` parameter is true, it will overwrite any existing scaffold templates
Export(forceIfExists bool) error
}
IScaffoldMap is an interface that defines the scaffold map that is used to generate the model templates
type ResponseCapabilities ¶ added in v0.1.5
func NewResponseCapabilities ¶ added in v0.1.5
func NewResponseCapabilities() *ResponseCapabilities
func (*ResponseCapabilities) EnableLayoutOnNonHxRequest ¶ added in v0.1.5
func (self *ResponseCapabilities) EnableLayoutOnNonHxRequest() bool
func (*ResponseCapabilities) HasAPI ¶ added in v0.1.5
func (self *ResponseCapabilities) HasAPI() bool
func (*ResponseCapabilities) HasUI ¶ added in v0.1.5
func (self *ResponseCapabilities) HasUI() bool
type ScaffoldDataModel ¶ added in v0.1.5
type ScaffoldDataModel struct {
// Type is the reflect.Type of the model
Type reflect.Type
// Name is the name of the model
Name string
// TemplateFileName is the name of the file where the template will be written
TemplateFileName string
Fields []reflect.StructField
// AllFields is a slice of reflect.StructField that represent all the fields of the model
AllFields []reflect.StructField
}
ScaffoldDataModel is a struct that holds the information needed to scaffold a template for a model. It is used to scaffold the templates for the given model
func NewScaffoldDataModel ¶ added in v0.1.5
func NewScaffoldDataModel(data interface{}, opts *ScaffoldDataModelConfigurator) *ScaffoldDataModel
func (*ScaffoldDataModel) Flush ¶ added in v0.1.5
func (md *ScaffoldDataModel) Flush(definition string, strategy ScaffoldStrategy) error
type ScaffoldDataModelConfigurator ¶ added in v0.1.5
type ScaffoldDataModelConfigurator struct {
// RootDir is the root directory where the templates will be written.
//
// It is used to create the TemplateFileName of the ModelDescriptor
RootDir string
// ModelNameSuffix is the suffix that will be added to the model name
ModelNameSuffix string
// TemplateNameSuffix is the suffix that will be added to the template name
TemplateNameSuffix string
// TemplateNamePrefix is the prefix that will be added to the template name
TemplateNamePrefix string
// TemplateExtension is the extension that will be added to the template name
TemplateExtension string
}
ScaffoldDataModelConfigurator is a struct that is used to create a ModelDescriptor
it defines the rules for the creation of the ModelDescriptor
type ScaffoldLayoutDataModel ¶ added in v0.1.5
type ScaffoldLayoutDataModel struct {
TemplateFileName string
Menu []ScaffoldMenuItem
}
ScaffoldLayoutDataModel is a struct that holds the data needed to scaffold the layout template
type ScaffoldMenuItem ¶ added in v0.1.5
ScaffoldMenuItem is a struct that holds the data needed to render a link to a model page in the layout template
type ScaffoldStrategy ¶ added in v0.0.10
type ScaffoldStrategy int
const ( // SCAFFOLD_ALWAYS will always scaffold the model templates SCAFFOLD_ALWAYS ScaffoldStrategy = iota // SCAFFOLD_IF_NOT_EXISTS will only scaffold the model templates if they do not exist SCAFFOLD_IF_NOT_EXISTS // SCAFFOLD_NEVER will never scaffold the model templates SCAFFOLD_NEVER )
func (ScaffoldStrategy) String ¶ added in v0.0.11
func (i ScaffoldStrategy) String() string
type SearchArgs ¶ added in v0.0.11
Struct for filter and pagination
func NewSearchArgs ¶ added in v0.0.11
func NewSearchArgs() SearchArgs
func NewSearchArgsFromQuery ¶ added in v0.0.11
func NewSearchArgsFromQuery(c *gin.Context) (SearchArgs, error)