jsondb

package
v0.0.0-...-cfcd19d Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package jsondb implements some structures useful for marshaling data structures into and out of a database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetJsonKeyMap

func GetJsonKeyMap[T any](km *KeyMap, reg *SchemaRegistry, key string) (T, error, bool)

GetJsonKeyMap will attempt to get the value of the key from the keymap in the form of the given type. Will fail if the given key is not registered, or if the key is registered to a different type.

func SetJsonKeyMap

func SetJsonKeyMap[T any](km *KeyMap, reg *SchemaRegistry, key string, value T) error

SetJsonKeyMap will attempt to set the value of the key in the key map to the given value. Will fail if the given key is not registered in the schema registry, or if the key is registered to a different type.

Types

type Json

type Json []byte

Json holds a byte slice that contains already-Marshalled JSON. It implements json.Marshaler and json.Unmarhaler, so that it can be included in larger structures which will be marshalled to JSON.

func Marshal

func Marshal(v any) (Json, error)

func (*Json) Marshal

func (rj *Json) Marshal(v any) error

func (Json) MarshalJSON

func (rj Json) MarshalJSON() ([]byte, error)

func (Json) String

func (rj Json) String() string

func (Json) Unmarshal

func (rj Json) Unmarshal(v any) error

func (*Json) UnmarshalJSON

func (rj *Json) UnmarshalJSON(in []byte) error

type KeyMap

type KeyMap map[string]Json

KeyMap is a structure designed to allow key-value pairs to be stored in a JSON field in a database; and speficially, to allow flexible recursive JSON structures if desired.

func (KeyMap) Get

func (km KeyMap) Get(key string, val any) (error, bool)

Get unmarshals the Json contained in km[key] into val. If km is nil, or the key does not exist, val is left empty and the bool is set to 'false'. Otherwise, the bool returns 'true', and error is the error value from marshalling.

func (*KeyMap) Scan

func (km *KeyMap) Scan(src interface{}) error

func (*KeyMap) Set

func (km *KeyMap) Set(key string, val any) error

Set marshal the key val into km[key]. If km is nil, it is initialized.

func (KeyMap) Value

func (km KeyMap) Value() (driver.Value, error)

type SchemaRegistry

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

SchemaRegistry is used to enforce a schema on the content of JSON keys.

func NewSchemaRegistry

func NewSchemaRegistry() *SchemaRegistry

func (*SchemaRegistry) Register

func (sr *SchemaRegistry) Register(key string, field any) error

Register the given key as being of the given type.

Jump to

Keyboard shortcuts

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