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

commonMain.com.zegreatrob.tools.digger.json.ContributionDataJson.kt Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
package com.zegreatrob.tools.digger.json

import com.zegreatrob.tools.digger.model.Contribution
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
internal data class ContributionJson(
    val lastCommit: String,
    val firstCommit: String,
    val authors: List,
    val dateTime: Instant? = null,
    val firstCommitDateTime: Instant? = null,
    val commitCount: Int,
    val ease: Int? = null,
    val storyId: String? = null,
    val semver: String? = null,
    val label: String? = null,
    val tagName: String? = null,
    val tagDateTime: Instant? = null,
)

fun Iterable.toJsonString(): String = Json.encodeToString(map(Contribution::toJsonModel))

fun Contribution.toJsonString(): String = Json.encodeToString(toJsonModel())

object ContributionParser {

    private val json = Json {
        ignoreUnknownKeys = true
    }

    fun parseContributions(jsonString: String) =
        json.decodeFromString>(jsonString)
            .map(ContributionJson::toModel)

    fun parseContribution(jsonString: String) =
        json.decodeFromString(jsonString)
            ?.toModel()
}

private fun Contribution.toJsonModel() =
    ContributionJson(
        lastCommit = lastCommit,
        firstCommit = firstCommit,
        authors = authors,
        dateTime = dateTime,
        firstCommitDateTime = firstCommitDateTime,
        ease = ease,
        storyId = storyId,
        semver = semver,
        label = label,
        tagName = tagName,
        tagDateTime = tagDateTime,
        commitCount = commitCount,
    )

private fun ContributionJson.toModel() = Contribution(
    lastCommit = lastCommit,
    firstCommit = firstCommit,
    authors = authors,
    dateTime = dateTime,
    firstCommitDateTime = firstCommitDateTime,
    ease = ease,
    storyId = storyId,
    semver = semver,
    label = label,
    tagName = tagName,
    tagDateTime = tagDateTime,
    commitCount = commitCount,
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy