Skip to content

Commit

Permalink
⬆️ [Update] Upgrade dependencies and small fixes (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
kittinunf authored Sep 5, 2020
1 parent d27c622 commit cbe55be
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 128 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact
import org.jmailen.gradle.kotlinter.support.ReporterType

plugins {
kotlin("jvm") version "1.3.60"
id("org.jmailen.kotlinter") version "2.3.0"
kotlin("jvm") version "1.4.0"
id("org.jmailen.kotlinter") version "3.0.2"

jacoco
`maven-publish`
Expand Down
8 changes: 4 additions & 4 deletions forge/src/main/kotlin/com/github/kittinunf/forge/Forge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import org.json.JSONObject
object Forge {

fun <T : Any, U : Deserializable<T>> modelFromJson(json: String, deserializer: U): DeserializedResult<T> =
deserializer.deserialize(JSON.parse(JSONObject(json)))
deserializer.deserialize(JSON.parse(JSONObject(json)))

fun <T : Any> modelFromJson(json: String, deserializer: JSON.() -> DeserializedResult<T>): DeserializedResult<T> =
JSON.parse(JSONObject(json)).deserializer()
JSON.parse(JSONObject(json)).deserializer()

fun <T : Any, U : Deserializable<T>> modelsFromJson(json: String, deserializer: U): DeserializedResult<List<T>> =
JSON.parse(JSONArray(json)).toList().map(deserializer::deserialize).lift()
JSON.parse(JSONArray(json)).toList().map(deserializer::deserialize).lift()

fun <T : Any> modelsFromJson(json: String, deserializer: JSON.() -> DeserializedResult<T>): DeserializedResult<List<T>> =
JSON.parse(JSONArray(json)).toList().map(deserializer).lift()
JSON.parse(JSONArray(json)).toList().map(deserializer).lift()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.github.kittinunf.forge.core
interface Deserializable<out T : Any> {

fun deserialize(json: JSON): DeserializedResult<T>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ sealed class ForgeError(message: String) : Exception(message)
class MissingAttributeError(val key: String) : ForgeError("Attribute is Missing - key \"$key\" is not found")

class AttributeTypeInvalidError(val key: String, val expectedClass: Class<*>, val receivedValue: Any) :
ForgeError("Attribute Type Invalid - key: \"$key\", expected type: $expectedClass, received value: ${if (receivedValue is String) "\"$receivedValue\"" else receivedValue}")
ForgeError("Attribute Type Invalid - key: \"$key\", expected type: $expectedClass, received value: ${if (receivedValue is String) "\"$receivedValue\"" else receivedValue}")
14 changes: 8 additions & 6 deletions forge/src/main/kotlin/com/github/kittinunf/forge/core/JSON.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ sealed class JSON : Sequence<JSON> {
is JSONObject -> return parse(toMap(json))
is JSONArray -> return parse(toList(json))

is Map<*, *> -> return Object((json as Map<kotlin.String, Any>).asSequence().fold(mutableMapOf()) { accum, entry ->
val (key, value) = entry
val jsonValue = parse(value)
accum += key to jsonValue
accum
})
is Map<*, *> -> return Object(
(json as Map<kotlin.String, Any>).asSequence().fold(mutableMapOf()) { accum, entry ->
val (key, value) = entry
val jsonValue = parse(value)
accum += key to jsonValue
accum
}
)

is List<*> -> return Array(json.map { parse(it!!) })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ infix fun <T, U> Function1<T, U>.map(result: DeserializedResult<T>) = result.map
fun <T, U> DeserializedResult<(T) -> U>.apply(result: DeserializedResult<T>): DeserializedResult<U> = flatMap(result::map)

inline infix fun <reified T> JSON.at(key: String): DeserializedResult<T> =
at(key, deserializer = { deserializeAs<T>(key) })
at(key, deserializer = { deserializeAs<T>(key) })

inline infix fun <reified T> JSON.maybeAt(key: String): DeserializedResult<T> =
maybeAt(key, deserializer = { deserializeAs<T>(key) })
maybeAt(key, deserializer = { deserializeAs<T>(key) })

fun <T> JSON.at(key: String, deserializer: JSON.() -> DeserializedResult<T>): DeserializedResult<T> =
find(key)?.deserializer() ?: Failure(MissingAttributeError(key))
find(key)?.deserializer() ?: Failure(MissingAttributeError(key))

@Suppress("UNCHECKED_CAST")
fun <T> JSON.maybeAt(key: String, deserializer: JSON.() -> DeserializedResult<T>): DeserializedResult<T> =
find(key)?.deserializer() ?: Success(null) as DeserializedResult<T>
find(key)?.deserializer() ?: Success(null) as DeserializedResult<T>

inline infix fun <reified T> JSON.list(key: String): DeserializedResult<List<T>> =
list(key, deserializer = { deserializeAs<T>(key) })
list(key, deserializer = { deserializeAs<T>(key) })

inline infix fun <reified T> JSON.maybeList(key: String): DeserializedResult<List<T>> =
maybeList(key, deserializer = { deserializeAs<T>(key) })
maybeList(key, deserializer = { deserializeAs<T>(key) })

fun <T> JSON.list(key: String, deserializer: JSON.() -> DeserializedResult<T>): DeserializedResult<List<T>> =
find(key)?.map(deserializer)?.toList()?.lift() ?: Failure(MissingAttributeError(key))
find(key)?.map(deserializer)?.toList()?.lift() ?: Failure(MissingAttributeError(key))

@Suppress("UNCHECKED_CAST")
fun <T> JSON.maybeList(key: String, deserializer: JSON.() -> DeserializedResult<T>): DeserializedResult<List<T>> =
find(key)?.map(deserializer)?.toList()?.lift() ?: Success(null) as DeserializedResult<List<T>>
find(key)?.map(deserializer)?.toList()?.lift() ?: Success(null) as DeserializedResult<List<T>>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.github.kittinunf.forge.core
interface Serializable {

fun serialize(): JSON
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fun toDate(style: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"): (String) -> Date = {
}

fun JSON.deserializeDate(key: String, style: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"): DeserializedResult<Date> =
when (this) {
is JSON.String -> {
Success(toDate(style).invoke(value))
}
else -> Failure(AttributeTypeInvalidError(key, javaClass, value))
when (this) {
is JSON.String -> {
Success(toDate(style).invoke(value))
}
else -> Failure(AttributeTypeInvalidError(key, javaClass, value))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import com.github.kittinunf.forge.core.JSON.Null
import com.github.kittinunf.result.Result.Failure
import com.github.kittinunf.result.Result.Success

inline fun <reified T : Any?> JSON.deserializeAs(key: String? = null): DeserializedResult<T> = when (this) {
inline fun <reified T> JSON.deserializeAs(key: String): DeserializedResult<T> = when (this) {
is Null -> Success(null as T)
else -> {
(value as? T)?.let(::Success) ?: Failure(AttributeTypeInvalidError(key ?: "(null)", T::class.java, value))
(value as? T)?.let { Success(it) } ?: Failure(AttributeTypeInvalidError(key, T::class.java, value))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ fun JSONArray.asSequence(): Sequence<Any> {
override fun hasNext() = it.hasNext()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ fun JSONObject.asSequence(): Sequence<Pair<String, Any>> {
override fun hasNext() = it.hasNext()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.github.kittinunf.forge.model.User
import com.github.kittinunf.forge.model.UserWithOptionalFields
import com.github.kittinunf.forge.util.create
import com.github.kittinunf.forge.util.curry

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.nullValue
import org.hamcrest.MatcherAssert.assertThat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ class JSONMappingArrayTest : BaseTest() {
class UserDeserializer : Deserializable<User> {
override fun deserialize(json: JSON): DeserializedResult<User> {
return ::User.create
.map(json at "id")
.apply(json at "username")
.apply(json at "name")
.apply(json at "age")
.apply(json at "email")
.apply(json maybeList "levels")
.apply(json.maybeAt("friend", FriendDeserializer()::deserialize))
.map(json at "id")
.apply(json at "username")
.apply(json at "name")
.apply(json at "age")
.apply(json at "email")
.apply(json maybeList "levels")
.apply(json.maybeAt("friend", FriendDeserializer()::deserialize))
}
}

val companyDeserializer = { json: JSON ->
::Company.create
.map(json at "name")
.apply(json at "catch_phrase")
.map(json at "name")
.apply(json at "catch_phrase")
}

val userModelWithCompany = { json: JSON ->
::UserWithCompany.create
.map(json at "id")
.apply(json at "username")
.apply(json maybeAt "is_deleted")
.apply(json.at("company", companyDeserializer))
.map(json at "id")
.apply(json at "username")
.apply(json maybeAt "is_deleted")
.apply(json.at("company", companyDeserializer))
}

@Test
Expand Down Expand Up @@ -74,16 +74,16 @@ class JSONMappingArrayTest : BaseTest() {

val dogDeserializer = { j: JSON ->
::Dog.create
.map(j at "name")
.apply(j at "breed")
.apply(j at "is_male")
.map(j at "name")
.apply(j at "breed")
.apply(j at "is_male")
}

val userWithDogDeserializer = { j: JSON ->
::UserWithDogs.create
.map(j at "email")
.apply(j at "phone")
.apply(j.maybeList("dogs", dogDeserializer))
.map(j at "email")
.apply(j at "phone")
.apply(j.maybeList("dogs", dogDeserializer))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class JSONMappingObjectTest : BaseTest() {

class SimpleUserDeserializer : Deserializable<SimpleUser> {
override fun deserialize(json: JSON): DeserializedResult<SimpleUser> =
::SimpleUser.create
.map(json at "id")
.apply(json at "name")
::SimpleUser.create
.map(json at "id")
.apply(json at "name")
}

@Test
Expand All @@ -48,14 +48,14 @@ class JSONMappingObjectTest : BaseTest() {

class UserDeserializer : Deserializable<User> {
override fun deserialize(json: JSON): DeserializedResult<User> =
::User.create
.map(json at "id")
.apply(json at "username")
.apply(json at "name")
.apply(json at "age")
.apply(json at "email")
.apply(json list "levels")
.apply(json.at("friend", FriendDeserializer()::deserialize))
::User.create
.map(json at "id")
.apply(json at "username")
.apply(json at "name")
.apply(json at "age")
.apply(json at "email")
.apply(json list "levels")
.apply(json.at("friend", FriendDeserializer()::deserialize))
}

@Test
Expand Down Expand Up @@ -84,16 +84,16 @@ class JSONMappingObjectTest : BaseTest() {

val companyDeserializer = { json: JSON ->
::Company.create
.map(json at "name")
.apply(json at "catch_phrase")
.map(json at "name")
.apply(json at "catch_phrase")
}

val userModelWithCompanyDeserializer = { json: JSON ->
::UserWithCompany.create
.map(json at "id")
.apply(json at "username")
.apply(json at "is_deleted")
.apply(json.at("company", companyDeserializer))
.map(json at "id")
.apply(json at "username")
.apply(json at "is_deleted")
.apply(json.at("company", companyDeserializer))
}

@Test
Expand All @@ -111,17 +111,17 @@ class JSONMappingObjectTest : BaseTest() {
class UserCreatedAt1Deserializer : Deserializable<UserCreatedAt> {

override fun deserialize(json: JSON): DeserializedResult<UserCreatedAt> =
::UserCreatedAt.create
.map(json at "id")
.apply(json.at("created_at", deserializer = { deserializeDate("created_at") }))
::UserCreatedAt.create
.map(json at "id")
.apply(json.at("created_at", deserializer = { deserializeDate("created_at") }))
}

class UserCreatedAt2Deserializer : Deserializable<UserCreatedAt> {

override fun deserialize(json: JSON): DeserializedResult<UserCreatedAt> =
::UserCreatedAt.create
.map(json at "id")
.apply(toDate() map (json at "created_at"))
::UserCreatedAt.create
.map(json at "id")
.apply(toDate() map (json at "created_at"))
}

@Test
Expand Down Expand Up @@ -168,12 +168,12 @@ class JSONMappingObjectTest : BaseTest() {
class UserWithOptionalFieldsDeserializer : Deserializable<UserWithOptionalFields> {

override fun deserialize(json: JSON): DeserializedResult<UserWithOptionalFields> =
::UserWithOptionalFields.create
.map(json at "name")
.apply(json maybeAt "city")
.apply(json maybeAt "gender")
.apply(json at "phone")
.apply(json at "weight")
::UserWithOptionalFields.create
.map(json at "name")
.apply(json maybeAt "city")
.apply(json maybeAt "gender")
.apply(json at "phone")
.apply(json at "weight")
}

@Test
Expand All @@ -191,28 +191,28 @@ class JSONMappingObjectTest : BaseTest() {
class AddressDeserializer : Deserializable<Friend.Address> {

override fun deserialize(json: JSON): DeserializedResult<Friend.Address> =
::Address.create
.map(json at "street")
.apply(json at "suite")
.apply(json at "city")
::Address.create
.map(json at "street")
.apply(json at "suite")
.apply(json at "city")
}

class FriendDeserializer : Deserializable<Friend> {
override fun deserialize(json: JSON): DeserializedResult<Friend> =
::Friend.create
.map(json at "id")
.apply(json at "name")
.apply(json.at("address", AddressDeserializer()::deserialize))
::Friend.create
.map(json at "id")
.apply(json at "name")
.apply(json.at("address", AddressDeserializer()::deserialize))
}

class UserWithFriendsDeserializer : Deserializable<UserWithFriends> {
override fun deserialize(json: JSON): DeserializedResult<UserWithFriends> =
::UserWithFriends.create
.map(json at "id")
.apply(json at "name")
.apply(json at "age")
.apply(json at "email")
.apply(json.list("friends", FriendDeserializer()::deserialize))
::UserWithFriends.create
.map(json at "id")
.apply(json at "name")
.apply(json at "age")
.apply(json at "email")
.apply(json.list("friends", FriendDeserializer()::deserialize))
}

@Test
Expand Down
Loading

0 comments on commit cbe55be

Please sign in to comment.