Skip to content

Commit

Permalink
parsers, support for multiples clients and unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DevSrSouza committed Apr 23, 2019
1 parent edab7f7 commit 02932c9
Show file tree
Hide file tree
Showing 38 changed files with 369 additions and 325 deletions.
39 changes: 33 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Redissed
Redis Kotlin wrapper using Jedis inspired on Exposed
Redis Kotlin wrapper inspired on Exposed

## Getting started

Expand All @@ -23,23 +23,23 @@ dependencies {
```kotlin
import br.com.devsrsouza.redissed.RedisObject

class MyObject(database: String, jedis: Jedis) : RedisObject(database, jedis) {
class MyObject(database: String, commands: RedissedCommands) : RedisObject(database, commands) {
var hello: String? by string()
var hi: String by string("Hi")

val other: OtherObject by obj { OtherObject(it, jedis) }
val other: OtherObject by obj { OtherObject(it, commands) }
}

class OtherObject(database: String, jedis: Jedis) : RedisObject(database, jedis) {
class OtherObject(database: String, commands: RedissedCommands) : RedisObject(database, commands) {
var something = string()

var points: Int = int(0)
}

val database = "my:db"
val jedis = myJedisInstance
val commands: RedissedCommands = myClient.redissed

val my = MyObject("$database:obj", jedis)
val my = MyObject("$database:obj", commands)

my.hello = "Hello" // setting "Hello" to my:db:obj:hello
my.hi // getting my:db:obj:hi from Redis if not exist return default "Hi"
Expand All @@ -50,6 +50,33 @@ my.other.something = "Anything" // setting "Anything" to my:db:obj:other:somethi
my.other.points = 10 // setting 10 to my:db:obj:other:points
```

## Supported Redis Clients
- Jedis
- Lettuce

### Jedis
```kotlin
import br.com.devsrsouza.redissed.clients.redissed
import br.com.devsrsouza.redissed.RedissedCommands

val jedis = Jedis("localhost", 6379)
jedis.connect()

val commands: RedissedCommands = jedis.redissed
```

### Lettuce
```kotlin
import br.com.devsrsouza.redissed.clients.redissed
import br.com.devsrsouza.redissed.RedissedCommands

val redis = RedisClient.create(RedisURI.create("localhost", 6379))
val conn = redis.connect()
val sync = conn.sync()

val commands: RedissedCommands = sync.redissed
```

## Supported types
```kotlin
string(): String?
Expand Down
16 changes: 13 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,31 @@ plugins {
}

group = "br.com.devsrsouza"
version = "0.1.1"
version = "1.0.0"

repositories {
mavenCentral()
}

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("redis.clients:jedis:3.0.1")

//clients
compileClasspath("redis.clients:jedis:3.0.1")
compileClasspath("io.lettuce:lettuce-core:5.1.6.RELEASE")

// test
testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

tasks.withType<Test> {
useJUnitPlatform()
}

bintray {
val settings = file("publish.json").takeIf { it.exists() }?.let {
groovy.json.JsonSlurper().parseText(it.readText())
Expand Down Expand Up @@ -77,7 +87,7 @@ publishing {

pom {
name.set("Redissed")
description.set("Redis Kotlin wrapper using Jedis inspired on Exposed")
description.set("Redis Kotlin wrapper inspired on Exposed")
url.set("https://github.com/DevSrSouza/Redissed")
licenses {
license {
Expand Down
43 changes: 23 additions & 20 deletions src/main/kotlin/br/com/devsrsouza/redissed/RedisObject.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
package br.com.devsrsouza.redissed

import br.com.devsrsouza.redissed.delegates.ParserDelegate
import br.com.devsrsouza.redissed.delegates.ParserDelegateNullable
import br.com.devsrsouza.redissed.delegates.RedisObjectDelegate
import br.com.devsrsouza.redissed.delegates.notnull.*
import br.com.devsrsouza.redissed.delegates.nullable.*
import redis.clients.jedis.commands.JedisCommands
import br.com.devsrsouza.redissed.parsers.*

abstract class RedisObject(val key: String, val connection: JedisCommands) {
fun string() = StringRedisDelegateNullable()
fun string(default: String) = StringRedisDelegate(default)
abstract class RedisObject(val key: String, val commands: RedissedCommands) {
fun <T> parser(parser: Parser<T>) = ParserDelegateNullable(parser)
fun <T> parser(parser: Parser<T>, default: T) = ParserDelegate(parser, default)

fun boolean() = BooleanRedisDelegateNullable()
fun boolean(default: Boolean) = BooleanRedisDelegate(default)
fun string() = parser(StringParser)
fun string(default: String) = parser(StringParser, default)

fun byte() = ByteRedisDelegateNullable()
fun byte(default: Byte) = ByteRedisDelegate(default)
fun boolean() = parser(BooleanParser)
fun boolean(default: Boolean) = parser(BooleanParser, default)

fun short() = ShortRedisDelegateNullable()
fun short(default: Short) = ShortRedisDelegate(default)
fun byte() = parser(ByteParser)
fun byte(default: Byte) = parser(ByteParser, default)

fun int() = IntRedisDelegateNullable()
fun int(default: Int) = IntRedisDelegate(default)
fun short() = parser(ShortParser)
fun short(default: Short) = parser(ShortParser, default)

fun long() = LongRedisDelegateNullable()
fun long(default: Long) = LongRedisDelegate(default)
fun int() = parser(IntParser)
fun int(default: Int) = parser(IntParser, default)

fun float() = FloatRedisDelegateNullable()
fun flaot(default: Float) = FloatRedisDelegate(default)
fun long() = parser(LongParser)
fun long(default: Long) = parser(LongParser, default)

fun double() = DoubleRedisDelegateNullable()
fun double(default: Double) = DoubleRedisDelegate(default)
fun float() = parser(FloatParser)
fun flaot(default: Float) = parser(FloatParser, default)

fun double() = parser(DoubleParser)
fun double(default: Double) = parser(DoubleParser, default)

fun <T : RedisObject> obj(factory: (String) -> T) = RedisObjectDelegate(factory)
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/br/com/devsrsouza/redissed/RedissedCommands.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package br.com.devsrsouza.redissed

interface RedissedCommands {

/**
* Set the [key] in redis with [value]
*/
operator fun set(key: String, value: String)

/**
* Returns the value of [key] from redis or `null` if does not exist
*/
operator fun get(key: String): String?

/**
* Returns `true` if the [key] exist in redis or `false` if does not
*/
operator fun contains(key: String): Boolean

/**
* Remove [key] from redis and returns `true` if [key] exist or `false` if does not
*/
fun del(key: String): Boolean

/**
* Set the expiration time of [key] in redis and returns `true` if success or `false` if does not
*/
fun expire(key: String, seconds: Int): Boolean

/**
* Returns the seconds to expire of [key] from redis
*/
fun ttl(key: String): Long

}
6 changes: 3 additions & 3 deletions src/main/kotlin/br/com/devsrsouza/redissed/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import kotlin.reflect.KProperty
fun path(ref: RedisObject, property: KProperty<*>) = ref.key + ":" + property.name

fun set(value: String, ref: RedisObject, property: KProperty<*>) {
ref.connection.set(path(ref, property), value)
ref.commands.set(path(ref, property), value)
}

fun setNullable(value: String?, ref: RedisObject, property: KProperty<*>) {
val con = ref.connection
val con = ref.commands
val path = path(ref, property)

if(value == null) con.del(path)
else con.set(path, value)
}

fun get(ref: RedisObject, property: KProperty<*>): String? {
return ref.connection.get(path(ref, property))
return ref.commands.get(path(ref, property))
}
34 changes: 34 additions & 0 deletions src/main/kotlin/br/com/devsrsouza/redissed/clients/Jedis.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package br.com.devsrsouza.redissed.clients

import br.com.devsrsouza.redissed.RedissedCommands
import redis.clients.jedis.commands.JedisCommands

val JedisCommands.redissed get() = JedisRedissedCommands(this)

class JedisRedissedCommands(val commands: JedisCommands) : RedissedCommands {

override fun set(key: String, value: String) {
commands.set(key, value)
}

override fun get(key: String): String? {
return commands.get(key)
}

override fun contains(key: String): Boolean {
return commands.exists(key)
}

override fun del(key: String): Boolean {
return commands.del(key) >= 1
}

override fun expire(key: String, seconds: Int): Boolean {
return commands.expire(key, seconds) == 1.toLong()
}

override fun ttl(key: String): Long {
return commands.ttl(key)
}

}
33 changes: 33 additions & 0 deletions src/main/kotlin/br/com/devsrsouza/redissed/clients/Lettuce.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package br.com.devsrsouza.redissed.clients

import br.com.devsrsouza.redissed.RedissedCommands
import io.lettuce.core.api.sync.RedisCommands

val RedisCommands<String, String>.redissed: LettuceRedissedCommands
get() = LettuceRedissedCommands(this)

class LettuceRedissedCommands(val commands: RedisCommands<String, String>) : RedissedCommands {
override fun set(key: String, value: String) {
commands.set(key, value)
}

override fun get(key: String): String? {
return commands.get(key)
}

override fun contains(key: String): Boolean {
return commands.exists(key) >= 1
}

override fun del(key: String): Boolean {
return commands.del(key) >= 1
}

override fun expire(key: String, seconds: Int): Boolean {
return commands.expire(key, seconds.toLong())
}

override fun ttl(key: String): Long {
return commands.ttl(key)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.devsrsouza.redissed.delegates

import br.com.devsrsouza.redissed.RedisObject
import br.com.devsrsouza.redissed.get
import br.com.devsrsouza.redissed.set
import br.com.devsrsouza.redissed.parsers.Parser
import kotlin.reflect.KProperty

class ParserDelegate<T>(val parser: Parser<T>, val default: T) {
operator fun getValue(ref: RedisObject, property: KProperty<*>): T {
return get(ref, property)?.let { parser.parse(it) } ?: default
}
operator fun setValue(ref: RedisObject, property: KProperty<*>, value: T) {
set(parser.render(value), ref, property)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.devsrsouza.redissed.delegates

import br.com.devsrsouza.redissed.RedisObject
import br.com.devsrsouza.redissed.get
import br.com.devsrsouza.redissed.setNullable
import br.com.devsrsouza.redissed.parsers.Parser
import kotlin.reflect.KProperty

class ParserDelegateNullable<T>(val parser: Parser<T>) {
operator fun getValue(ref: RedisObject, property: KProperty<*>): T? {
return get(ref, property)?.let { parser.parse(it) }
}

operator fun setValue(ref: RedisObject, property: KProperty<*>, value: T?) {
val render = value?.let { parser.render(it) }
setNullable(render, ref, property)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 02932c9

Please sign in to comment.