Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support boolean type in Argument #15

Merged
merged 2 commits into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions core/src/main/kotlin/me/lazmaid/kraph/lang/DataEntry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.lazmaid.kraph.lang

import javax.xml.crypto.Data

/**
* Created by vwongsawangt on 8/6/2017 AD.
*/

internal sealed class DataEntry {
abstract fun print(prettyFormat: Boolean): String

class NonDecimalNumberData(val value: Long) : DataEntry() {
override fun print(prettyFormat: Boolean) = value.toString()
}

class DecimalNumberData(val value: Double) : DataEntry() {
override fun print(prettyFormat: Boolean) = value.toString()
}

class BooleanData(val value: Boolean) : DataEntry() {
override fun print(prettyFormat: Boolean) = value.toString()
}

class StringData(val value: String) : DataEntry() {
override fun print(prettyFormat: Boolean) =
if (prettyFormat) {
"\"$value\""
} else {
"\\\"$value\\\""
}
}

class ArrayData(val values: List<DataEntry>) : DataEntry() {
override fun print(prettyFormat: Boolean) =
values.foldIndexed("[") { index, acc, item ->
var newAcc = acc + item.print(prettyFormat)
if (index != values.size - 1) {
newAcc += ", "
} else {
newAcc += "]"
}
newAcc
}
}

class ObjectData(val values: List<Pair<String, DataEntry>>) : DataEntry() {
override fun print(prettyFormat: Boolean) =
values.foldIndexed("{") { index, acc, (k, v) ->
var newAcc = acc + "${k.wrappedWithQuotes(prettyFormat)}: ${v.print(prettyFormat)}"
if (index != values.size - 1) {
newAcc += ", "
} else {
newAcc += "}"
}
newAcc
}
}
}

49 changes: 3 additions & 46 deletions core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ abstract internal class GraphQLNode {
is Float -> {
DataEntry.DecimalNumberData(value.toDouble())
}
is Boolean -> {
DataEntry.BooleanData(value)
}
is Double -> {
DataEntry.DecimalNumberData(value)
}
Expand All @@ -61,49 +64,3 @@ internal fun String.wrappedWithQuotes(shouldEscaped: Boolean) =
"\\\"$this\\\""
}

internal sealed class DataEntry {
abstract fun print(prettyFormat: Boolean): String

class NonDecimalNumberData(val value: Long) : DataEntry() {
override fun print(prettyFormat: Boolean) = value.toString()
}

class DecimalNumberData(val value: Double) : DataEntry() {
override fun print(prettyFormat: Boolean) = value.toString()
}

class StringData(val value: String) : DataEntry() {
override fun print(prettyFormat: Boolean) =
if (prettyFormat) {
"\"$value\""
} else {
"\\\"$value\\\""
}
}

class ArrayData(val values: List<DataEntry>) : DataEntry() {
override fun print(prettyFormat: Boolean) =
values.foldIndexed("[") { index, acc, item ->
var newAcc = acc + item.print(prettyFormat)
if (index != values.size - 1) {
newAcc += ", "
} else {
newAcc += "]"
}
newAcc
}
}

class ObjectData(val values: List<Pair<String, DataEntry>>) : DataEntry() {
override fun print(prettyFormat: Boolean) =
values.foldIndexed("{") { index, acc, (k, v) ->
var newAcc = acc + "${k.wrappedWithQuotes(prettyFormat)}: ${v.print(prettyFormat)}"
if (index != values.size - 1) {
newAcc += ", "
} else {
newAcc += "}"
}
newAcc
}
}
}
13 changes: 13 additions & 0 deletions core/src/test/kotlin/me/lazmaid/kraph/test/GraphQLPrintSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ class GraphQLPrintSpek : Spek({
}
}
}
given("isCancel as argument and value as false") {
val node = Argument(mapOf("isCancel" to false))
on("print pretty") {
it("should print (isCancel: false)") {
assertThat(node.print(true, 0), equalTo("(isCancel: false)"))
}
}
on("print normal") {
it("should print (isCancel: false)") {
assertThat(node.print(false, 0), equalTo("(isCancel: false)"))
}
}
}
given("id and title as arguments and value as 1 and Kraph") {
val node = Argument(mapOf("id" to 1, "title" to "Kraph"))
on("print pretty") {
Expand Down