All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.definitions.RaptorScalarGraphDefinitionBuilder.kt Maven / Gradle / Ivy

The newest version!
package io.fluidsonic.raptor

import io.fluidsonic.raptor.graphql.internal.*
import kotlin.reflect.*


@RaptorDsl
public class RaptorScalarGraphDefinitionBuilder internal constructor(
	kotlinType: KotlinType,
	name: String,
	private val stackTrace: List,
) : RaptorNamedGraphTypeDefinitionBuilder(
	kotlinType = kotlinType,
	name = name
) {

	private var parse: (RaptorGraphInputScope.(input: Any) -> Any)? = null
	private var serialize: (RaptorGraphOutputScope.(output: Any) -> Any)? = null


	override fun build(description: String?) = ScalarGraphDefinition(
		additionalDefinitions = emptyList(),
		description = description,
		isInput = true,
		isOutput = true,
		kotlinType = kotlinType,
		name = name,
		parse = parse ?: error("A parser must be defined: parse(Boolean/Int/Float/Object/String) { … }"),
		serialize = serialize ?: error("A serializer must be defined: serialize { … }"),
		stackTrace = stackTrace
	)


	@RaptorDsl
	public fun parse(parse: KFunction1) {
		this.parse { input ->
			parse(input)
		}
	}


	@RaptorDsl
	public fun parse(parse: RaptorGraphInputScope.(input: Any) -> Type) {
		check(this.parse === null) { "Cannot define multiple parsers." }

		this.parse = parse
	}


	@RaptorDsl
	public fun parseBoolean(parse: KFunction1) {
		parseBoolean { input ->
			parse(input)
		}
	}


	@RaptorDsl
	public fun parseBoolean(parse: RaptorGraphInputScope.(input: Boolean) -> Type) {
		this.parse { input ->
			parse(input as? Boolean ?: invalid())
		}
	}


	@RaptorDsl
	public fun parseFloat(parse: KFunction1) {
		parseFloat { input ->
			parse(input)
		}
	}


	@RaptorDsl
	public fun parseFloat(parse: RaptorGraphInputScope.(input: Double) -> Type) {
		this.parse { input ->
			parse(input as? Double ?: invalid())
		}
	}


	@RaptorDsl
	public fun parseInt(parse: KFunction1) {
		parseInt { input ->
			parse(input)
		}
	}


	@RaptorDsl
	public fun parseInt(parse: RaptorGraphInputScope.(input: Int) -> Type) {
		this.parse { input ->
			parse(input as? Int ?: invalid())
		}
	}


	@RaptorDsl
	public fun parseObject(parse: KFunction1, Type>) {
		parseObject { input ->
			parse(input)
		}
	}


	@RaptorDsl
	@Suppress("UNCHECKED_CAST")
	public fun parseObject(parse: RaptorGraphInputScope.(input: Map) -> Type) {
		this.parse { input ->
			parse(input as? Map ?: invalid())
		}
	}


	@RaptorDsl
	public fun parseString(parse: KFunction1) {
		parseString { input ->
			parse(input)
		}
	}


	@RaptorDsl
	public fun parseString(parse: RaptorGraphInputScope.(input: String) -> Type) {
		this.parse { input ->
			parse(input as? String ?: invalid())
		}
	}


	@RaptorDsl
	public fun serialize(serialize: KFunction1) {
		serialize { serialize(it) }
	}


	@RaptorDsl
	public fun serialize(serialize: KProperty1) {
		serialize(serialize::invoke)
	}


	@RaptorDsl
	@Suppress("UNCHECKED_CAST")
	public fun serialize(serialize: RaptorGraphOutputScope.(output: Type) -> Any) {
		check(this.serialize == null) { "Cannot define multiple serializers." }

		this.serialize = serialize as RaptorGraphOutputScope.(output: Any) -> Any
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy