types

package
v2.0.0-...-13f87ff Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: MIT Imports: 18 Imported by: 2

Documentation

Index

Constants

View Source
const SHARD_COUNT = 32

Variables

This section is empty.

Functions

func Column

func Column(col string) columnExpression

Types

type Archive

type Archive struct {
	Archive bool `gorm:"column:archive" json:"archive"`
}

func (*Archive) SetArchive

func (v *Archive) SetArchive(archive bool)

type ConcurrentMapShared

type ConcurrentMapShared[K comparable, V any] struct {
	sync.RWMutex // Protects access to the internal map.
	// contains filtered or unexported fields
}

ConcurrentMapShared represents a single shard of the ConcurrentMap. It contains a map guarded by a RWMutex for safe concurrent access.

type CreatedAt

type CreatedAt struct {
	CreatedAt time.Time `gorm:"column:created_at;default:CURRENT_TIMESTAMP()" json:"created_at"`
}

CreatedAt represents the timestamp of when an entity or object was created. It uses GORM's autoCreateTime for automatic timestamp management.

type Date

type Date struct {
	time.Time
}

func NewDate

func NewDate(year, month, day int) Date

NewDate is a constructor for Date and returns new Date.

func (Date) GormDBDataType

func (Date) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType returns gorm DB data type based on the current using database.

func (Date) GormDataType

func (Date) GormDataType() string

GormDataType returns gorm common data type. This type is used for the field's column type.

func (Date) MarshalJSON

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

MarshalJSON implements json.Marshaler to convert Time to json serialization.

func (*Date) Scan

func (t *Date) Scan(src any) error

Scan implements sql.Scanner interface and scans value into Time,

func (Date) String

func (t Date) String() string

String implements fmt.Stringer interface.

func (*Date) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler to deserialize json data.

func (Date) Value

func (t Date) Value() (driver.Value, error)

Value implements driver.Valuer interface and returns string format of Time.

type Dictionary

