rejson

package module
v0.0.0-...-1c3d131 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2018 License: MIT Imports: 3 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

This section is empty.

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 string, path string, scalar interface{}, start int, stop int) (res interface{}, err error)

JSON.ARRINDEX return the position of the scalar value in the array, or -1 if unfound. JSON.ARRINDEX <key> <path> <json-scalar> [start [stop]] The optional inclusive start (default 0) and exclusive stop (default 0, meaning that the last element is included) specify a slice of the array to search.

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 string, path string, index int) (res interface{}, err error)

JSON.ARRPOP Remove and return element from the index in the array. JSON.ARRPOP <key> [path [index]] index is the position in the array to start popping from (defaults to -1, meaning the last element).

func JSONArrTrim

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

JSON.ARRTRIM Trim 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 string, arguments ...string) (res interface{}, err error)

JSON.DEBUG Supported subcommands are: MEMORY <key> path - report the memory usage in bytes of a value. path defaults to root if not provided. returns an integer, specifically the size in bytes of the value HELP - reply with a helpful message.returns an array, specifically with the help message JSON.DEBUG <subcommand & arguments>

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)

JSON.FORGET An alias for JSON.DEL.

func JSONGet

func JSONGet(conn redis.Conn, key string, path string, options ...string) (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 string, path string) (res interface{}, err error)

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

func JSONObjLen

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

JSON.OBJLEN 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)

JSON.RESP Return 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

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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