Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Data ¶
type Data map[string]json.RawMessage
Data is a type that can be used as an attribute type in a GORM model to support storing arbitrary data in a record. The Data struct handles initialization of the underlying map internally.
Data implements (database/sql/driver).Valuer and (database/sql).Scanner in order to be serializable into a SQL database. The encoding of the stored value is JSON so it can be stored in JSON column types.
To support arbitrary values be encoded, Data uses json.RawMessage which allows the decoding into concrete types to be deferred until they are accessed. Use the Get and Set methods to access values.
func (Data) Get ¶
Get extracts the data value identified by the given key and decodes it into the passed out value. The type of the out value should be the same that was passed to Set when writing the value.
The first return value indicates whether there is a data value with the given key. An error is returned as the second return value if decoding fails.
func (*Data) Scan ¶
Scan implements sql.Scanner and creates a Data instance with data from a database query result.
func (*Data) Set ¶
Set stores the given key/value pair in the Data instance. If there already is a value with this key, it is overwritten. The value can be of any type that is JSON serializable and deserializable.
Note that if the value includes dynamic types (e.g. a struct with an attribute with an interface type), the value should implement json.Unmarshaler.
An error is returned if encoding the value fails.