-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6368d79
commit c841177
Showing
17 changed files
with
276 additions
and
72 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
api/src/main/kotlin/com/possible_triangle/skygrid/api/events/SkygridElements.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,42 @@ | ||
package com.possible_triangle.skygrid.api.events | ||
|
||
import com.possible_triangle.skygrid.platform.Services | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.serializer | ||
import kotlin.reflect.KClass | ||
|
||
interface RegisterElementEvent { | ||
|
||
companion object { | ||
val EVENT = Services.EVENTS.createEvent(RegisterElementEvent::class) | ||
} | ||
|
||
fun <TParent : Any, TSub : TParent> register( | ||
parent: KClass<TParent>, | ||
subclass: KClass<TSub>, | ||
serializer: KSerializer<TSub>, | ||
) | ||
|
||
} | ||
|
||
interface RegisterElementBuilder<TParent : Any> { | ||
fun <TSub : TParent> subclass(clazz: KClass<TSub>, serializer: KSerializer<TSub>) | ||
} | ||
|
||
inline fun <TParent : Any, reified TSub : TParent> RegisterElementEvent.register( | ||
parent: KClass<TParent>, | ||
subclass: KClass<TSub>, | ||
) = register(parent, subclass, serializer()) | ||
|
||
inline fun <TParent : Any, reified TSub : TParent> RegisterElementBuilder<TParent>.subclass(clazz: KClass<TSub>) = | ||
subclass(clazz, serializer()) | ||
|
||
|
||
inline fun <TParent : Any> RegisterElementEvent.register( | ||
parent: KClass<TParent>, | ||
builder: RegisterElementBuilder<TParent>.() -> Unit, | ||
) = object : RegisterElementBuilder<TParent> { | ||
override fun <TSub : TParent> subclass(clazz: KClass<TSub>, serializer: KSerializer<TSub>) { | ||
register(parent, clazz, serializer) | ||
} | ||
}.apply(builder) |
7 changes: 3 additions & 4 deletions
7
...ble_triangle/skygrid/platform/Services.kt → ...ble_triangle/skygrid/platform/Services.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 |
---|---|---|
@@ -1,21 +1,20 @@ | ||
package com.possible_triangle.skygrid.platform | ||
|
||
import com.possible_triangle.skygrid.SkygridMod | ||
import com.possible_triangle.skygrid.platform.services.IConfig | ||
import com.possible_triangle.skygrid.platform.services.IEvents | ||
import com.possible_triangle.skygrid.platform.services.IPlatformHelper | ||
import java.util.* | ||
|
||
object Services { | ||
|
||
val PLATFORM = load(IPlatformHelper::class.java) | ||
val CONFIG = load(IConfig::class.java) | ||
val EVENTS = load(IEvents::class.java) | ||
|
||
private fun <T> load(clazz: Class<T>): T { | ||
val loadedService: T = ServiceLoader.load(clazz) | ||
return ServiceLoader.load(clazz) | ||
.findFirst() | ||
.orElseThrow { NullPointerException("Failed to load service for ${clazz.name}") } | ||
SkygridMod.LOGGER.debug("Loaded $loadedService for service $clazz") | ||
return loadedService | ||
} | ||
|
||
} |
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
api/src/main/kotlin/com/possible_triangle/skygrid/platform/services/IEvents.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,18 @@ | ||
package com.possible_triangle.skygrid.platform.services | ||
|
||
import kotlin.reflect.KClass | ||
|
||
interface EventInvoker<TEvent> { | ||
fun invoke(event: TEvent) | ||
fun addListener(callback: EventCallback<TEvent>) | ||
} | ||
|
||
fun interface EventCallback<T> { | ||
fun accept(event: T) | ||
} | ||
|
||
interface IEvents { | ||
|
||
fun <TEvent : Any> createEvent(clazz: KClass<TEvent>): EventInvoker<TEvent> | ||
|
||
} |
1 change: 0 additions & 1 deletion
1
...grid/platform/services/IPlatformHelper.kt → ...grid/platform/services/IPlatformHelper.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
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
108 changes: 108 additions & 0 deletions
108
common/src/main/kotlin/com/possible_triangle/skygrid/xml/XMLModule.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,108 @@ | ||
package com.possible_triangle.skygrid.xml | ||
|
||
import com.possible_triangle.skygrid.api.events.RegisterElementEvent | ||
import com.possible_triangle.skygrid.api.events.register | ||
import com.possible_triangle.skygrid.api.events.subclass | ||
import com.possible_triangle.skygrid.api.xml.DeserializationException | ||
import com.possible_triangle.skygrid.api.xml.elements.* | ||
import com.possible_triangle.skygrid.api.xml.elements.extras.Cardinal | ||
import com.possible_triangle.skygrid.api.xml.elements.extras.Offset | ||
import com.possible_triangle.skygrid.api.xml.elements.extras.Side | ||
import com.possible_triangle.skygrid.api.xml.elements.filters.ExceptFilter | ||
import com.possible_triangle.skygrid.api.xml.elements.filters.ModFilter | ||
import com.possible_triangle.skygrid.api.xml.elements.filters.NameFilter | ||
import com.possible_triangle.skygrid.api.xml.elements.filters.TagFilter | ||
import com.possible_triangle.skygrid.api.xml.elements.providers.* | ||
import com.possible_triangle.skygrid.api.xml.elements.transformers.CyclePropertyTransformer | ||
import com.possible_triangle.skygrid.api.xml.elements.transformers.SetPropertyTransformer | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.modules.PolymorphicModuleBuilder | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.SerializersModuleBuilder | ||
import kotlinx.serialization.modules.polymorphic | ||
import nl.adaptivity.xmlutil.ExperimentalXmlUtilApi | ||
import nl.adaptivity.xmlutil.serialization.UnknownChildHandler | ||
import nl.adaptivity.xmlutil.serialization.XML | ||
import nl.adaptivity.xmlutil.serialization.XmlSerializationPolicy | ||
import kotlin.reflect.KClass | ||
|
||
private typealias Sub<T> = Pair<KClass<T>, KSerializer<T>> | ||
|
||
private fun SerializersModuleBuilder.collectRegisteredElements() { | ||
val collected = hashMapOf<KClass<*>, MutableCollection<Sub<*>>>() | ||
|
||
RegisterElementEvent.EVENT.invoke(object : RegisterElementEvent { | ||
override fun <TParent : Any, TSub : TParent> register( | ||
parent: KClass<TParent>, | ||
subclass: KClass<TSub>, | ||
serializer: KSerializer<TSub>, | ||
) { | ||
collected.getOrPut(parent, ::hashSetOf).add(subclass to serializer) | ||
} | ||
}) | ||
|
||
fun <TParent : Any, TSub : TParent> PolymorphicModuleBuilder<TParent>.subclass(it: Sub<*>) { | ||
subclass(it.first as KClass<TSub>, it.second as KSerializer<TSub>) | ||
} | ||
|
||
fun <TParent : Any> registerPolymorphic(parent: KClass<TParent>, subclasses: Collection<Sub<*>>) { | ||
polymorphic(parent) { | ||
subclasses.forEach { | ||
subclass(it) | ||
} | ||
} | ||
} | ||
|
||
collected.forEach { (parent, subclasses) -> | ||
registerPolymorphic(parent, subclasses) | ||
} | ||
} | ||
|
||
fun RegisterElementEvent.registerDefaultElements() { | ||
register(BlockProvider::class) { | ||
subclass(BlockList::class) | ||
subclass(Fallback::class) | ||
subclass(Tag::class) | ||
subclass(SingleBlock::class) | ||
subclass(Reference::class) | ||
} | ||
|
||
register(Transformer::class) { | ||
subclass(SetPropertyTransformer::class) | ||
subclass(CyclePropertyTransformer::class) | ||
} | ||
|
||
register(FilterOperator::class) { | ||
subclass(ExceptFilter::class) | ||
} | ||
|
||
register(Extra::class) { | ||
subclass(Side::class) | ||
subclass(Cardinal::class) | ||
subclass(Offset::class) | ||
} | ||
|
||
register(Filter::class) { | ||
subclass(NameFilter::class) | ||
subclass(ModFilter::class) | ||
subclass(TagFilter::class) | ||
} | ||
} | ||
|
||
@ExperimentalXmlUtilApi | ||
fun createXMLModule() = XML(SerializersModule { | ||
collectRegisteredElements() | ||
}) { | ||
defaultPolicy { | ||
indentString = " ".repeat(3) | ||
autoPolymorphic = true | ||
encodeDefault = XmlSerializationPolicy.XmlEncodeDefault.NEVER | ||
unknownChildHandler = UnknownChildHandler { input, _, descriptor, name, candidates -> | ||
throw DeserializationException( | ||
input.locationInfo, | ||
"${descriptor.tagName}/${name ?: "<CDATA>"}", | ||
candidates | ||
) | ||
} | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
common/src/test/kotlin/com/possible_triangle/skygrid/test/mocks/EventsMock.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,26 @@ | ||
package com.possible_triangle.skygrid.test.mocks | ||
|
||
import com.possible_triangle.skygrid.platform.services.EventCallback | ||
import com.possible_triangle.skygrid.platform.services.EventInvoker | ||
import com.possible_triangle.skygrid.platform.services.IEvents | ||
import kotlin.reflect.KClass | ||
|
||
class EventsMock : IEvents { | ||
|
||
class SimpleEventBus<TEvent> : EventInvoker<TEvent> { | ||
private val listeners = hashSetOf<EventCallback<TEvent>>() | ||
|
||
override fun invoke(event: TEvent) { | ||
listeners.forEach { | ||
it.accept(event) | ||
} | ||
} | ||
|
||
override fun addListener(callback: EventCallback<TEvent>) { | ||
listeners.add(callback) | ||
} | ||
} | ||
|
||
override fun <TEvent : Any> createEvent(clazz: KClass<TEvent>) = SimpleEventBus<TEvent>() | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
.../test/resources/META-INF/services/com.possible_triangle.skygrid.platform.services.IEvents
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 @@ | ||
com.possible_triangle.skygrid.test.mocks.EventsMock |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
serializer=au.com.origin.snapshots.serializers.ToStringSnapshotSerializer | ||
comparator=au.com.origin.snapshots.comparators.PlainTextEqualsComparator | ||
reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter | ||
serializer=au.com.origin.snapshots.serializers.v1.ToStringSnapshotSerializer | ||
comparator=au.com.origin.snapshots.comparators.v1.PlainTextEqualsComparator | ||
reporters=au.com.origin.snapshots.reporters.v1.PlainTextSnapshotReporter | ||
snapshot-dir=__snapshots__ | ||
output-dir=../src/test/kotlin | ||
ci-env-var=CI | ||
ci-env-var=CI | ||
update-snapshot=none |
Oops, something went wrong.