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

handle default value of operation #7

Open
yshrsmz opened this issue Jan 17, 2019 · 5 comments · May be fixed by #23
Open

handle default value of operation #7

yshrsmz opened this issue Jan 17, 2019 · 5 comments · May be fixed by #23

Comments

@yshrsmz
Copy link
Owner

yshrsmz commented Jan 17, 2019

No description provided.

@yshrsmz yshrsmz mentioned this issue Jan 28, 2019
@yshrsmz
Copy link
Owner Author

yshrsmz commented Apr 22, 2019

Kotlin/kotlinx.serialization#276

JsonObject could be the key.
Instead of generating Variables class

@yshrsmz
Copy link
Owner Author

yshrsmz commented Apr 22, 2019

tested with iosX64

enum class Type {
    FOO, BAR
}

@Serializable
data class Name(val value: String)

@Serializable
data class User(val id: String, val age: Int, val name: Name, val type: Type)

fun test() {
    val json = json {
        "key" to "string"
        "key2" to 0
        "user" to Json.plain.toJson(User.serializer(), User("idstring", 32, Name("namestring"), Type.BAR))
    }

    val stringified = Json.stringify(JsonObjectSerializer, json)
    println("map: $stringified")
    // map: {"key":"string","key2":0,"user":{"id":"idstring","age":32,"name":{"value":"namestring"},"type":"BAR"}}
}

root element can be JsonObject. This way we can ignore value for arguments not provided

@yshrsmz
Copy link
Owner Author

yshrsmz commented Apr 23, 2019

draft

sealed class KgqlValue<out T : Any?> {
    data class Some<out T : Any?>(val value: T) : KgqlValue<T>()
    object None : KgqlValue<Nothing>()
}

class Variables(val login: String) {
    private var name: KgqlValue<Int?> = KgqlValue.None
    private var id: KgqlValue<Int?> = KgqlValue.None
    private var company: KgqlValue<String?> = KgqlValue.None
    private var foo: KgqlValue<Float?> = KgqlValue.None
    private var logins: KgqlValue<List<String?>?> = KgqlValue.None

    fun name(name: Int?): Variables2 {
        this.name = KgqlValue.Some(name)
        return this
    }

    fun id(id: Int?): Variables2 {
        this.id = KgqlValue.Some(id)
        return this
    }

    fun company(company: String?): Variables2 {
        this.company = KgqlValue.Some(company)
        return this
    }

    fun foo(foo: Float?): Variables2 {
        this.foo = KgqlValue.Some(foo)
        return this
    }

    fun logins(logins: List<String?>?): Variables2 {
        this.logins = KgqlValue.Some(logins)
        return this
    }

    fun asJsonObject(): JsonObject {
        return json {
            "login" to login
            (name as? KgqlValue.Some)?.let { "name" to it.value }
            (id as? KgqlValue.Some)?.let { "id" to it.value }
            (company as? KgqlValue.Some)?.let { "company" to it.value }
            (foo as? KgqlValue.Some)?.let { "foo" to it.value }
            (logins as? KgqlValue.Some)?.let {
                "logins" to Json.plain.toJson(
                    NullableSerializer(NullableSerializer(String.serializer()).list),
                    it.value
                )
            }
        }
    }
}
  • introduce KgqlValue to express implicit/explicit null.
  • required parameter is set through constructor
  • use builder pattern to set optional field

@yshrsmz yshrsmz reopened this Apr 23, 2019
@yshrsmz
Copy link
Owner Author

yshrsmz commented Apr 23, 2019

in order to serialize enum value properly, we need to know if a target value is enum or not. But to do this, we need schema. Without schema we can not distinguish enum from other custom type

we first need to resolve #1

@yshrsmz
Copy link
Owner Author

yshrsmz commented Apr 23, 2019

another workaround is to let users define serializer function for enums like below

enum class Type {
    FOO, BAR;

    companion object {
        fun serializer(): KSerializer<Type> = EnumSerializer(Type::class)
    }
}

yshrsmz added a commit that referenced this issue Apr 23, 2019
@yshrsmz yshrsmz linked a pull request Apr 25, 2019 that will close this issue
8 tasks
yshrsmz added a commit that referenced this issue Jun 23, 2019
yshrsmz added a commit that referenced this issue Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant