-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support saving the list of marshallers during generation to a file
Support passing MarshallersProvider to avoid greedy registration of all serializers on the startup, and just search for them when they are needed. It also solves the `Maybe you forgot to invoke 'register()' method` problem
- Loading branch information
1 parent
3f609d1
commit 354b367
Showing
9 changed files
with
165 additions
and
41 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
rd-kt/rd-framework/src/main/kotlin/com/jetbrains/rd/framework/MarshallersProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.jetbrains.rd.framework | ||
|
||
import com.jetbrains.rd.util.error | ||
import com.jetbrains.rd.util.getLogger | ||
import java.io.InputStream | ||
|
||
interface MarshallersProvider { | ||
object Dummy : MarshallersProvider { | ||
override fun getMarshaller(id: RdId): IMarshaller<*>? = null | ||
} | ||
|
||
companion object { | ||
fun extractMarshallers( | ||
stream: InputStream, | ||
classsLoader: ClassLoader | ||
): List<IMarshaller<*>> = stream.reader().useLines { lines: Sequence<String> -> | ||
lines.map<String, List<String>> { it.split(":") }.map<List<String>, LazyCompanionMarshaller<Any>> { | ||
LazyCompanionMarshaller<Any>(RdId(it.first().toLong()), classsLoader, it.last()) | ||
}.toList() | ||
} | ||
} | ||
|
||
fun getMarshaller(id: RdId): IMarshaller<*>? | ||
} | ||
|
||
class AggregatedMarshallersProvider(val providers: Sequence<MarshallersProvider>) : MarshallersProvider { | ||
override fun getMarshaller(id: RdId): IMarshaller<*>? { | ||
return providers.mapNotNull { it.getMarshaller(id) }.firstOrNull() | ||
} | ||
} | ||
|
||
abstract class MarhallersProviderFromResourcesBase(val resourceName: String) : MarshallersProvider { | ||
private val map: Map<RdId, IMarshaller<*>> by lazy { | ||
val classLoader = javaClass.classLoader | ||
val resource = classLoader.getResourceAsStream(resourceName) ?: run { | ||
getLogger(this::class).error { "$resourceName is not found" } | ||
return@lazy emptyMap() | ||
} | ||
|
||
MarshallersProvider.extractMarshallers(resource, classsLoader = classLoader).associateBy { it.id } | ||
} | ||
|
||
override fun getMarshaller(id: RdId): IMarshaller<*>? = map[id] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
rd-kt/rd-gen/src/main/kotlin/com/jetbrains/rd/generator/nova/MarshallersCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.jetbrains.rd.generator.nova | ||
|
||
import com.jetbrains.rd.util.hash.getPlatformIndependentHash | ||
import java.io.File | ||
|
||
|
||
interface MarshallersCollector { | ||
val shouldGenerateRegistrations: Boolean | ||
|
||
fun addMarshaller(namespace: String, name: String) | ||
} | ||
|
||
object DisabledMarshallersCollector : MarshallersCollector { | ||
override val shouldGenerateRegistrations: Boolean | ||
get() = true | ||
|
||
override fun addMarshaller(namespace: String, name: String) { | ||
} | ||
} | ||
|
||
class RealMarshallersCollector(val marshallersFile: File) : MarshallersCollector { | ||
private val marshallers = mutableSetOf<String>() | ||
|
||
override val shouldGenerateRegistrations: Boolean | ||
get() = false // We may want to add a separate setting here, but for now just disable it | ||
|
||
override fun addMarshaller(namespace: String, name: String) { | ||
marshallers.add("${name.getPlatformIndependentHash()}:${namespace}.${name}") | ||
} | ||
|
||
fun close() { | ||
marshallersFile.parentFile.mkdirs() | ||
marshallersFile.writer().use { writer -> | ||
marshallers.sorted().forEach { | ||
writer.append(it).append("\n") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.