gokv

package module
v0.0.0-...-872cf84 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Joe Bot - gokv Adapter

Build Status codecov Go Report Card CodeFactor License

Integration Joe with gokv.

Getting Started

To install:

go get github.com/tdakkota/joe-gokv-memory
Note

gokv currently does not support GetAll/Keys operations(see this issue)

Package provides Keys interface to store list of keys(by default map used).

Example usage
package main

import (
	"github.com/go-joe/joe"
	"github.com/philippgille/gokv/redis"
	"github.com/tdakkota/joe-gokv-memory"
	"go.uber.org/zap"
)

type Bot struct {
	*joe.Bot
}

func main() {
	store, err := redis.NewClient(redis.DefaultOptions)
	if err != nil {
		zap.L().Fatal(err.Error())
	}

	b := Bot{joe.New(
		"example-bot",
		gokv.Memory(store),
	)}
	b.Respond("remember (.+) is (.+)", b.Remember)
	b.Respond("what is (.+)", b.WhatIs)

	err = b.Run()
	if err != nil {
		zap.L().Fatal(err.Error())
	}
}

func (b *Bot) Remember(msg joe.Message) error {
	key, value := msg.Matches[0], msg.Matches[1]
	msg.Respond("OK, I'll remember %s is %s", key, value)
	return b.Store.Set(key, value)
}

func (b *Bot) WhatIs(msg joe.Message) error {
	key := msg.Matches[0]
	var value string
	ok, err := b.Store.Get(key, &value)
	if err != nil {
		return err
	}
	if ok {
		msg.Respond("%s is %s", key, value)
	} else {
		msg.Respond("I do not remember %q", key)
	}
	return nil
}

License

BSD-3-Clause

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Memory

func Memory(store gokv.Store) joe.Module

Memory creates a joe.Module

Types

type Keys

type Keys interface {
	OnAdd(key string)
	OnDelete(key string)
	Keys() []string
}

Keys is abstraction for keys list

type MemoryStore

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

func NewMemory

func NewMemory(store gokv.Store, options ...Option) (*MemoryStore, error)

NewMemory is MemoryStore struct constructor

func (MemoryStore) Close

func (m MemoryStore) Close() error

func (MemoryStore) Delete

func (m MemoryStore) Delete(key string) (bool, error)

func (MemoryStore) Get

func (m MemoryStore) Get(key string) (value []byte, ok bool, err error)

func (MemoryStore) Keys

func (m MemoryStore) Keys() ([]string, error)

func (MemoryStore) Set

func (m MemoryStore) Set(key string, value []byte) error

type Option

type Option func(*MemoryStore) error

Option func

func WithKeys

func WithKeys(keys Keys) Option

WithKeys is Option func which sets the instance of Keys

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger is Option func which sets the instance of logger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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