rejonson

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: Apache-2.0 Imports: 1 Imported by: 1

README

Rejonson

Redis rejson extension built upon go-redis

Build Status Coverage Status

Table of Contents

  1. Quick start
  2. API
  3. Dependencies
  4. Testing
  5. License
  6. Contact

Quick start

Install
go-redis v6
go get github.com/KromDaniel/rejonson
go-redis v7
go get github.com/KromDaniel/rejonson/v7
go-redis v8
go get github.com/KromDaniel/rejonson/v8
go-redis v9
go get github.com/KromDaniel/rejonson/v9

Quick Start

import (
	"github.com/KromDaniel/rejonson"
	"github.com/go-redis/redis"
)

func FunctionalApi(client *redis.Client) {
  // all rejonson.JsonX functions accepts redis.Client or redis.Pipeline
	// notice that some versions of go-redis also require context.Context (which is supported by rejonson)
	jsonStringCmd := rejonson.JsonGet(client, "key")
	if err := jsonStringCmd.Err(); err != nil {
		panic(err)
	}

	pipeline := client.Pipeline()
	rejonson.JsonGet(pipeline, "key")
	cmds, err := pipeline.Exec()
	// do something with cmds, err
}

func ExtendClient(client *redis.Client) *rejonson.Client {
  // You can extend go-redis client to rejonson.Client
  // that will have all JSON API as methods
	rejonsonClient := rejonson.ExtendClient(client)
	pingCmd := rejonsonClient.Ping()
	jsonCmd := rejonsonClient.JsonDel("key")

	return rejonsonClient
}
Functional API

Rejonson exports JsonX functions, all of them accept RedisProcessor as first parameter and context.Context (for go-redis versions >= 8) as second parameter, the other parameters are command specific

RedisProcessor

RedisProcessor is interface with the following definition:

type RedisProcessor interface {
	Process(redis.Cmder) error
}
go-redis >= 8
type RedisProcessor interface {
	Process(context.Context, redis.Cmder) error
}

By default all *redis.Client, redis.Pipeliner, *redis.ClusterClient, *redis.SentinelClient implenets that interface, so you can pass any of them to the rejonson functional API

example
client := redis.NewClient(&redis.Options{ /*...*/ })

res := rejonson.JsonMGet(client, "key1", "key2", "$..a")
if res.Err() != nil {
	// handle error
}

for _, value := range res.Val() {
	// do something with value
}
Extend Client

Extends go-redis client with all ReJSON abilities, so you can use directly the rejson client for all redis usage and commands.

// go redis client
goRedisClient := redis.NewClient(&redis.Options{
  Addr: "localhost:6379",
})

client := rejonson.ExtendClient(goRedisClient)
defer client.Close()

arr := []interface{}{"hello", "world", 1, map[string]interface{}{"key": 12}}
js, err := json.Marshal(arr)
if err != nil {
  // handle
}
// redis "native" command
client.Set("go-redis-cmd", "hello", time.Second)
// rejson command
client.JsonSet("rejson-cmd", ".", string(js))

// int command
arrLen, err := client.JsonArrLen("rejson-cmd", ".").Result()
if err != nil {
  // handle
}

fmt.Printf("Array length: %d", arrLen)
// Output: Array length: 4
Pipeline

Client will also return extended Pipeline and TXPipeline

goRedisClient := redis.NewClient(&redis.Options{
  Addr: "localhost:6379",
})

client := rejonson.ExtendClient(goRedisClient)

pipeline := client.Pipeline()
pipeline.JsonSet("rejson-cmd-pipeline", ".", "[10]")
pipeline.JsonNumMultBy("rejson-cmd-pipeline", "[0]", 10)
pipeline.Set("go-redis-pipeline-command", "hello from go-redis", time.Second)

_, err := pipeline.Exec()
if err != nil {
  // handle error
}
jsonString, err := client.JsonGet("rejson-cmd-pipeline").Result()
if err != nil {
  // handle error
}

fmt.Printf("Array %s", jsonString)

// Output: Array [100]

API

Rejonson implements all the methods as described at ReJson Commands except for JSON.DEBUG and JSON.RESP.

The args will be serialized to redis directly so make sure to read ReJSON command docs

All the rejson methods starts with the prefix of Json e.g JsonDel, JsonArrIndex, JsonMGet.
Each command returns specific go-redis.Cmder by the specific request.


Due to some ReJSON bug - #issue-76, some empty strings will be ignored.

Dependencies

Rejonson depends only on go-redis. The testing also depends on assert library

Test

Rejonson tests must use real redis with ReJson module to run

It is recommended to run the tests when using rejonson.
The unit tests will make sure your go-redis version is compatible and your rejson plugin supports all the methods and working as expected.

The testing library depends on assert library

License

Apache 2.0

Contact

For any question or contribution, feel free to open an issue.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JsonArrAppend added in v1.0.0

func JsonArrAppend(processor RedisProcessor, key string, path string, args ...interface{}) *redis.IntCmd

---- JsonArrAppend ----

