models

package
v0.4.17 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ModelRegistry = make(map[string]*Reg)

ModelRegistry is model registry

View Source
var OwnerTyp reflect.Type

OwnerTyp is the model of the Owner table

View Source
var UserTyp reflect.Type

UserTyp is the model of the User table

View Source
var (
	Validate *validator.Validate
)

use a single instance , it caches struct info

Functions

func CreateBetterRESTTable added in v0.4.5

func CreateBetterRESTTable()

CreateBetterRESTTable registers models

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 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 GetTableNameFromTypeString added in v0.1.39

func GetTableNameFromTypeString(typeString string) string

GetTableNameFromTypeString get table name from typeString

func GetTagValueFromModelByTagKeyBetterRestAndValueKey added in v0.4.5

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

func OrgModelNameFromOrgResourceTypeString added in v0.4.0

func OrgModelNameFromOrgResourceTypeString(typeString string) string

OrgModelNameFromOrgResourceTypeString given org resource typeString, returns organization table name

func OrgModelTypeFromOrgResourceTypeString added in v0.4.0

func OrgModelTypeFromOrgResourceTypeString(typeString string) reflect.Type

OrgModelTypeFromOrgResourceTypeString given org resource typeString returns the reflect type of the organization

func OrgOwnershipModelNameFromOrgResourceTypeString added in v0.4.0

func OrgOwnershipModelNameFromOrgResourceTypeString(typeString string) string

OrgOwnershipModelNameFromOrgResourceTypeString given org resource typeString, returns name of organization table's linking table (ownership table) to user

func OwnershipTableNameFromOwnershipResourceTypeString added in v0.4.0

func OwnershipTableNameFromOwnershipResourceTypeString(typeString string) string

OwnershipTableNameFromOwnershipResourceTypeString given ownership resource typeStirng returns name of ownership table to the user

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" 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) GetID

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

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

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 Who
	// Scope included in the token who is accessing right now
	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
}

BatchHookPointData is the data send to batch model hookpoints

type BetterRESTTable added in v0.4.5

type BetterRESTTable struct {
	ID        *datatypes.UUID `gorm:"type:uuid;primary_key;" json:"id"`
	CreatedAt time.Time       `json:"createdAt" json:"createdAt"`
	UpdatedAt time.Time       `json:"updatedAt"`

	// Name is table name
	Name string `gorm:"unique_index:name"`
	// Version is table version
	Version string
}

BetterRESTTable store the information on all other models

type CRUPDOp added in v0.4.2

type CRUPDOp int

CRUPDOp designates the type of operations for BeforeCRUPD and AfterCRUPD hookpoints

const (
	CRUPDOpRead CRUPDOp = iota
	CRUPDOpCreate
	CRUPDOpUpdate
	CRUPDOpPatch
	CRUPDOpDelete
)

type Client

type Client struct {
	gorm.Model  // Includes ID, CreatedAt, UpdatedAt, DeletedAt
	Name        string
	Secret      string `gorm:"not null" json:"-"`
	RedirectURI string // TODO: RedirectURI can be multiples
}

Client is the program that makes request to the API So iOS would be one client, android would be another Any website making API request would have its own client ID This needs to be inserted into db beforehand. So we can validate the app making the request. Any such app has the permission to create the user

type HTTP added in v0.4.3

type HTTP struct {
	Endpoint string
	Method   string
}

HTTP stores HTTP request information

type HookPointData added in v0.2.0

type HookPointData struct {
	// DB handle
	DB *gorm.DB
	// Who is the user information, who is operating this CRUPD right now
	Who Who
	// 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.)
	Cargo *ModelCargo
	// Role of this user in relation to this data, only available during read
	Role *UserRole
}

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 IAfterLogin added in v0.1.47

type IAfterLogin interface {
	AfterLogin(hpdata HookPointData, payload map[string]interface{}) (map[string]interface{}, error)
}

IAfterLogin has a function that is a hookpoint for actions after login but before marshalling

type IAfterLoginFailed added in v0.2.0

type IAfterLoginFailed interface {
	AfterLoginFailed(hpdata HookPointData) error
}

IAfterLoginFailed has a function that is a hookpoint for actions after login but before marshalling

type IAfterPasswordUpdate added in v0.2.2

type IAfterPasswordUpdate interface {
	AfterPasswordUpdateDB(hpdata HookPointData) error
}

IAfterPasswordUpdate supports method to be called after data is updated in 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 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 IBeforeLogin added in v0.1.47

type IBeforeLogin interface {
	BeforeLogin(hpdata HookPointData) error
}

IBeforeLogin has a function that is a hookpoint for actions after login but before marshalling

type IBeforePasswordUpdate added in v0.2.2

type IBeforePasswordUpdate interface {
	BeforePasswordUpdateDB(hpdata HookPointData) error
}

IBeforePasswordUpdate supports method to be called before data is updated in 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(models Who, 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 Who) (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(role UserRole, who Who) []byte
}

IHasRenderer is for formatting IModel with a custom function basically do your own custom output For batch renderer, register a Render(r UserRole, who Who, modelObjs []IModel) []byte

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)
}

