models

package
v0.9.13 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Validate *validator.Validate
	Trans    ut.Translator
)

use a single instance , it caches struct info

Functions

func AppendToSliceAtFieldNum added in v0.9.7

func AppendToSliceAtFieldNum(modelObj IModel, fieldNum int, ele interface{})

func FieldNameToColumn added in v0.5.11

func FieldNameToColumn(modelObj IModel, fieldName string) (string, error)

func FieldNameToJSONName added in v0.5.11

func FieldNameToJSONName(modelObj IModel, fieldName string) (string, error)

func FindAllBetterRestPeggOrPegAssocIDs added in v0.9.7

func FindAllBetterRestPeggOrPegAssocIDs(modelObj interface{}, result *PeggedIDSearch) error

FindAllBetterRestPeggOrPegAssocIDs finds all Pegged or pegassoc ids, this is for OrgPartition which need pegged field only, pegassoc not yet tested. Many to many not handled at this point This is modified from markForDelete in gormfix and gormfixes modelObj is not models.IModel just because I don't need the dependency, may make it more generic later

func GetFieldNameFromModelByTagKey added in v0.4.0

func GetFieldNameFromModelByTagKey(modelObj interface{}, valueKey string) *string

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

func GetModelFieldTypeInModelIfValid(modelObj IModel, field string) (reflect.Type, error)

Never returns the pointer value Since what we want is reflec.New() and it would be a pointer

func GetModelTableNameInModelIfValid added in v0.5.11

func GetModelTableNameInModelIfValid(modelObj IModel, field string) (string, error)

func GetModelTypeNameFromIModel added in v0.4.36

func GetModelTypeNameFromIModel(model IModel) string

func GetTableNameFromIModel added in v0.1.20

func GetTableNameFromIModel(model IModel) string

GetTableNameFromIModel gets table name from an IModel

func GetTableNameFromType added in v0.1.39

func GetTableNameFromType(typ reflect.Type) string

GetTableNameFromType get table name from the model reflect.type

func GetTagValueFromModelByTagKeyBetterRestAndValueKey added in v0.4.5

func GetTagValueFromModelByTagKeyBetterRestAndValueKey(modelObj interface{}, valueKey string) *string

func IsFieldInModel added in v0.5.11

func IsFieldInModel(modelObj IModel, field string) bool

func JSONKeysToFieldName added in v0.5.11

func JSONKeysToFieldName(modelObj IModel, key string) (string, error)

JSONKeyToColumnName transforms json name to column name if not found, return err By doing this we don't need model check?

func SetSliceAtFieldNum added in v0.9.7

func SetSliceAtFieldNum(modelObj IModel, fieldNum int, ele interface{})

func SetStructAtFieldNum added in v0.9.7

func SetStructAtFieldNum(modelObj IModel, fieldNum int, ele interface{})

func SetStructPtrAtFieldNum added in v0.9.7

func SetStructPtrAtFieldNum(modelObj IModel, fieldNum int, ele interface{})

func TranslateValidationErrorMessage added in v0.5.11

func TranslateValidationErrorMessage(errs validator.ValidationErrors, modelObj IModel) (string, error)

func ValidateModel added in v0.5.11

func ValidateModel(modelObj IModel) error

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

func (b *BaseModel) BeforeCreate(scope *gorm.Scope) error

BeforeCreate sets a UUID if no ID is set (this is Gorm's hookpoint)

func (*BaseModel) GetCreatedAt added in v0.8.0

func (b *BaseModel) GetCreatedAt() *time.Time

GetCreatedAt gets the time stamp the record is created

func (*BaseModel) GetDeletedAt added in v0.8.0

func (b *BaseModel) GetDeletedAt() *time.Time

GetUpdatedAt gets the time stamp the record is deleted (which we don't use)

func (*BaseModel) GetID

func (b *BaseModel) GetID() *datatypes.UUID

GetID Get the ID field of the model (useful when using interface)

func (*BaseModel) GetUpdatedAt added in v0.8.0

func (b *BaseModel) GetUpdatedAt() *time.Time

GetUpdatedAt gets the time stamp the record is updated

func (*BaseModel) SetID

func (b *BaseModel) SetID(id *datatypes.UUID)

SetID Set the ID field of the model (useful when using interface)

func (*BaseModel) Validate

func (b *BaseModel) Validate() error

Validate validates the model

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

const (
	CRUPDOpOther CRUPDOp = iota // should not be used
	CRUPDOpRead
	CRUPDOpCreate
	CRUPDOpUpdate
	CRUPDOpPatch
	CRUPDOpDelete
)

func HTTPMethodToCRUDOp added in v0.5.13

func HTTPMethodToCRUDOp(method string) CRUPDOp

type FieldAsKey added in v0.9.7

type FieldAsKey struct {
	FieldNum  int // which field this belongs
	FieldType FieldType
	Rel       Relation // peg or pegassoc

}

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 FieldNumAndType added in v0.9.7

type FieldNumAndType struct {
	FieldNum  int
	TypeName  string
	FieldName string
	ObjType   reflect.Type
	IsSlice   bool // Could be slice of pointer or not
	IsPtr     bool
	IsStruct  bool
}

func GetPeggedFieldNumAndType added in v0.9.7

func GetPeggedFieldNumAndType(modelObj IModel) []FieldNumAndType

type FieldType added in v0.9.7

type FieldType int
const (
	FieldTypeSlice     FieldType = iota // within slice
	FieldTypeStruct                     // within struct
	FieldTypeStructPtr                  // within struct pointer
)

type HTTP added in v0.4.3

type HTTP struct {
	Endpoint string
	Op       CRUPDOp
}

HTTP stores HTTP request information

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

type IGuardAPIEntry interface {
	GuardAPIEntry(who UserIDFetchable, http HTTP) bool
}

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

func GetInnerModelIfValid added in v0.5.11

func GetInnerModelIfValid(modelObj IModel, field string) (IModel, error)

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 ModelAndIDs added in v0.9.7

type ModelAndIDs struct {
	ModelObj IModel // this is just one or a new model we can look up

	// IDs is the parent ID who we want to query
	// IDs       []interface{} // to send to Gorm need to be interface not *datatypes.UUID
	IDs       mapset.Set[string] // parent id
	ModelObjs []IModel           // this is a storage when we've queried it
}

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

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 PeggedIDSearch added in v0.9.7

type PeggedIDSearch struct {
	// There could be the same struct which exists in two places in the model
	// so we use FieldAsKey to store it, also tell us whether it was pointer or pointer to struct or slice
	// key is table name
	// tablename is the PARENT table name
	// tablename --> modelObj
	// ModelObjs map[string]reflect.Value
	// tablename -> FieldAsKey -> ids
	ToProcess map[string]map[FieldAsKey]*ModelAndIDs // those whose relationship with upper level is Peg
}

func NewPeggedIDSearch added in v0.9.7

func NewPeggedIDSearch() *PeggedIDSearch

type Relation added in v0.9.7

type Relation int
const (
	RelationPeg Relation = iota
	RelationPegAssoc
	RelationPegAssocManyToMany // not handled yet
)

type UserIDFetchable added in v0.7.0

type UserIDFetchable interface {
	GetUserID() *datatypes.UUID
}

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
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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