rejson

package module
v0.0.0-...-76806f6 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2019 License: MIT Imports: 4 Imported by: 0

README

Go-ReJSON - a golang client for ReJSON (a JSON data type for Redis)

Go-ReJSON is a Go client for rejson redis module.

Build Status codecov

ReJSON is a Redis module that implements ECMA-404 The JSON Data Interchange Standard as a native data type. It allows storing, updating and fetching JSON values from Redis keys (documents). Primary features: > Full support of the JSON standard > JSONPath-like syntax for selecting element inside documents > Documents are stored as binary data in a tree structure, allowing fast access to sub-elements > Typed atomic operations for all JSON values types

  • Go-ReJSON is built atop the redigo client.
  • The package is intended to be used in conjuction with the redigo, which means all features provided by the original package will be available.

Installation

go get github.com/nitishm/go-rejson

Task List

Example usage

package main

import (
	"encoding/json"
	"flag"
	rejson "go-rejson"
	"log"

	"github.com/gomodule/redigo/redis"
)

var addr = flag.String("Server", "localhost:6379", "Redis server address")

type Name struct {
	First  string `json:"first,omitempty"`
	Middle string `json:"middle,omitempty"`
	Last   string `json:"last,omitempty"`
}

type Student struct {
	Name Name `json:"name,omitempty"`
	Rank int  `json:"rank,omitempty"`
}

func main() {
	flag.Parse()

	conn, err := redis.Dial("tcp", *addr)
	if err != nil {
		log.Fatalf("Failed to connect to redis-server @ %s", *addr)
	}

	student := Student{
		Name: Name{
			"Mark",
			"S",
			"Pronto",
		},
		Rank: 1,
	}
	res, err := rejson.JSONSet(conn, "student", ".", student, false, false)
	if err != nil {
		log.Fatalf("Failed to JSONSet")
		return
	}

	log.Printf("Success if - %s\n", res)

	studentJSON, err := redis.Bytes(rejson.JSONGet(conn, "student", ""))
	if err != nil {
		log.Fatalf("Failed to JSONGet")
		return
	}

	readStudent := Student{}
	err = json.Unmarshal(studentJSON, &readStudent)
	if err != nil {
		log.Fatalf("Failed to JSON Unmarshal")
		return
	}

	log.Printf("Student read from redis : %#v\n", readStudent)
}

Documentation

Index

Constants

View Source
const (
	// PopArrLast gives index of the last element for JSONArrPop
	PopArrLast = -1
	// DebugMemorySubcommand provide the corresponding MEMORY sub commands for JSONDebug
	DebugMemorySubcommand = "MEMORY"
	// DebugHelpSubcommand provide the corresponding HELP sub commands for JSONDebug
	DebugHelpSubcommand = "HELP"
	// DebugHelpOutput is the ouput of command JSON.Debug HELP <obj> [path]
	DebugHelpOutput = "MEMORY <key> [path] - reports memory usage\nHELP                - this message"
)

Variables

This section is empty.

Functions

func CommandBuilder

func CommandBuilder(commandNameIn string, argsIn ...interface{}) (commandNameOut string, argsOut []interface{}, err error)

CommandBuilder is used to build a command that can be used directly with redigo's conn.Do() This is especially useful if you do not need to conn.Do() and instead need to use the JSON.* commands in a MUTLI/EXEC scenario along with some other operations like GET/SET/HGET/HSET/...

func JSONArrAppend

func JSONArrAppend(conn redis.Conn, key string, path string, values ...interface{}) (res interface{}, err error)

JSONArrAppend to append json value into array at path JSON.ARRAPPEND <key> <path> <json> [json ...]

func JSONArrIndex

func JSONArrIndex(conn redis.Conn, key, path string, jsonValue interface{}, optionalRange ...int) (res interface{}, err error)

JSONArrIndex returns the index of the json element provided and return -1 if element is not present JSON.ARRINDEX <key> <path> <json-scalar> [start [stop]]

func JSONArrInsert

func JSONArrInsert(conn redis.Conn, key, path string, index int, values ...interface{}) (res interface{}, err error)

JSONArrInsert inserts the json value(s) into the array at path before the index (shifts to the right). JSON.ARRINSERT <key> <path> <index> <json> [json ...]

func JSONArrLen

func JSONArrLen(conn redis.Conn, key string, path string) (res interface{}, err error)

JSONArrLen returns the length of the json array at path JSON.ARRLEN <key> path

func JSONArrPop

func JSONArrPop(conn redis.Conn, key, path string, index int) (res interface{}, err error)

JSONArrPop removes and returns element from the index in the array to pop last element use rejson.PopArrLast JSON.ARRPOP <key> [path [index]]

func JSONArrTrim

func JSONArrTrim(conn redis.Conn, key, path string, start, end int) (res interface{}, err error)

JSONArrTrim trims an array so that it contains only the specified inclusive range of elements JSON.ARRTRIM <key> <path> <start> <stop>

func JSONDebug

func JSONDebug(conn redis.Conn, subcommand, key, path string) (res interface{}, err error)

JSONDebug reports information JSON.DEBUG <subcommand & arguments>

JSON.DEBUG MEMORY <key> [path]	- report the memory usage in bytes of a value. path defaults to root if not provided.
JSON.DEBUG HELP					- reply with a helpful message

func JSONDel

func JSONDel(conn redis.Conn, key string, path string) (res interface{}, err error)

JSONDel to delete a json object JSON.DEL <key> <path>

func JSONForget

func JSONForget(conn redis.Conn, key string, path string) (res interface{}, err error)

JSONForget is an alias for JSONDel

func JSONGet

func JSONGet(conn redis.Conn, key string, path string, opts ...JSONGetOption) (res interface{}, err error)

JSONGet used to get a json object JSON.GET <key>

     [INDENT indentation-string]
		[NEWLINE line-break-string]
		[SPACE space-string]
		[NOESCAPE]
		[path ...]

func JSONMGet

func JSONMGet(conn redis.Conn, path string, keys ...string) (res interface{}, err error)

JSONMGet used to get path values from multiple keys JSON.MGET <key> [key ...] <path>

func JSONNumIncrBy

func JSONNumIncrBy(conn redis.Conn, key string, path string, number int) (res interface{}, err error)

JSONNumIncrBy to increment a number by provided amount JSON.NUMINCRBY <key> <path> <number>

func JSONNumMultBy

func JSONNumMultBy(conn redis.Conn, key string, path string, number int) (res interface{}, err error)

JSONNumMultBy to increment a number by provided amount JSON.NUMMULTBY <key> <path> <number>

func JSONObjKeys

func JSONObjKeys(conn redis.Conn, key, path string) (res interface{}, err error)

JSONObjKeys returns the keys in the object that's referenced by path JSON.OBJKEYS <key> path

func JSONObjLen

func JSONObjLen(conn redis.Conn, key, path string) (res interface{}, err error)

JSONObjLen report the number of keys in the JSON Object at path in key JSON.OBJLEN <key> path

func JSONResp

func JSONResp(conn redis.Conn, key string, path string) (res interface{}, err error)

JSONResp returns the JSON in key in Redis Serialization Protocol (RESP). JSON.RESP <key> path

func JSONSet

func JSONSet(conn redis.Conn, key string, path string, obj interface{}, NX bool, XX bool) (res interface{}, err error)

JSONSet used to set a json object JSON.SET <key> <path> <json>

[NX | XX]

func JSONStrAppend

func JSONStrAppend(conn redis.Conn, key string, path string, jsonstring string) (res interface{}, err error)

JSONStrAppend to append a jsonstring to an existing member JSON.STRAPPEND <key> path <json-string>

func JSONStrLen

func JSONStrLen(conn redis.Conn, key string, path string) (res interface{}, err error)

JSONStrLen to return the length of a string member JSON.STRLEN <key> path

func JSONType

func JSONType(conn redis.Conn, key string, path string) (res interface{}, err error)

JSONType to get the type of key or member at path. JSON.TYPE <key> path

Types

type JSONGetOption

type JSONGetOption interface {
	// contains filtered or unexported methods
}

JSONGetOption provides methods various options for the JSON.GET Method

func NewJSONGetOptionIndent

func NewJSONGetOptionIndent(val string) JSONGetOption

NewJSONGetOptionIndent provides new INDENT options for JSON.GET Method

func NewJSONGetOptionNewLine

func NewJSONGetOptionNewLine(val string) JSONGetOption

NewJSONGetOptionNewLine provides new NEWLINE options for JSON.GET Method

func NewJSONGetOptionNoEscape

func NewJSONGetOptionNoEscape() JSONGetOption

NewJSONGetOptionNoEscape provides new NOESCAPE options for JSON.GET Method

func NewJSONGetOptionSpace

func NewJSONGetOptionSpace(val string) JSONGetOption

NewJSONGetOptionSpace provides new SPACE options for JSON.GET Method

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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