models

package
v0.0.0-...-16952c6 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package models provides shared model definitions.

Index

Constants

View Source
const (
	KindNone       = Kind(0)
	KindOrg        = Kind(1)
	KindUser       = Kind(2)
	KindRepository = Kind(3)
)
View Source
const (
	RoleNone          = Role(0) // never store
	RoleNormal        = Role(1)
	RoleAdmin         = Role(2)
	RoleTest          = Role(3)
	StatusNone        = Status(0) // never store
	StatusUnconfirmed = Status(1)
	StatusActive      = Status(2)
	StatusInactive    = Status(3)
)

Variables

View Source
var ErrConflict error = errors.New("row insertion conflict")

ErrConflict describes a duplicate row insertion.

View Source
var ErrDisallowedValue error = errors.New("value disallowed in this context")

ErrDisallowedValue signals a value of the right type, just not allowed.

View Source
var ErrModelMigrate error = errors.New("schema version error; cannot migrate model")

ErrModelMigrate signals a model could not be migrated to a different version.

View Source
var ErrNotFound error = errors.New("related model not found")

ErrNotFound describes an unfound model.

View Source
var ErrRelatedOrg error = errors.New("related org is missing or not Active")

ErrRelatedOrg signals that an org is missing or not Active.

View Source
var ErrRelatedUser error = errors.New("related user is missing, not Active, or is in a different org")

ErrRelatedUser signals that a user is missing, not Active, or is in a different org.

View Source
var ErrRole error = errors.New("malformed or disallowed role")

ErrRole signals a problem with a role field.

View Source
var ErrRowsAffected error = errors.New("db RowsAffected was not correct")

ErrRowsAffected describes an incorrect number of rows changed from a db mutation.

View Source
var ErrStatus error = errors.New("malformed or disallowed status")

ErrStatus signals a problem with a status field.

View Source
var ErrUnsafeString error = errors.New("string deemed unsafe")

ErrUnsafeString signals bad string input.

Functions

func NotNullConstraint

func NotNullConstraint(err error) bool

NotNullConstraint will try to match the db not-null constraint violation.

func UniqueConstraint

func UniqueConstraint(err error) bool

UniqueConstraint will try to match the db unique constraint violation.

func Update

func Update(ctx context.Context,
	conn *pgx.Conn,
	tableName string,
	id ID,
	colName string,
	val any,
) error

Update changes the value of a column given a tablename, column name and id.

Caller should Refresh() their model row again to get refreshed signature, ctime, mtime etc.

Types

type Base

type Base struct {
	ID   ID   `json:"id"`
	Meta Meta `json:"meta"`
}

Base models core attributes common to all models.

type ID

type ID uuid.UUID

ID is implemented as a UUID.

func NewID

func NewID() ID

func (ID) IsEmpty

func (id ID) IsEmpty() bool

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

func (*ID) Scan

func (id *ID) Scan(src interface{}) error

func (ID) String

func (id ID) String() string

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(bs []byte) error

func (ID) Value

func (id ID) Value() (driver.Value, error)

type Kind

type Kind int64

Kind is a symbol for a model kind.

type Meta

type Meta struct {
	Ctime         int64     `json:"ctime"`
	Mtime         int64     `json:"mtime"`
	Role          Role      `json:"role"`
	SchemaVersion int64     `json:"schema_version"`
	Signature     uuid.UUID `json:"signature"`
	Status        Status    `json:"status"`
}

Meta models metadata common to all models.

Ctime and Mtime are set in db by a trigger as unixtime. Signature is set in db by a trigger as a UUID -> Text conversion.

Default initialization will have Role as RoleNormal. Status as StatusUnconfirmed, which is intended.

type Role

type Role int64

Role describes the context of a model row. Role is an int64 when stored in the db.

func NewRole

func NewRole(role int64) (Role, error)

NewRole creates a Role from an int.

func (Role) IsEmpty

func (r Role) IsEmpty() bool

func (*Role) Scan

func (r *Role) Scan(src interface{}) error

func (*Role) UnmarshalJSON

func (r *Role) UnmarshalJSON(bs []byte) error

func (Role) Value

func (r Role) Value() (driver.Value, error)

type Status

type Status int64

Status describes the active state of a model row. Status is an int64 when stored in the db.

func NewStatus

func NewStatus(status int64) (Status, error)

NewStatus creates a Status from an int.

func (Status) IsEmpty

func (s Status) IsEmpty() bool

func (*Status) Scan

func (s *Status) Scan(src interface{}) error

func (*Status) UnmarshalJSON

func (s *Status) UnmarshalJSON(bs []byte) error

func (Status) Value

func (s Status) Value() (driver.Value, error)

type WithID

type WithID interface {
	GetID() ID
}

WithID indicates a model has a retrievable model ID.

type WithOrg

type WithOrg interface {
	GetOrg() ID
}

WithOrg indicates a model has a retrievable Org ID.

type WithReposity

type WithReposity interface {
	GetRepository() ID
}

WithRepository indicates a models has a retrievable RepositoryID.

type WithUser

type WithUser interface {
	GetUser() ID
}

WithUser indicates a model has a retrievable User ID.

Jump to

Keyboard shortcuts

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