type Dictionary[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Dictionary represents an array of KeyValue pairs with thread-safety

func (*Dictionary[K, V]) Add

func (dict *Dictionary[K, V]) Add(key K, value V)

Add adds a new key-value pair to the dictionary (Thread-Safe)

func (*Dictionary[K, V]) Delete

func (dict *Dictionary[K, V]) Delete(key K)

Delete removes a key-value pair by key (Thread-Safe)

func (*Dictionary[K, V]) FindByKey

func (dict *Dictionary[K, V]) FindByKey(key K) (int, *KeyValue[K, V])

FindByKey finds a key and returns its index and pointer to the KeyValue (Thread-Safe)

func (*Dictionary[K, V]) FindByValue

func (dict *Dictionary[K, V]) FindByValue(value V) (int, *KeyValue[K, V])

FindByValue finds a value and returns its index and pointer to the KeyValue (Thread-Safe)

func (*Dictionary[K, V]) FindByValueFunc

func (dict *Dictionary[K, V]) FindByValueFunc(value V, equal func(a, b V) bool) (int, *KeyValue[K, V])

FindByValueFunc finds a value and returns its index and pointer to the KeyValue (Thread-Safe)

func (*Dictionary[K, V]) GormDBDataType

func (*Dictionary[K, V]) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType specifies the database data type for Dictionary

func (*Dictionary[K, V]) GormDataType

func (*Dictionary[K, V]) GormDataType() string

GormDataType specifies the GORM data type for Dictionary

func (*Dictionary[K, V]) GormValue

func (dict *Dictionary[K, V]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

GormValue serializes the dictionary value for GORM

func (*Dictionary[K, V]) JSON

func (dict *Dictionary[K, V]) JSON() ([]byte, error)

JSON serializes the dictionary into JSON format (Thread-Safe)

func (*Dictionary[K, V]) MarshalJSON

func (dict *Dictionary[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON serializes the dictionary to JSON (Thread-Safe)

func (*Dictionary[K, V]) Replace

func (dict *Dictionary[K, V]) Replace(index int, key K, value V) bool

Replace replaces a key-value pair at a specific index (Thread-Safe)

func (*Dictionary[K, V]) Scan

func (dict *Dictionary[K, V]) Scan(val any) error

Scan scans value into Dictionary, implements sql.Scanner interface

func (*Dictionary[K, V]) String

func (dict *Dictionary[K, V]) String() string

String displays all key-value pairs in the dictionary (for debugging)

func (*Dictionary[K, V]) UnmarshalJSON

func (dict *Dictionary[K, V]) UnmarshalJSON(b []byte) error

UnmarshalJSON deserializes JSON into the dictionary (Thread-Safe)

func (*Dictionary[K, V]) Value

func (dict *Dictionary[K, V]) Value() (driver.Value, error)

Value returns JSON value, implements driver.Valuer interface

type Int64Array

type Int64Array []string

func (Int64Array) GormDBDataType

func (Int64Array) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (Int64Array) GormDataType

func (Int64Array) GormDataType() string

func (*Int64Array) Scan

func (o *Int64Array) Scan(src any) error

func (Int64Array) Value

func (o Int64Array) Value() (driver.Value, error)

type IntArray

type IntArray []string

func (IntArray) GormDBDataType

func (IntArray) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (IntArray) GormDataType

func (IntArray) GormDataType() string

func (*IntArray) Scan

func (o *IntArray) Scan(src any) error

func (IntArray) Value

func (o IntArray) Value() (driver.Value, error)

type JSON

type JSON json.RawMessage

JSON defined JSON data type, need to implements driver.Valuer, sql.Scanner interface

func (JSON) GormDBDataType

func (JSON) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (JSON) GormDataType

func (JSON) GormDataType() string

GormDataType gorm common data type

func (JSON) GormValue

func (js JSON) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

func (JSON) MarshalJSON

func (j JSON) MarshalJSON() ([]byte, error)

MarshalJSON to output non base64 encoded []byte

func (*JSON) Scan

func (j *JSON) Scan(value any) error

Scan scan value into Jsonb, implements sql.Scanner interface

func (JSON) String

func (j JSON) String() string

func (*JSON) UnmarshalJSON

func (j *JSON) UnmarshalJSON(b []byte) error

UnmarshalJSON to deserialize []byte

func (JSON) Value

func (j JSON) Value() (driver.Value, error)

Value return json value, implement driver.Valuer interface

type JSONArrayExpression

type JSONArrayExpression struct {
	// contains filtered or unexported fields
}

func JSONArrayQuery

func JSONArrayQuery(column string) *JSONArrayExpression

func (*JSONArrayExpression) Build

func (json *JSONArrayExpression) Build(builder clause.Builder)

Build implements clause.Expression

func (*JSONArrayExpression) Contains

func (json *JSONArrayExpression) Contains(value any) *JSONArrayExpression

type JSONMap

type JSONMap map[string]any

JSONMap defined JSON data type, need to implements driver.Valuer, sql.Scanner interface

func (JSONMap) GormDBDataType

func (JSONMap) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (JSONMap) GormDataType

func (m JSONMap) GormDataType() string

GormDataType gorm common data type

func (JSONMap) GormValue

func (jm JSONMap) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

func (JSONMap) MarshalJSON

func (m JSONMap) MarshalJSON() ([]byte, error)

MarshalJSON to output non base64 encoded []byte

func (*JSONMap) Scan

func (m *JSONMap) Scan(val any) error

Scan scan value into Jsonb, implements sql.Scanner interface

func (*JSONMap) UnmarshalJSON

func (m *JSONMap) UnmarshalJSON(b []byte) error

UnmarshalJSON to deserialize []byte

func (JSONMap) Value

func (m JSONMap) Value() (driver.Value, error)

Value return json value, implement driver.Valuer interface

type JSONOverlapsExpression

type JSONOverlapsExpression struct {
	// contains filtered or unexported fields
}

JSONOverlapsExpression JSON_OVERLAPS expression, implements clause.Expression interface to use as querier

func JSONOverlaps

func JSONOverlaps(column clause.Expression, value string) *JSONOverlapsExpression

JSONOverlaps query column as json

func (*JSONOverlapsExpression) Build

func (json *JSONOverlapsExpression) Build(builder clause.Builder)

Build implements clause.Expression only mysql support JSON_OVERLAPS

type JSONQueryExpression

type JSONQueryExpression struct {
	// contains filtered or unexported fields
}

JSONQueryExpression json query expression, implements clause.Expression interface to use as querier

func JSONQuery

func JSONQuery(column string) *JSONQueryExpression

JSONQuery query column as json

func (*JSONQueryExpression) Build

func (jsonQuery *JSONQueryExpression) Build(builder clause.Builder)

Build implements clause.Expression

func (*JSONQueryExpression) Equals

func (jsonQuery *JSONQueryExpression) Equals(value any, keys ...string) *JSONQueryExpression

Keys returns clause.Expression

func (*JSONQueryExpression) Extract

func (jsonQuery *JSONQueryExpression) Extract(path string) *JSONQueryExpression

Extract extract json with path

func (*JSONQueryExpression) HasKey

func (jsonQuery *JSONQueryExpression) HasKey(keys ...string) *JSONQueryExpression

HasKey returns clause.Expression

type JSONSetExpression

type JSONSetExpression struct {
	// contains filtered or unexported fields
}

JSONSetExpression json set expression, implements clause.Expression interface to use as updater

func JSONSet

func JSONSet(column string) *JSONSetExpression

JSONSet update fields of json column

func (*JSONSetExpression) Build

func (jsonSet *JSONSetExpression) Build(builder clause.Builder)

Build implements clause.Expression support mysql, sqlite and postgres

func (*JSONSetExpression) Set

func (jsonSet *JSONSetExpression) Set(path string, value any) *JSONSetExpression

Set return clause.Expression.

{
	"age": 20,
	"name": "json-1",
	"orgs": {"orga": "orgv"},
	"tags": ["tag1", "tag2"]
}

// In MySQL/SQLite, path is `age`, `name`, `orgs.orga`, `tags[0]`, `tags[1]`.
DB.UpdateColumn("attr", JSONSet("attr").Set("orgs.orga", 42))

// In PostgreSQL, path is `{age}`, `{name}`, `{orgs,orga}`, `{tags, 0}`, `{tags, 1}`.
DB.UpdateColumn("attr", JSONSet("attr").Set("{orgs, orga}", "bar"))

type JSONSlice

type JSONSlice[T any] []T

JSONSlice give a generic data type for json encoded slice data.

func NewJSONSlice

func NewJSONSlice[T any](s []T) JSONSlice[T]

func (JSONSlice[T]) GormDBDataType

func (JSONSlice[T]) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (JSONSlice[T]) GormDataType

func (JSONSlice[T]) GormDataType() string

GormDataType gorm common data type

func (JSONSlice[T]) GormValue

func (j JSONSlice[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

func (*JSONSlice[T]) Scan

func (j *JSONSlice[T]) Scan(value any) error

Scan scan value into JSONType[T], implements sql.Scanner interface

func (JSONSlice[T]) Value

func (j JSONSlice[T]) Value() (driver.Value, error)

Value return json value, implement driver.Valuer interface

type JSONType

type JSONType[T any] struct {
	// contains filtered or unexported fields
}

JSONType give a generic data type for json encoded data.

func NewJSONType

func NewJSONType[T any](data T) JSONType[T]

func (JSONType[T]) Data

func (j JSONType[T]) Data() T

Data return data with generic Type T

func (JSONType[T]) GormDBDataType

func (JSONType[T]) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (JSONType[T]) GormDataType

func (JSONType[T]) GormDataType() string

GormDataType gorm common data type

func (JSONType[T]) GormValue

func (js JSONType[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

func (JSONType[T]) MarshalJSON

func (j JSONType[T]) MarshalJSON() ([]byte, error)

MarshalJSON to output non base64 encoded []byte

func (*JSONType[T]) Scan

func (j *JSONType[T]) Scan(value any) error

Scan scan value into JSONType[T], implements sql.Scanner interface

func (*JSONType[T]) UnmarshalJSON

func (j *JSONType[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON to deserialize []byte

func (JSONType[T]) Value

func (j JSONType[T]) Value() (driver.Value, error)

Value return json value, implement driver.Valuer interface

type KeyValue

type KeyValue[K comparable, V any] struct {
	Key   K `json:"key"`
	Value V `json:"value"`
}

KeyValue represents a key-value pair with generic types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map represents a thread-safe map using sharded storage. It divides the map into multiple shards to reduce lock contention and improve performance.

func NewMap

func NewMap[V any]() Map[string, V]

NewMap creates a new ConcurrentMap with string keys and a default sharding function.

func NewMapWithCustomShardingFunction

func NewMapWithCustomShardingFunction[K comparable, V any](sharding func(key K) uint32) Map[K, V]

NewMapWithCustomShardingFunction creates a new Map with a user-defined sharding function.

func NewStringerMap

func NewStringerMap[K Stringer, V any]() Map[K, V]

NewStringerMap creates a new Map with custom Stringer keys and a sharding function.

func (Map[K, V]) Exists

func (m Map[K, V]) Exists(key K) bool

Exists checks if a key exists in the map

func (Map[K, V]) Get

func (m Map[K, V]) Get(key K) (V, bool)

Get retrieves a value by key from the map.

func (Map[K, V]) GetShard

func (m Map[K, V]) GetShard(key K) *ConcurrentMapShared[K, V]

GetShard returns the shard corresponding to the given key.

func (*Map[K, V]) GormDBDataType

func (m *Map[K, V]) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType defines how GORM should handle the type in different databases.

func (Map[K, V]) Iterate

func (m Map[K, V]) Iterate() <-chan struct {
	Key   K
	Value V
}

Iterate returns a channel to iterate over all key-value pairs in the map.

func (Map[K, V]) MSet

func (m Map[K, V]) MSet(data map[K]V)

MSet sets multiple key-value pairs in the map concurrently.

func (Map[K, V]) Range

func (m Map[K, V]) Range(f func(key K, value V))

Range iterates over all key-value pairs and applies the callback function to each pair.

func (Map[K, V]) Remove

func (m Map[K, V]) Remove(key K)

Remove deletes a key-value pair from the map.

func (Map[K, V]) RemoveCb

func (m Map[K, V]) RemoveCb(key K, cb RemoveCb[K, V]) bool

RemoveCb removes a key-value pair if the callback condition returns true.

func (Map[K, V]) Set

func (m Map[K, V]) Set(key K, value V)

Set inserts or updates a value in the map under the specified key.

func (Map[K, V]) SetIfAbsent

func (m Map[K, V]) SetIfAbsent(key K, value V) bool

SetIfAbsent sets a value in the map only if the key does not already exist.

func (Map[K, V]) Upsert

func (m Map[K, V]) Upsert(key K, value V, cb UpsertCb[V]) (res V)

Upsert inserts or updates a value in the map using a callback function.

type RemoveCb

type RemoveCb[K any, V any] func(key K, v V, exists bool) bool

RemoveCb defines a callback for conditional removal.

type SoftDelete

type SoftDelete struct {
	Deleted   bool       `gorm:"column:deleted;default:0;index" json:"deleted"`
	DeletedAt *time.Time `gorm:"column:deleted_at;nullable" json:"deleted_at"`
}

SoftDelete provides soft delete functionality in GORM. It includes a `Deleted` flag and a `DeletedAt` timestamp.

func (*SoftDelete) Delete

func (o *SoftDelete) Delete()

Delete set the Deleted status and the DeletedAt timestamp.

func (*SoftDelete) IsDeleted

func (o *SoftDelete) IsDeleted() bool

IsDeleted checks if the entity has been marked as deleted. Returns true if the `Deleted` field is set to true.

func (*SoftDelete) Restore

func (o *SoftDelete) Restore()

Restore resets the Deleted status and clears the DeletedAt timestamp.

func (*SoftDelete) SetDeleted

func (o *SoftDelete) SetDeleted(v bool)

SetDeleted updates the `Deleted` and `DeletedAt` fields of the SoftDelete object. If `v` is true, it sets `Deleted` to true and `DeletedAt` to the current time. If `v` is false, it resets `Deleted` to false and clears `DeletedAt`.

type StringArray

type StringArray []string

func (StringArray) GormDBDataType

func (StringArray) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (StringArray) GormDataType

func (StringArray) GormDataType() string

func (*StringArray) Scan

func (o *StringArray) Scan(src any) error

func (StringArray) Value

func (o StringArray) Value() (driver.Value, error)

type Stringer

type Stringer interface {
	fmt.Stringer
	comparable
}

Stringer is an interface that combines fmt.Stringer and comparable. It ensures that the type can be used both as a string and as a map key.

type Time

type Time time.Duration

Time is time data type.

func NewTime

func NewTime(hour, min, sec, nsec int) Time

NewTime is a constructor for Time and returns new Time.

func (Time) GormDBDataType

func (Time) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType returns gorm DB data type based on the current using database.

func (Time) GormDataType

func (Time) GormDataType() string

GormDataType returns gorm common data type. This type is used for the field's column type.

func (Time) MarshalJSON

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

MarshalJSON implements json.Marshaler to convert Time to json serialization.

func (*Time) Scan

func (t *Time) Scan(src any) error

Scan implements sql.Scanner interface and scans value into Time,

func (Time) String

func (t Time) String() string

String implements fmt.Stringer interface.

func (*Time) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler to deserialize json data.

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements driver.Valuer interface and returns string format of Time.

type URL

type URL url.URL

func (URL) GormDBDataType

func (URL) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (URL) GormDataType

func (URL) GormDataType() string

func (URL) MarshalJSON

func (u URL) MarshalJSON() ([]byte, error)

func (*URL) Scan

func (u *URL) Scan(value any) error

func (*URL) String

func (u *URL) String() string

func (*URL) UnmarshalJSON

func (u *URL) UnmarshalJSON(data []byte) error

func (URL) Value

func (u URL) Value() (driver.Value, error)

type UUID

type UUID uuid.UUID

func NewUUID

func NewUUID() UUID

NewUUID generates a UUID version 4, panics on generation failure.

func NewUUIDv1

func NewUUIDv1() UUID

NewUUIDv1 generates a UUID version 1, panics on generation failure.

func NewUUIDv4

func NewUUIDv4() UUID

NewUUIDv4 generates a UUID version 4, panics on generation failure.

func (UUID) Equals

func (u UUID) Equals(other UUID) bool

Equals returns true if string form of UUID matches other, false otherwise.

func (UUID) GormDBDataType

func (UUID) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type.

func (UUID) GormDataType

func (UUID) GormDataType() string

GormDataType gorm common data type.

func (UUID) IsEmpty

func (u UUID) IsEmpty() bool

IsEmpty returns true if UUID is nil UUID or of zero length, false otherwise.

func (*UUID) IsEmptyPtr

func (u *UUID) IsEmptyPtr() bool

IsEmptyPtr returns true if caller UUID ptr is nil or it's value is empty.

func (UUID) IsNil

func (u UUID) IsNil() bool

IsNil returns true if the UUID is a nil UUID (all zeroes), false otherwise.

func (*UUID) IsNilPtr

func (u *UUID) IsNilPtr() bool

IsNilPtr returns true if caller UUID ptr is nil, false otherwise.

func (UUID) Length

func (u UUID) Length() int

Length returns the number of characters in string form of UUID.

func (*UUID) Scan

func (u *UUID) Scan(value interface{}) error

Scan is the scanner function for this datatype.

func (UUID) String

func (u UUID) String() string

String returns the string form of the UUID.

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value is the valuer function for this datatype.

type UpdatedAt

type UpdatedAt struct {
	UpdatedAt time.Time `gorm:"column:updated_at;default:CURRENT_TIMESTAMP();ON_UPDATE:CURRENT_TIMESTAMP()" json:"updated_at"`
}

UpdatedAt represents the timestamp when an entity was last updated. It uses GORM's autoUpdateTime for automatic timestamp management.

type UpsertCb

type UpsertCb[V any] func(exist bool, valueInMap V, newValue V) V

UpsertCb defines a callback function for Upsert operations. The callback receives whether the key exists, the current value, and the new value.

Jump to

Keyboard shortcuts

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