IModel is the interface for all domain models

func NewFromTypeString

func NewFromTypeString(typeString string) IModel

NewFromTypeString instantiate a new IModel object from type registry

func NewOrgModelFromOrgResourceTypeString added in v0.4.0

func NewOrgModelFromOrgResourceTypeString(typeString string) IModel

NewOrgModelFromOrgResourceTypeString gets Organization object If you're a resource under hooked up by Organization

func NewOrgOwnershipModelFromOrgResourceTypeString added in v0.4.0

func NewOrgOwnershipModelFromOrgResourceTypeString(typeString string) IModel

NewOrgOwnershipModelFromOrgResourceTypeString gets the joining table from the resource's organization model to the user

func NewOwnershipModelFromOwnershipResourceTypeString added in v0.4.0

func NewOwnershipModelFromOwnershipResourceTypeString(typeString string) IModel

NewOwnershipModelFromOwnershipResourceTypeString returns the model object of the ownership table (the table that links from this resource represented by the type string to the user)

func NewSliceFromDBByType added in v0.1.28

func NewSliceFromDBByType(modelType reflect.Type, f func(interface{}, ...interface{}) *gorm.DB) ([]IModel, error)

NewSliceFromDBByType queries the database for an array of models based on modelType func(dest interface{}) *gorm.DB

func NewSliceFromDBByTypeString added in v0.1.28

func NewSliceFromDBByTypeString(typeString string, f func(interface{}, ...interface{}) *gorm.DB) ([]IModel, error)

NewSliceFromDBByTypeString queries the database for an array of models based on typeString func(dest interface{}) *gorm.DB

func NewSliceStructFromTypeString

func NewSliceStructFromTypeString(typeString string) []IModel

NewSliceStructFromTypeString : return something originally like this obj := make(map[string][]Room) obj["content"] = make([]Room, 0, 0) https://stackoverflow.com/questions/50233285/create-a-map-in-go-using-reflection func NewSliceStructFromTypeString(typeString string) map[string][]IModel {

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 Who, 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 MapperType added in v0.1.28

type MapperType int

MapperType is the mapper type

const (
	// MapperTypeViaOwnership is for type which user owns something
	MapperTypeViaOwnership MapperType = iota

	// MapperTypeUser is user itself
	MapperTypeUser

	// MapperTypeViaOrganization is for type where an organization owns something
	MapperTypeViaOrganization

	// MapperTypeGlobal is for type where data is public to all
	MapperTypeGlobal

	// MapperTypeLinkTable is for table linking user and regular models
	MapperTypeLinkTable
)

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 `json:"userID"` // I guess the user's table has to be named "User" then.
	ModelID *datatypes.UUID `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) 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) 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 Reg

type Reg struct {
	Typ        reflect.Type
	TypVersion string // TypVersion is the Version of this model
	// CreateObj is by default the one passed in when calling RegModel*
	// It could be overriden with RegCustomCreate()
	CreateObj IModel

	// If type is link to user type, store type of ownership table (the one
	// that links to user)
	OwnershipType      reflect.Type
	OwnershipTableName *string
	// If custom ownership table is registered, store here
	OwnershipModelObjPtr IModel

	OrgTypeString string // If type has link to organization type, store organization typestring

	// CreateMethod can be defined with RegCustomCreate()
	CreateMethod func(db *gorm.DB) (*gorm.DB, error)

	BatchMethods string     // Batch endpoints, "CRUD" for create, batch read, batch update, batch delete
	IdvMethods   string     //  ID end points, "RUD" for read one, update one, and delete one
	Mapper       MapperType // Custmized mapper, default to datamapper.SharedOwnershipMapper

	BeforeCUPD func(bhpData BatchHookPointData, op CRUPDOp) error // no R since model doens't exist yet
	AfterCRUPD func(bhpData BatchHookPointData, op CRUPDOp) error

	AfterRead func(bhpData BatchHookPointData) error

	BeforeCreate func(bhpData BatchHookPointData) error
	AfterCreate  func(bhpData BatchHookPointData) error

	BeforeUpdate func(bhpData BatchHookPointData) error
	AfterUpdate  func(bhpData BatchHookPointData) error

	BeforePatchApply func(bhpData BatchHookPointData) error // This comes before patch is applied. Before "BeforePatch"
	BeforePatch      func(bhpData BatchHookPointData) error
	AfterPatch       func(bhpData BatchHookPointData) error

	BeforeDelete func(bhpData BatchHookPointData) error
	AfterDelete  func(bhpData BatchHookPointData) error

	BatchRenderer func(roles []UserRole, who Who, modelObj []IModel) []byte
}

Reg is a registry item

type RegOptions added in v0.4.0

type RegOptions struct {
	BatchMethods string // Batch endpoints, "CRUD" for create, batch read, batch update, batch delete
	IdvMethods   string //  ID end points, "RUD" for read one, update one, and delete one
	Mapper       MapperType
}

RegOptions is options when you want to add a model to registry

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
)

type Who added in v0.4.2

type Who struct {
	Client *Client
	Oid    *datatypes.UUID // ownerid
	Scope  *string
}

Who is the information about the client or the user

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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