func JsonArrIndex added in v1.0.0

func JsonArrIndex(processor RedisProcessor, key string, path string, value interface{}, startAndStop ...interface{}) *redis.IntCmd

---- JsonArrIndex ----

func JsonArrInsert added in v1.0.0

func JsonArrInsert(processor RedisProcessor, key string, path string, index int, values ...interface{}) *redis.IntCmd

---- JsonArrInsert ----

func JsonArrLen added in v1.0.0

func JsonArrLen(processor RedisProcessor, key string, path string) *redis.IntCmd

---- JsonArrLen ----

func JsonArrPop added in v1.0.0

func JsonArrPop(processor RedisProcessor, key string, path string, index int) *redis.StringCmd

---- JsonArrPop ----

func JsonArrTrim added in v1.0.0

func JsonArrTrim(processor RedisProcessor, key string, path string, start int, stop int) *redis.IntCmd

---- JsonArrTrim ----

func JsonDel added in v1.0.0

func JsonDel(processor RedisProcessor, key string, args ...interface{}) *redis.IntCmd

---- JsonDel ----

func JsonGet added in v1.0.0

func JsonGet(processor RedisProcessor, key string, args ...interface{}) *redis.StringCmd

---- JsonGet ----

func JsonMGet added in v1.0.0

func JsonMGet(processor RedisProcessor, key string, args ...interface{}) *redis.StringSliceCmd

---- JsonMGet ----

func JsonNumIncrBy added in v1.0.0

func JsonNumIncrBy(processor RedisProcessor, key string, path string, value int) *redis.StringCmd

---- JsonNumIncrBy ----

func JsonNumMultBy added in v1.0.0

func JsonNumMultBy(processor RedisProcessor, key string, path string, value int) *redis.StringCmd

---- JsonNumMultBy ----

func JsonObjKeys added in v1.0.0

func JsonObjKeys(processor RedisProcessor, key string, path string) *redis.StringSliceCmd

---- JsonObjKeys ----

func JsonObjLen added in v1.0.0

func JsonObjLen(processor RedisProcessor, key string, path string) *redis.IntCmd

---- JsonObjLen ----

func JsonSet added in v1.0.0

func JsonSet(processor RedisProcessor, key string, path string, json string, args ...interface{}) *redis.StatusCmd

---- JsonSet ----

func JsonStrAppend added in v1.0.0

func JsonStrAppend(processor RedisProcessor, key string, path string, value string) *redis.IntCmd

---- JsonStrAppend ----

func JsonStrLen added in v1.0.0

func JsonStrLen(processor RedisProcessor, key string, path string) *redis.IntCmd

---- JsonStrLen ----

func JsonType added in v1.0.0

func JsonType(processor RedisProcessor, key string, path string) *redis.StringCmd

---- JsonType ----

Types

type Client

type Client struct {
	*redis.Client
}

Client is an extended redis.Client, stores a pointer to the original redis.Client

func ExtendClient

func ExtendClient(client *redis.Client) *Client
Example
package main

import (
	"encoding/json"
	"fmt"
	"github.com/KromDaniel/rejonson"
	"github.com/go-redis/redis"
	"time"
)

func main() {
	goRedisClient := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})

	client := rejonson.ExtendClient(goRedisClient)
	defer client.Close()

	arr := []interface{}{"hello", "world", 1, map[string]interface{}{"key": 12}}
	js, err := json.Marshal(arr)
	if err != nil {
		// handle
	}
	// redis "native" command
	client.Set("go-redis-cmd", "hello", time.Second)
	client.JsonSet("rejson-cmd", ".", string(js))

	// int command
	arrLen, err := client.JsonArrLen("rejson-cmd", ".").Result()
	if err != nil {
		// handle
	}

	fmt.Printf("Array length: %d", arrLen)
}
Output:

Array length: 4

func (*Client) JsonArrAppend

func (processor *Client) JsonArrAppend(key string, path string, args ...interface{}) *redis.IntCmd

func (*Client) JsonArrIndex

func (processor *Client) JsonArrIndex(key string, path string, value interface{}, startAndStop ...interface{}) *redis.IntCmd

func (*Client) JsonArrInsert

func (processor *Client) JsonArrInsert(key string, path string, index int, values ...interface{}) *redis.IntCmd

func (*Client) JsonArrLen

func (processor *Client) JsonArrLen(key string, path string) *redis.IntCmd

func (*Client) JsonArrPop

func (processor *Client) JsonArrPop(key string, path string, index int) *redis.StringCmd

func (*Client) JsonArrTrim

func (processor *Client) JsonArrTrim(key string, path string, start int, stop int) *redis.IntCmd

func (*Client) JsonDel

func (processor *Client) JsonDel(key string, args ...interface{}) *redis.IntCmd

func (*Client) JsonGet

func (processor *Client) JsonGet(key string, args ...interface{}) *redis.StringCmd

func (*Client) JsonMGet

func (processor *Client) JsonMGet(key string, args ...interface{}) *redis.StringSliceCmd

func (*Client) JsonNumIncrBy

