-
Notifications
You must be signed in to change notification settings - Fork 0
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
Christian Würthner
committed
Nov 7, 2024
1 parent
bacab7f
commit 0165a42
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
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
18 changes: 18 additions & 0 deletions
18
figex-core/src/main/kotlin/com/iodigital/figex/models/figma/FigmaVariableValueCollection.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.iodigital.figex.models.figma | ||
|
||
import com.iodigital.figex.serializer.VariableValueCollectionSerializer | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable(with = VariableValueCollectionSerializer::class) | ||
internal sealed class FigmaVariableValueCollection : Collection<FigmaVariableValue> { | ||
|
||
data class List( | ||
val values: kotlin.collections.List<FigmaVariableValue> | ||
) : FigmaVariableValueCollection(), | ||
kotlin.collections.List<FigmaVariableValue> by values | ||
|
||
class Map( | ||
val values: kotlin.collections.Map<String, FigmaVariableValue> | ||
) : FigmaVariableValueCollection(), | ||
Collection<FigmaVariableValue> by values.values | ||
} |
41 changes: 41 additions & 0 deletions
41
...-core/src/main/kotlin/com/iodigital/figex/serializer/VariableValueCollectionSerializer.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,41 @@ | ||
package com.iodigital.figex.serializer | ||
|
||
import com.iodigital.figex.models.figma.FigmaVariableValue | ||
import com.iodigital.figex.models.figma.FigmaVariableValueCollection | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonDecoder | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
|
||
internal object VariableValueCollectionSerializer : KSerializer<FigmaVariableValueCollection> { | ||
override val descriptor: SerialDescriptor = | ||
PrimitiveSerialDescriptor("value", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): FigmaVariableValueCollection { | ||
val tree = (decoder as JsonDecoder).decodeJsonElement() | ||
return tree.getValue(decoder.json) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: FigmaVariableValueCollection) = | ||
throw UnsupportedOperationException("Not implemented") | ||
|
||
private fun JsonElement.getValue(json: Json): FigmaVariableValueCollection = when (this) { | ||
is JsonArray -> FigmaVariableValueCollection.List( | ||
json.decodeFromJsonElement<List<FigmaVariableValue>>(this) | ||
) | ||
|
||
is JsonObject -> FigmaVariableValueCollection.Map( | ||
json.decodeFromJsonElement<Map<String, FigmaVariableValue>>(this) | ||
) | ||
|
||
else -> throw IllegalStateException("Dead end: $this") | ||
} | ||
} |