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 1 commit
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
Next Next commit
Add support for boolean type
  • Loading branch information
Verachad (Birth) authored and Verachad (Birth) committed Aug 6, 2017
commit 14ae634ad6077b84e3dc04836f049d7f6fdc16a0
65 changes: 65 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,65 @@
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
}
}
}

enum class ScalarType {
INTEGER,
FLOAT,
BOOLEAN,
STRING
}
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
}
}
}