func (processor *Client) JsonNumIncrBy(key string, path string, value int) *redis.StringCmd

func (*Client) JsonNumMultBy

func (processor *Client) JsonNumMultBy(key string, path string, value int) *redis.StringCmd

func (*Client) JsonObjKeys

func (processor *Client) JsonObjKeys(key string, path string) *redis.StringSliceCmd

func (*Client) JsonObjLen

func (processor *Client) JsonObjLen(key string, path string) *redis.IntCmd

func (*Client) JsonSet

func (processor *Client) JsonSet(key string, path string, json string, args ...interface{}) *redis.StatusCmd

func (*Client) JsonStrAppend

func (processor *Client) JsonStrAppend(key string, path string, value string) *redis.IntCmd

func (*Client) JsonStrLen

func (processor *Client) JsonStrLen(key string, path string) *redis.IntCmd

func (*Client) JsonType

func (processor *Client) JsonType(key string, path string) *redis.StringCmd

func (*Client) Pipeline

func (cl *Client) Pipeline() *Pipeline

func (*Client) TXPipeline

func (cl *Client) TXPipeline() *Pipeline

type Pipeline

type Pipeline struct {
	redis.Pipeliner
}

Pipeline is an extended redis.Pipeline, stores a pointer to the original redis.Pipeliner

func ExtendPipeline

func ExtendPipeline(pipeline redis.Pipeliner) *Pipeline
Example
package main

import (
	"fmt"
	"github.com/KromDaniel/rejonson"
	"github.com/go-redis/redis"
	"time"
)

func main() {
	goRedisClient := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})

	client := rejonson.ExtendClient(goRedisClient)

	pipeline := client.Pipeline()
	pipeline.JsonSet("rejson-cmd-pipeline", ".", "[10]")
	pipeline.JsonNumMultBy("rejson-cmd-pipeline", "[0]", 10)
	pipeline.Set("go-redis-pipeline-command", "hello from go-redis", time.Second)

	_, err := pipeline.Exec()
	if err != nil {
		// handle error
	}
	jsonString, err := client.JsonGet("rejson-cmd-pipeline").Result()
	if err != nil {
		// handle error
	}

	fmt.Printf("Array %s", jsonString)

}
Output:

Array [100]

func (*Pipeline) JsonArrAppend

func (processor *Pipeline) JsonArrAppend(key string, path string, args ...interface{}) *redis.IntCmd

func (*Pipeline) JsonArrIndex

func (processor *Pipeline) JsonArrIndex(key string, path string, value interface{}, startAndStop ...interface{}) *redis.IntCmd

func (*Pipeline) JsonArrInsert

func (processor *Pipeline) JsonArrInsert(key string, path string, index int, values ...interface{}) *redis.IntCmd

func (*Pipeline) JsonArrLen

func (processor *Pipeline) JsonArrLen(key string, path string) *redis.IntCmd

func (*Pipeline) JsonArrPop

func (processor *Pipeline) JsonArrPop(key string, path string, index int) *redis.StringCmd

func (*Pipeline) JsonArrTrim

func (processor *Pipeline) JsonArrTrim(key string, path string, start int, stop int) *redis.IntCmd

func (*Pipeline) JsonDel

func (processor *Pipeline) JsonDel(key string, args ...interface{}) *redis.IntCmd

func (*Pipeline) JsonGet

func (processor *Pipeline) JsonGet(key string, args ...interface{}) *redis.StringCmd

func (*Pipeline) JsonMGet

func (processor *Pipeline) JsonMGet(key string, args ...interface{}) *redis.StringSliceCmd

func (*Pipeline) JsonNumIncrBy

func (processor *Pipeline) JsonNumIncrBy(key string, path string, value int) *redis.StringCmd

func (*Pipeline) JsonNumMultBy

func (processor *Pipeline) JsonNumMultBy(key string, path string, value int) *redis.StringCmd

func (*Pipeline) JsonObjKeys

func (processor *Pipeline) JsonObjKeys(key string, path string) *redis.StringSliceCmd

func (*Pipeline) JsonObjLen

func (processor *Pipeline) JsonObjLen(key string, path string) *redis.IntCmd

func (*Pipeline) JsonSet

func (processor *Pipeline) JsonSet(key string, path string, json string, args ...interface{}) *redis.StatusCmd

func (*Pipeline) JsonStrAppend

func (processor *Pipeline) JsonStrAppend(key string, path string, value string) *redis.IntCmd

func (*Pipeline) JsonStrLen

func (processor *Pipeline) JsonStrLen(key string, path string) *redis.IntCmd

func (*Pipeline) JsonType

func (processor *Pipeline) JsonType(key string, path string) *redis.StringCmd

func (*Pipeline) Pipeline

func (pl *Pipeline) Pipeline() *Pipeline

type RedisProcessor added in v1.0.0

type RedisProcessor interface {
	Process(redis.Cmder) error
}

RedisProcessor is redis client or pipeline instance that will process a command

Jump to

Keyboard shortcuts

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