Skip to content

Commit

Permalink
Generate automatic routes names.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjaros committed Jun 1, 2018
1 parent 656e823 commit 93cfdf0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,87 +41,87 @@ enum class HttpMethod {
/**
* Multiplatform service manager.
*/
expect open class ServiceManager<out T>(service: T? = null) {
expect open class ServiceManager<out T>(service: T) {
/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected inline fun <reified RET> bind(
route: String,
noinline function: T.(Request?) -> Deferred<RET>,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST,
prefix: String = "/"
)

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected inline fun <reified PAR, reified RET> bind(
route: String,
noinline function: T.(PAR, Request?) -> Deferred<RET>,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST,
prefix: String = "/"
)

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected inline fun <reified PAR1, reified PAR2, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST,
prefix: String = "/"
)

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST,
prefix: String = "/"
)

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST,
prefix: String = "/"
)

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST,
prefix: String = "/"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,30 @@ import org.slf4j.LoggerFactory
* Multiplatform service manager.
*/
@Suppress("EXPERIMENTAL_FEATURE_WARNING")
actual open class ServiceManager<out T> actual constructor(val service: T?) {
actual open class ServiceManager<out T> actual constructor(val service: T) {

companion object {
val LOG: Logger = LoggerFactory.getLogger(ServiceManager::class.java.name)
}

protected val routes: MutableList<JoobyServer.() -> Unit> = mutableListOf()
val mapper = jacksonObjectMapper()
var counter: Int = 0

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified RET> bind(
route: String,
noinline function: T.(Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
routes.add({
call(method, "$prefix$route") { req, res ->
call(method, "$prefix$routeDef") { req, res ->
if (service != null) {
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
try {
Expand All @@ -78,17 +80,18 @@ actual open class ServiceManager<out T> actual constructor(val service: T?) {

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR, reified RET> bind(
route: String,
noinline function: T.(PAR, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
routes.add({
call(method, "$prefix$route") { req, res ->
call(method, "$prefix$routeDef") { req, res ->
if (service != null) {
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
if (jsonRpcRequest.params.size == 1) {
Expand Down Expand Up @@ -117,17 +120,18 @@ actual open class ServiceManager<out T> actual constructor(val service: T?) {

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
routes.add({
call(method, "$prefix$route") { req, res ->
call(method, "$prefix$routeDef") { req, res ->
if (service != null) {
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
if (jsonRpcRequest.params.size == 2) {
Expand Down Expand Up @@ -157,17 +161,18 @@ actual open class ServiceManager<out T> actual constructor(val service: T?) {

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
routes.add({
call(method, "$prefix$route") { req, res ->
call(method, "$prefix$routeDef") { req, res ->
if (service != null) {
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
if (jsonRpcRequest.params.size == 3) {
Expand Down Expand Up @@ -198,17 +203,18 @@ actual open class ServiceManager<out T> actual constructor(val service: T?) {

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
routes.add({
call(method, "$prefix$route") { req, res ->
call(method, "$prefix$routeDef") { req, res ->
if (service != null) {
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
if (jsonRpcRequest.params.size == 4) {
Expand Down Expand Up @@ -241,20 +247,21 @@ actual open class ServiceManager<out T> actual constructor(val service: T?) {

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
reified PAR4, reified PAR5, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>,
route: String?,
method: RpcHttpMethod,
prefix: String
) {
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
routes.add({
call(method, "$prefix$route") { req, res ->
call(method, "$prefix$routeDef") { req, res ->
if (service != null) {
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
if (jsonRpcRequest.params.size == 5) {
Expand Down
57 changes: 32 additions & 25 deletions src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,94 +26,101 @@ import kotlinx.coroutines.experimental.Deferred
/**
* Multiplatform service manager.
*/
actual open class ServiceManager<out T> actual constructor(service: T?) {
actual open class ServiceManager<out T> actual constructor(service: T) {
protected val calls: MutableMap<String, Pair<String, RpcHttpMethod>> = mutableMapOf()
var counter: Int = 0

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified RET> bind(
route: String,
noinline function: T.(Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
calls[function.toString()] = Pair("$prefix$route", method)
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
calls[function.toString()] = Pair("$prefix$routeDef", method)
}

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR, reified RET> bind(
route: String,
noinline function: T.(PAR, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
calls[function.toString()] = Pair("$prefix$route", method)
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
calls[function.toString()] = Pair("$prefix$routeDef", method)
}

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
calls[function.toString()] = Pair("$prefix$route", method)
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
calls[function.toString()] = Pair("$prefix$routeDef", method)
}

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
calls[function.toString()] = Pair("$prefix$route", method)
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
calls[function.toString()] = Pair("$prefix$routeDef", method)
}

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>, method: RpcHttpMethod, prefix: String
noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>,
route: String?, method: RpcHttpMethod, prefix: String
) {
calls[function.toString()] = Pair("$prefix$route", method)
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
calls[function.toString()] = Pair("$prefix$routeDef", method)
}

/**
* Binds a given route with a function of the receiver.
* @param route a route
* @param function a function of the receiver
* @param route a route
* @param method a HTTP method
* @param prefix an URL address prefix
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
reified PAR4, reified PAR5, reified RET> bind(
route: String,
noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>,
route: String?,
method: RpcHttpMethod,
prefix: String
) {
calls[function.toString()] = Pair("$prefix$route", method)
val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}"
calls[function.toString()] = Pair("$prefix$routeDef", method)
}

/**
Expand All @@ -127,4 +134,4 @@ actual open class ServiceManager<out T> actual constructor(service: T?) {
* Returns the map of defined paths.
*/
actual fun getCalls(): Map<String, Pair<String, RpcHttpMethod>> = calls
}
}

0 comments on commit 93cfdf0

Please sign in to comment.