mdl

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 15 Imported by: 10

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

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

func FieldNameToColumn

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

func FieldNameToJSONName

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

func FindAllBetterRestPeggOrPegAssocIDs

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 mdl.IModel just because I don't need the dependency, may make it more generic later

func GetModelFieldTypeInModelIfValid

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

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

func GetModelTypeNameFromIModel

func GetModelTypeNameFromIModel(m IModel) string

func GetTableNameFromIModel

func GetTableNameFromIModel(imodel IModel) string

GetTableNameFromIModel gets table name from an IModel

func GetTableNameFromType

func GetTableNameFromType(typ reflect.Type) string

GetTableNameFromType get table name from the mdl reflect.type

func IsFieldInModel

func IsFieldInModel(modelObj IModel, field string) bool

func JSONKeysToFieldName

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 mdl check?

func SetSliceAtFieldNum

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

func SetStructAtFieldNum

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

func SetStructPtrAtFieldNum

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

Types

type BaseModel

type BaseModel struct {

	// For Postgres
	ID        *datatype.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 mdl 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

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

GetCreatedAt gets the time stamp the record is created

func (*BaseModel) GetDeletedAt

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() *datatype.UUID

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

func (*BaseModel) GetUpdatedAt

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

GetUpdatedAt gets the time stamp the record is updated

func (*BaseModel) SetID

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

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

func (*BaseModel) Validate

func (b *BaseModel) Validate() error

Validate validates the mdl

type FieldAsKey

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

}

type FieldNotInModelError

type FieldNotInModelError struct {
	Msg string
}

FieldNotInModelError is for GetModelFieldTypeIfValid. if field doesn't exist in the mdl, 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

func (r *FieldNotInModelError) Error() string

type FieldNumAndType

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

func GetPeggedFieldNumAndType(modelObj IModel) []FieldNumAndType

type FieldType

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

type IHasTableName

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() *datatype.UUID
	SetID(id *datatype.UUID)
	GetCreatedAt() *time.Time
	GetUpdatedAt() *time.Time
}

IModel is the interface for all domain mdl

func GetInnerModelIfValid

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

type ModelAndIDs

type ModelAndIDs struct {
	ModelObj IModel // this is just one or a new mdl 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 *datatype.UUID
	IDs       mapset.Set[string] // parent id
	ModelObjs []IModel           // this is a storage when we've queried it
}

type PeggedIDSearch

type PeggedIDSearch struct {
	// There could be the same struct which exists in two places in the mdl
	// 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

func NewPeggedIDSearch() *PeggedIDSearch

type Relation

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

Jump to

Keyboard shortcuts

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