gormt

package module
v0.0.0-...-8808836 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2022 License: MIT Imports: 3 Imported by: 0

README

gormt

gormt is a library designed for the Go programming language. It allows to use custom structs as gorm JSON type columns through go generics. Requires Go version 1.18 or higher.

How to use

Given the following Postgres table:

CREATE TABLE letters (
	id  UUID NOT NULL PRIMARY KEY,
	info JSON NOT NULL
);

where info will contains a JSON like:

{ 
  "sender": "Dvor",
  "recipient": "Magic",
  "content": "Hello World"
}

We implement the model as follows:

type Letter struct {
  ID   string `gorm:"primaryKey"`
  Info gormt.JSON[[]*Info]
}

type Info struct {
  Sender    string `json:"sender"`
  Recipient string `json:"recipient"`
  Content   string `json:"content"`
}

GORM requires the customized data type to implement the Scanner and Valuer interfaces. This way GORM knowns to how to receive/save it into the database. But using this library you get rid of any complication. You just need to add the label gormt.JSON[T] where T could be your custom type and you are good to go!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

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

func (*JSON[T]) Get

func (j *JSON[T]) Get() T

func (JSON[T]) GormDataType

func (j JSON[T]) GormDataType() string

func (*JSON[T]) Scan

func (j *JSON[T]) Scan(value interface{}) error

func (*JSON[T]) Set

func (j *JSON[T]) Set(v T)

func (JSON[T]) Value

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

Jump to

Keyboard shortcuts

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