jsondb

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

jsondb

Description

A simple json db in GO

What it does?

This library enables to store, retrieve, update and delete data from db based on json files.

Usage

func New
func  New(path  string, divider  ...string) (*Jsondb, error)

New initializes a new json database at the specified path with an optional divider string.

type jsondb
type  Jsondb  struct {
path     string
mux      *sync.RWMutex
divider  string
}

The jsondb type represents a JSON database.

Insert

func (db *jsondb) Insert
func (db *jsondb) Insert(keysStr  string, value  any) error

Insert adds a new value to the JSON database using the specified keys.

func (db *jsondb) InsertDir
func (db *jsondb) InsertDir(dirsStr  string) error

InsertDir adds directories with the specified directory names if they do not exist.

Get

func (db *jsondb) GetLen
func (db *Jsondb) GetLen(keysStr string) (int, error)

GetLen returns the number of entries in the directory.

func (db *jsondb) GetAny
func (db *jsondb) GetAny(keysStr  string) (any, error)

GetAny returns any stored under the specified key.

func (db *jsondb) GetBool
func (db *jsondb) GetBool(keysStr  string) (bool, error)

GetBool returns bool stored under the specified key, returns an error if the value cannot be converted to type bool.

func (db *jsondb) GetInt
func (db *jsondb) GetInt(keysStr  string) (int, error)

GetInt returns int stored under the specified key, returns an error if the value cannot be converted to type int.

func (db *jsondb) GetFloat
func (db *jsondb) GetFloat(keysStr  string) (float64, error)

GetFloat returns float64 stored under the specified key returns an error if the value cannot be converted to type float64.

func (db *jsondb) GetString
func (db *jsondb) GetString(keysStr  string) (string, error)

GetString returns string stored under the specified key, returns an error if the value cannot be converted to type string.

func (db *jsondb) GetMap
func (db *jsondb) GetMap(keysStr  string) (map[string]any, error)

GetMap returns map[string]any stored under the specified key, returns an error if the value cannot be converted to type map[string]any.

func (db *jsondb) GetAllMaps
func (db *jsondb) GetAllMaps(keysStr  string) ([]map[string]any, error)

GetAllMaps returns all files stored under the specified key converted to []map[string]any.

func (db *jsondb) GetStruct
func (db *jsondb) GetStruct(keysStr  string, dst  any) error

GetStruct returns a result stored under the specified key in the value pointed to by dst.

Delete

func (db *jsondb) Delete
func (db *jsondb) Delete(keysStr  string) error

Delete deletes all values stored under the specified key.

Documentation

Index

Constants

View Source
const (
	DatabaseFileType = ".json"
	DirDefault       = 0755
	FileDefault      = 0644
)

Variables

View Source
var (
	ErrorTypeConverting = errors.New("can't convert type")
)

Functions

func MapToStruct

func MapToStruct(dst any, src map[string]any) error

MapToStruct converts a map to a struct.

dst: Pointer to destination variable to store the converted struct value. src: The map containing the data to be converted to a struct. error: Returns an error if any.

func StructToMap

func StructToMap(dst map[string]any, src any) error

StructToMap converts a struct to a map.

dst: The destination map variable to store the converted struct value. src: The struct containing the data to be converted to a map. error: Returns an error if any.

Types

type Jsondb

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

func New

func New(path string, divider ...string) (*Jsondb, error)

New initializes a new json database at the specified path with an optional divider string.

path: the file path where the jsondb will be created. divider: an optional string used to divide nested keys in the jsondb. Returns a pointer to the newly initialized Jsondb instance and an error.

func (*Jsondb) Delete

func (db *Jsondb) Delete(keysStr string) error

Delete deletes all values stored under the specified key.

keysStr: A string representing the path to the desired values. error: An error if any occurs during the delete operation. Returns: An error if any occurs during the delete operation.

func (*Jsondb) Divider

func (db *Jsondb) Divider() string

func (*Jsondb) GetAllMaps

func (db *Jsondb) GetAllMaps(keysStr string) ([]map[string]any, error)

GetAllMaps retrieves all map values from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. Returns an array of map values found at the given keys and an error if any.

func (*Jsondb) GetAny

func (db *Jsondb) GetAny(keysStr string) (any, error)

GetAny retrieves an any value from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. Returns the any value found at the given keys and an error if not.

func (*Jsondb) GetBool

func (db *Jsondb) GetBool(keysStr string) (bool, error)

GetBool retrieves a boolean value from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. Returns the boolean value found at the given keys and an error if any.

func (*Jsondb) GetFloat

func (db *Jsondb) GetFloat(keysStr string) (float64, error)

GetFloat retrieves an float value from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. Returns the float64 value found at the given keys and an error if any.

func (*Jsondb) GetInt

func (db *Jsondb) GetInt(keysStr string) (int, error)

GetInt retrieves an integer value from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. Returns the integer value found at the given keys and an error if any.

func (*Jsondb) GetLen

func (db *Jsondb) GetLen(keysStr string) (int, error)

GetLen retrieves the number of entries in a directory specified by the given keys.

keysStr: A string representing the path to the directory. Returns the number of entries in the directory.

func (*Jsondb) GetMap

func (db *Jsondb) GetMap(keysStr string) (map[string]any, error)

GetMap retrieves a map value from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. Returns the map value found at the given keys and an error if any.

func (*Jsondb) GetString

func (db *Jsondb) GetString(keysStr string) (string, error)

GetString retrieves an integer value from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. Returns the string value found at the given keys and an error if any.

func (*Jsondb) GetStruct

func (db *Jsondb) GetStruct(keysStr string, dst any) error

GetStruct retrieves a struct value from the JSON database based on the specified keys.

keysStr: A string representing the path to the desired integer value. dst: Pointer to destination variable to store the retrieved struct value. Returns an error if any.

func (*Jsondb) Insert

func (db *Jsondb) Insert(keysStr string, value any) error

Insert adds a new value to the JSON database using the specified keys.

keysStr: A string representing the path where the value is stored. value: The value to be added. Returns an error if any.

func (*Jsondb) InsertDir

func (db *Jsondb) InsertDir(dirsStr string) error

InsertDir adds directories with the specified directory names if they do not exist.

dirsStr: A string representing the names of the directories to be added. Returns an error if any.

Jump to

Keyboard shortcuts

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