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

app.softwork.kobol.plugins.fir.NullableToZero.kt Maven / Gradle / Ivy

There is a newer version: 0.2.15
Show newest version
package app.softwork.kobol.plugins.fir

import app.softwork.kobol.fir.*
import app.softwork.serviceloader.ServiceLoader

@ServiceLoader(FirPluginBeforePhase::class)
public class NullableToZero : FirPluginBeforePhase {
    override fun invoke(tree: CobolFIRTree): CobolFIRTree = tree.copy(data = tree.data.toNull())
}

private fun CobolFIRTree.DataTree.toNull(): CobolFIRTree.DataTree {
    return copy(
        fileSection = fileSection.toNull(),
        workingStorage = workingStorage.toNull(),
        linkingSection = linkingSection.toNull()
    )
}

@JvmName("ListFileToNull")
private fun List.toNull() = map {
    it.copy(records = it.records.toNull())
}

@JvmName("ListRecordToNull")
private fun List.toNull() =
    map {
        it.toNull()
    }

@JvmName("ListWorkingStorageToNull")
private fun  List.toNull() = map {
    it.toNull()
}

private fun  T.toNull(): T {
    return when (this) {
        is CobolFIRTree.DataTree.WorkingStorage.Elementar.EmptyElementar -> this
        is CobolFIRTree.DataTree.WorkingStorage.Elementar.NumberElementar.Normal -> copy(
            value = value ?: 0.0
        )
        is CobolFIRTree.DataTree.WorkingStorage.Elementar.NumberElementar.ReturnCode -> this

        is CobolFIRTree.DataTree.WorkingStorage.Elementar.Pointer -> this
        is CobolFIRTree.DataTree.WorkingStorage.Elementar.StringElementar -> copy(
            value = value ?: "",
        )

        is CobolFIRTree.DataTree.WorkingStorage.Record -> copy(
            elements = elements.toNull()
        )

        is CobolFIRTree.DataTree.WorkingStorage.Sql -> this
        else -> notPossible()
    } as T
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy