dbtype

package
v0.0.0-...-7301b28 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: GPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TYPES = &typeRegistry{
	byType: make(map[reflect.Type]Type),
	byKind: make(map[reflect.Kind]Type),
	Locked: signals.New[TypeRegistry]("dbtype.TYPES.Locked"),
}

Functions

func Add

func Add(srcTyp any, dbType Type, forceKind ...bool) bool

Add a type to the registry.

The srcTyp can be a reflect.Type, reflect.Value, reflect.Kind, or we use reflect.TypeOf(srcTyp) to get the type. The dbType is the database type to bind to the srcTyp.

If forceKind is true, the kind of the srcTyp will be used to register the type as well.

After adding the type, calling `For(reflect.Type)` will return the dbType for the srcTyp.

This function will only return false if the type registry is locked.

func IsLocked

func IsLocked() bool

IsLocked checks if the type registry is locked.

func Lock

func Lock()

Lock locks the type registry.

This is used to prevent further modifications to the type registry after it has been locked.

It is used to ensure that the type registry is not modified after everything has been setup and registered.

func Types

func Types() iter.Seq2[reflect.Type, Type]

Types returns a sequence of all registered types in the registry. It yields pairs of reflect.Type and Type, allowing iteration over all registered types. This is useful for introspection, debugging purposes and type management.

Currently, it is only used in [drivers.registerTypeConversions] for converting a [string] of `<pkgpath.typename>` to reflect.Type for the initialization of default values during migrations for older migration files who's default value type might not match the current type. this is because the type registry provides a nice and unified way to handle go types which are also used in the database drivers.

Types

type CanDBType

type CanDBType interface {
	DBType() Type
}

CanDBType is an interface that defines a method to get the database type of a value.

It can be implemented by both [attrs.Field], or the reflect.Type returned by [attrs.Field.Type]

type CanDBTypeString

type CanDBTypeString interface {
	DBType() string
}

CanDBTypeString is a type that can be used to define a field that can return a database type as a string.

type Type

type Type int
const (
	Invalid Type = iota
	Text
	String
	Char
	Int
	Uint
	Float
	Decimal
	Bool
	UUID
	ULID
	Bytes
	JSON
	BLOB
	Timestamp
	LocalTime
	DateTime

	DEFAULT = Text // Default type used when no specific type is registered
)

func For

func For(typ reflect.Type) (dbType Type, exists bool)

For returns the database type for the given reflect.Type.

If the type is not registered, it will return the default type (Text).

func NewFromString

func NewFromString(s string) (Type, bool)

func (Type) MarshalJSON

func (t Type) MarshalJSON() ([]byte, error)

func (Type) String

func (t Type) String() string

func (*Type) UnmarshalJSON

func (t *Type) UnmarshalJSON(data []byte) error

type TypeRegistry

type TypeRegistry interface {
	Add(srcTyp any, dbType Type, forceKind ...bool) bool
	For(typ reflect.Type) (dbType Type, exists bool)
	IsLocked() bool
	Lock() (success bool)
	Types() iter.Seq2[reflect.Type, Type]
	Unlock()
}

The TypeRegistry interface defines methods for managing a registry of database types. It is only used for the signal before the type registry is locked.

Jump to

Keyboard shortcuts

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