Kotlin Cursor Compiler

Kotlin Cursor & ContentValues boilerplate annotation processor.

License

License

Categories

Categories

Kotlin Languages Ant Build Tools
GroupId

GroupId

com.github.danysantiago
ArtifactId

ArtifactId

kotlincursor-compiler
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Kotlin Cursor Compiler
Kotlin Cursor & ContentValues boilerplate annotation processor.
Project URL

Project URL

https://github.com/danysantiago/kotlin-cursor
Source Code Management

Source Code Management

https://github.com/danysantiago/kotlin-cursor

Download kotlincursor-compiler

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.danysantiago/kotlincursor-compiler/ -->
<dependency>
    <groupId>com.github.danysantiago</groupId>
    <artifactId>kotlincursor-compiler</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.danysantiago/kotlincursor-compiler/
implementation 'com.github.danysantiago:kotlincursor-compiler:0.1.1'
// https://jarcasting.com/artifacts/com.github.danysantiago/kotlincursor-compiler/
implementation ("com.github.danysantiago:kotlincursor-compiler:0.1.1")
'com.github.danysantiago:kotlincursor-compiler:jar:0.1.1'
<dependency org="com.github.danysantiago" name="kotlincursor-compiler" rev="0.1.1">
  <artifact name="kotlincursor-compiler" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.danysantiago', module='kotlincursor-compiler', version='0.1.1')
)
libraryDependencies += "com.github.danysantiago" % "kotlincursor-compiler" % "0.1.1"
[com.github.danysantiago/kotlincursor-compiler "0.1.1"]

Dependencies

compile (4)

Group / Artifact Type Version
com.github.danysantiago : kotlincursor-api jar 0.1.1
org.jetbrains.kotlin : kotlin-stdlib jar 1.1.2-4
com.google.auto.service : auto-service jar 1.0-rc2
com.squareup : kotlinpoet jar 0.2.0

test (4)

Group / Artifact Type Version
junit : junit jar 4.12
com.google.testing.compile : compile-testing jar 0.11
com.google.truth : truth jar 0.33
com.google.guava : guava jar 22.0

Project Modules

There are no modules declared in this project.

Kotlin-Cursor

An annotation processor that generates extension functions to convert Kotlin data classes to ContentValues and from Cursor.

Usage

Annotate your data class with @KCursorData

@KCursorData
data class Cat(val name: String)

then once the class is processed a file will be generated with the following extension functions:

fun Cat.toContentValues(): ContentValues { /* ... */ }
fun Cursor.toCat(): Cat { /* ... */ }

Custom types

The following types are supported by default:

  • String
  • ByteArray
  • Double
  • Float
  • Int
  • Long
  • Short
  • Boolean

Kotlin-Cursor also supports types that are also annotated with @KCursorData. For exmaple:

@KCursorData
data class Bed(val owner: Cat)

For other types, you need to use the @ColumnAdapter annotation and specify a class that implements ColumnTypeAdapter. For example:

Person.kt

data class Person(
    val name: String,
    @ColumnAdapter(CatListAdapter::class)
    val cats: List<Cat>
)

CatListColumnTypeAdapter.kt:

class CatListAdapter : ColumnTypeAdapter<List<Cat>> {
    override fun fromCursor(cursor: Cursor, columnName: String): List<Cat> {
        val listOfCats = cursor.getString(cursor.getColumnIndexOrThrow("cat_list"))
        return listOfCats.split(',').map { Cat(it) }
    }

    override fun toContentValues(values: ContentValues, columnName: String, value: List<Cat>) {
        values.put("cat_list", value.map { it.name }.joinToString(separator = ","))
    }
}

Setup

Add a Gradle dependency:

compile 'com.github.danysantiago:kotlincursor-api:0.1.1'
kapt 'com.github.danysantiago:kotlincursor-compiler:0.1.1'

Snapshots of the development version are available in Sonatype's snapshots repository.

Thanks

To Gabriel Ittner's auto-value-cursor for which this project is based on.

Versions

Version
0.1.1
0.1.0