Documentation ¶
Index ¶
- Variables
- func FieldNameToColumn(modelObj IModel, fieldName string) (string, error)
- func FieldNameToJSONName(modelObj IModel, fieldName string) (string, error)
- func GetFieldNameFromModelByTagKey(modelObj interface{}, valueKey string) *string
- func GetFieldTypeFromModelByTagKeyBetterRestAndValueKey(modelObj IModel, valueKey string, recurseIntoEmbedded bool) reflect.Type
- func GetFieldValueFromModelByTagKeyBetterRestAndValueKey(modelObj IModel, valueKey string) interface{}
- func GetModelFieldTypeInModelIfValid(modelObj IModel, field string) (reflect.Type, error)
- func GetTagValueFromModelByTagKeyBetterRestAndValueKey(modelObj interface{}, valueKey string) *string
- func IsFieldInModel(modelObj IModel, field string) bool
- func JSONKeysToFieldName(modelObj IModel, key string) (string, error)
- func TranslateValidationErrorMessage(errs validator.ValidationErrors, modelObj IModel) (string, error)
- func ValidateModel(modelObj IModel) error
- type BaseModel
- func (b *BaseModel) BeforeCreate(scope *gorm.Scope) error
- func (b *BaseModel) GetCreatedAt() *time.Time
- func (b *BaseModel) GetDeletedAt() *time.Time
- func (b *BaseModel) GetID() *datatypes.UUID
- func (b *BaseModel) GetUpdatedAt() *time.Time
- func (b *BaseModel) SetID(id *datatypes.UUID)
- func (b *BaseModel) Validate() error
- type BatchHookCargo
- type BatchHookPointData
- type CRUPDOp
- type FieldNotInModelError
- type HTTP
- type HookPointData
- type IAfterCRUPD
- type IAfterCreate
- type IAfterDelete
- type IAfterPatch
- type IAfterRead
- type IAfterTransact
- type IAfterUpdate
- type IBeforeCUPD
- type IBeforeCreate
- type IBeforeDelete
- type IBeforePatch
- type IBeforePatchApply
- type IBeforeUpdate
- type IDoRealDelete
- type IGuardAPIEntry
- type IHasPermissions
- type IHasRenderer
- type IHasTableName
- type IModel
- type IOwnership
- type IValidate
- type JSONIDPatch
- type ModelCargo
- type ModelHasOwnership
- type OwnershipModelBase
- type OwnershipModelWithIDBase
- func (b *OwnershipModelWithIDBase) GetCreatedAt() *time.Time
- func (o *OwnershipModelWithIDBase) GetID() *datatypes.UUID
- func (o *OwnershipModelWithIDBase) GetModelID() *datatypes.UUID
- func (b *OwnershipModelWithIDBase) GetUpdatedAt() *time.Time
- func (o *OwnershipModelWithIDBase) GetUserID() *datatypes.UUID
- func (o *OwnershipModelWithIDBase) SetID(id *datatypes.UUID)
- func (o *OwnershipModelWithIDBase) SetModelID(id *datatypes.UUID)
- func (o *OwnershipModelWithIDBase) SetUserID(id *datatypes.UUID)
- type UserIDFetchable
- type UserRole
Constants ¶
This section is empty.
Variables ¶
var ( Validate *validator.Validate Trans ut.Translator )
use a single instance , it caches struct info
Functions ¶
func FieldNameToColumn ¶ added in v0.5.11
func FieldNameToJSONName ¶ added in v0.5.11
func GetFieldNameFromModelByTagKey ¶ added in v0.4.0
GetFieldNameFromModelByTagKey get's the name of the tagged field If it's a slice, it returns the element type It's an interface{} because it's not necessarily IModel
func GetFieldTypeFromModelByTagKeyBetterRestAndValueKey ¶ added in v0.4.0
func GetFieldTypeFromModelByTagKeyBetterRestAndValueKey(modelObj IModel, valueKey string, recurseIntoEmbedded bool) reflect.Type
GetFieldTypeFromModelByTagKeyBetterRestAndValueKey fetches the datatype of the variable tagged in tag
func GetFieldValueFromModelByTagKeyBetterRestAndValueKey ¶ added in v0.4.0
func GetFieldValueFromModelByTagKeyBetterRestAndValueKey(modelObj IModel, valueKey string) interface{}
GetFieldValueFromModelByTagKeyBetterRestAndValueKey fetches value of the variable tagged in tag
func GetModelFieldTypeInModelIfValid ¶ added in v0.5.11
Never returns the pointer value Since what we want is reflec.New() and it would be a pointer
func GetTagValueFromModelByTagKeyBetterRestAndValueKey ¶ added in v0.4.5
func IsFieldInModel ¶ added in v0.5.11
func JSONKeysToFieldName ¶ added in v0.5.11
JSONKeyToColumnName transforms json name to column name if not found, return err By doing this we don't need model check?
func TranslateValidationErrorMessage ¶ added in v0.5.11
func ValidateModel ¶ added in v0.5.11
Types ¶
type BaseModel ¶
type BaseModel struct { // For Postgres ID *datatypes.UUID `gorm:"type:uuid;primary_key;" json:"id"` CreatedAt time.Time `sql:"index" json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` DeletedAt *time.Time `sql:"index" json:"deletedAt"` }
BaseModel is the base class domain model which has standard ID
func (*BaseModel) BeforeCreate ¶
BeforeCreate sets a UUID if no ID is set (this is Gorm's hookpoint)
func (*BaseModel) GetCreatedAt ¶ added in v0.8.0
GetCreatedAt gets the time stamp the record is created
func (*BaseModel) GetDeletedAt ¶ added in v0.8.0
GetUpdatedAt gets the time stamp the record is deleted (which we don't use)
func (*BaseModel) GetUpdatedAt ¶ added in v0.8.0
GetUpdatedAt gets the time stamp the record is updated
type BatchHookCargo ¶
type BatchHookCargo struct {
Payload interface{}
}
BatchHookCargo is payload between batch update and batch delete hookpoints
type BatchHookPointData ¶ added in v0.2.0
type BatchHookPointData struct { // Ms is the slice of IModels Ms []IModel // DB is the DB handle DB *gorm.DB // Who is operating this CRUPD right now Who UserIDFetchable // TypeString TypeString string // Cargo between Before and After hookpoints (not used in AfterRead since there is before read hookpoint.) Cargo *BatchHookCargo // Role of this user in relation to this data, only available during read Roles []UserRole // URL parameters URLParams map[urlparam.Param]interface{} }
BatchHookPointData is the data send to batch model hookpoints
type CRUPDOp ¶ added in v0.4.2
type CRUPDOp int
------------------------------------------------------------------------------------ Old Rest Op CRUPDOp designates the type of operations for BeforeCRUPD and AfterCRUPD hookpoints
func HTTPMethodToCRUDOp ¶ added in v0.5.13
type FieldNotInModelError ¶ added in v0.5.11
type FieldNotInModelError struct {
Msg string
}
FieldNotInModelError is for GetModelFieldTypeIfValid. if field doesn't exist in the model, return this error We want to go ahead and skip it since this field may be other options that user can read in hookpoints
func (*FieldNotInModelError) Error ¶ added in v0.5.11
func (r *FieldNotInModelError) Error() string
type HookPointData ¶ added in v0.2.0
type HookPointData struct { // DB handle (not available for AfterTransact) DB *gorm.DB // Who is the user information, who is operating this CRUPD right now Who UserIDFetchable // TypeString is the typeString (model string) of this model TypeString string // Cargo between Before and After hookpoints (not used in IAfterRead since there is no IBeforeRead.) // Currently not supported in the AfterTransact hookpoint Cargo *ModelCargo // Role of this user in relation to this data, only available during read Role *UserRole // URL parameters URLParams map[urlparam.Param]interface{} }
HookPointData is the data send to single model hookpoints
type IAfterCRUPD ¶ added in v0.4.2
type IAfterCRUPD interface {
AfterCRUPDDB(hpdata HookPointData, op CRUPDOp) error
}
IAfterCRUPD supprots method to be called after data is after all CRUPD operations This is called before the individual ops
type IAfterCreate ¶ added in v0.3.2
type IAfterCreate interface {
AfterCreateDB(hpdata HookPointData) error
}
IAfterCreate supports method to be called after data is inserted (created) into the database
type IAfterDelete ¶
type IAfterDelete interface {
AfterDeleteDB(hpdata HookPointData) error
}
IAfterDelete supports method to be called after data is deleted from the database
type IAfterPatch ¶
type IAfterPatch interface {
AfterPatchDB(hpdata HookPointData) error
}
IAfterPatch supports method to be called before data is patched in the database
type IAfterRead ¶ added in v0.1.18
type IAfterRead interface {
AfterReadDB(hpdata HookPointData) error
}
IAfterRead supports method to be called after data is read from the database
type IAfterTransact ¶ added in v0.4.36
type IAfterTransact interface {
AfterTransact(hpdata HookPointData, op CRUPDOp)
}
IAfterTransact is the method to be called after data is after the entire CRUPD transaction is done.
type IAfterUpdate ¶
type IAfterUpdate interface {
AfterUpdateDB(hpdata HookPointData) error
}
IAfterUpdate supports method to be called after data is updated in the database
type IBeforeCUPD ¶ added in v0.4.3
type IBeforeCUPD interface {
BeforeCUPDDB(hpdata HookPointData, op CRUPDOp) error
}
IBeforeCUPD supprots method to be called before data is after all CRUPD operations This is called before the individual ops
type IBeforeCreate ¶ added in v0.3.2
type IBeforeCreate interface {
BeforeCreateDB(hpdata HookPointData) error
}
IBeforeCreate supports method to be called before data is inserted (created) into the database
type IBeforeDelete ¶
type IBeforeDelete interface {
BeforeDeleteDB(hpdata HookPointData) error
}
IBeforeDelete supports method to be called before data is deleted from the database
type IBeforePatch ¶
type IBeforePatch interface {
BeforePatchDB(hpdata HookPointData) error
}
IBeforePatch supports method to be called before data is patched in the database
type IBeforePatchApply ¶ added in v0.4.6
type IBeforePatchApply interface {
BeforePatchApplyDB(hpdata HookPointData) error
}
IBeforePatchApply supports method to be called before data is patched in the database And also before the patch is applied. This comes before BeforePatchDB
type IBeforeUpdate ¶
type IBeforeUpdate interface {
BeforeUpdateDB(hpdata HookPointData) error
}
IBeforeUpdate supports method to be called before data is updated in the database
type IDoRealDelete ¶ added in v0.1.4
type IDoRealDelete interface {
DoRealDelete() bool
}
IDoRealDelete is an interface to customize specification for real db delete
type IGuardAPIEntry ¶ added in v0.1.25
IGuardAPIEntry supports method which guard access to API based on scope
type IHasPermissions ¶ added in v0.4.0
type IHasPermissions interface {
Permissions(role UserRole, who UserIDFetchable) (jsontrans.Permission, jsontrans.JSONFields)
}
IHasPermissions is for IModel with a custom permission field to cherry pick json fields default is to return all but the dates
type IHasRenderer ¶ added in v0.4.4
type IHasRenderer interface {
Render(c *gin.Context, hpdata *HookPointData, op CRUPDOp) bool
}
IHasRenderer is for formatting IModel with a custom function basically do your own custom output If return false, use the default JSON output For batch renderer, register a Render(r UserRole, who models.UserIDFetchable, modelObjs []IModel) bool
type IHasTableName ¶ added in v0.1.18
type IHasTableName interface {
TableName() string
}
IHasTableName we know if there is Gorm's defined custom TableName
type IModel ¶
type IModel interface { // The following two avoids having to use reflection to access ID GetID() *datatypes.UUID SetID(id *datatypes.UUID) GetCreatedAt() *time.Time GetUpdatedAt() *time.Time }
IModel is the interface for all domain models
type IOwnership ¶ added in v0.1.18
type IOwnership interface { GetRole() UserRole SetRole(UserRole) GetUserID() *datatypes.UUID SetUserID(*datatypes.UUID) GetModelID() *datatypes.UUID SetModelID(*datatypes.UUID) GetID() *datatypes.UUID SetID(*datatypes.UUID) }
IOwnership is what OwnershipModelBase tables should satisfy. Except OwnershipType, that's for struct which embed OwnershipModelBase
type IValidate ¶
type IValidate interface {
Validate(who UserIDFetchable, http HTTP) error
}
IValidate supports validation with govalidator
type JSONIDPatch ¶ added in v0.3.1
type JSONIDPatch struct { ID *datatypes.UUID `json:"id"` Patch json.RawMessage `json:"patch"` // json.RawMessage is actually just typedefed to []byte }
JSONIDPatch is the stuff inside "content" for PatchMany operation
type ModelCargo ¶
type ModelCargo struct {
Payload interface{}
}
ModelCargo is payload between hookpoints
type ModelHasOwnership ¶ added in v0.4.5
type ModelHasOwnership struct { BaseModel Ownerships []OwnershipModelWithIDBase `gorm:"PRELOAD:false" json:"-" betterrest:"ownership"` }
ModelHasOwnership is the standard domain model to embed when creating an ownership type. If you need a customized linking table, Embed a BaseModel instead, and define a gorm "PRELOAD:false", json "-", and betterrest:"ownership"
type OwnershipModelBase ¶ added in v0.1.3
type OwnershipModelBase struct { ID *datatypes.UUID `gorm:"type:uuid;primary_key;" json:"id"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` DeletedAt *time.Time `sql:"index" json:"deletedAt"` Role UserRole `json:"role"` // an int }
OwnershipModelBase has a role. Intended to be embedded by table serving as link from resource to user
func (*OwnershipModelBase) BeforeCreate ¶ added in v0.3.0
func (o *OwnershipModelBase) BeforeCreate(scope *gorm.Scope) error
BeforeCreate sets a UUID if no ID is set (this is Gorm's hookpoint)
func (*OwnershipModelBase) GetID ¶ added in v0.3.0
func (o *OwnershipModelBase) GetID() *datatypes.UUID
GetID Get the ID field of the model (useful when using interface)
func (*OwnershipModelBase) GetRole ¶ added in v0.1.3
func (o *OwnershipModelBase) GetRole() UserRole
GetRole gets the role field of the model, comforms to IOwnership
func (*OwnershipModelBase) SetID ¶ added in v0.3.0
func (o *OwnershipModelBase) SetID(id *datatypes.UUID)
SetID Set the ID field of the model (useful when using interface)
func (*OwnershipModelBase) SetRole ¶ added in v0.1.3
func (o *OwnershipModelBase) SetRole(r UserRole)
SetRole sets the role field of the model, comforms to IOwnership
type OwnershipModelWithIDBase ¶ added in v0.3.0
type OwnershipModelWithIDBase struct { OwnershipModelBase UserID *datatypes.UUID `gorm:"index" json:"userID"` // I guess the user's table has to be named "User" then. ModelID *datatypes.UUID `gorm:"index" json:"modelID"` }
OwnershipModelWithIDBase is one with ID, if you don't need unique index for userID and modelID (if you don't expose the link table via LinkTableMapper) You can use this.
func (*OwnershipModelWithIDBase) GetCreatedAt ¶ added in v0.8.0
func (b *OwnershipModelWithIDBase) GetCreatedAt() *time.Time
GetCreatedAt gets the time stamp the record is created
func (*OwnershipModelWithIDBase) GetID ¶ added in v0.4.0
func (o *OwnershipModelWithIDBase) GetID() *datatypes.UUID
GetID Get the ID field of the model (useful when using interface)
func (*OwnershipModelWithIDBase) GetModelID ¶ added in v0.3.0
func (o *OwnershipModelWithIDBase) GetModelID() *datatypes.UUID
GetModelID gets the id of the model, comforms to IOwnership
func (*OwnershipModelWithIDBase) GetUpdatedAt ¶ added in v0.8.0
func (b *OwnershipModelWithIDBase) GetUpdatedAt() *time.Time
GetUpdatedAt gets the time stamp the record is updated
func (*OwnershipModelWithIDBase) GetUserID ¶ added in v0.3.0
func (o *OwnershipModelWithIDBase) GetUserID() *datatypes.UUID
GetUserID gets the user id of the model, comforms to IOwnership
func (*OwnershipModelWithIDBase) SetID ¶ added in v0.4.0
func (o *OwnershipModelWithIDBase) SetID(id *datatypes.UUID)
SetID Set the ID field of the model (useful when using interface)
func (*OwnershipModelWithIDBase) SetModelID ¶ added in v0.3.0
func (o *OwnershipModelWithIDBase) SetModelID(id *datatypes.UUID)
SetModelID sets the id of the model, comforms to IOwnership
func (*OwnershipModelWithIDBase) SetUserID ¶ added in v0.3.0
func (o *OwnershipModelWithIDBase) SetUserID(id *datatypes.UUID)
SetUserID sets the user id of the model, comforms to IOwnership
type UserIDFetchable ¶ added in v0.7.0
type UserRole ¶
type UserRole int
UserRole type with enum
const ( // UserRoleAny not for value in db, but for permission where any is fine (link table) UserRoleAny UserRole = -2 // UserRoleInvalid is invalid for this resource UserRoleInvalid UserRole = -1 // UserRoleAdmin is admin UserRole UserRoleAdmin UserRole = 0 // UserRoleGuest is guest UserRole (screw go-lint man) UserRoleGuest UserRole = 1 // UserRolePublic to all (global object) UserRolePublic UserRole = 2 // UserRoleTableBased is like admin but permission is subject to table control // Cannot delete site or alter permissions UserRoleTableBased UserRole = 3 )