Skip to content

Commit

Permalink
json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxcapades committed Sep 14, 2024
1 parent 096da5d commit bff9a73
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "org.veupathdb.lib"
version = "0.4.0"
version = "0.5.0"

buildscript {
dependencies {
Expand Down
36 changes: 35 additions & 1 deletion src/main/kotlin/org/veupathdb/lib/cli/diamond/Diamond.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.veupathdb.lib.cli.diamond.commands.BlastX
import org.veupathdb.lib.cli.diamond.commands.impl.BlastNImpl
import org.veupathdb.lib.cli.diamond.commands.impl.BlastPImpl
import org.veupathdb.lib.cli.diamond.commands.impl.BlastXImpl
import org.veupathdb.lib.cli.diamond.opts.output_format.cacheFormatFields
import org.veupathdb.lib.cli.diamond.opts.fields.output_format.cacheFormatFields
import org.veupathdb.lib.jackson.Json
import java.io.InputStream

Expand All @@ -39,6 +39,18 @@ object Diamond {
@DiamondExtras
fun blastN(): BlastN = BlastNImpl()

/**
* Creates a new, blank [BlastN] CLI configuration instance and passes it to
* the given action before returning it.
*
* @param action Action which may be used to mutate the new `BlastN` instance.
*
* @return The new `BlastN` instance.
*/
@JvmStatic
@DiamondExtras
inline fun blastN(action: BlastN.() -> Unit): BlastN = blastN().apply(action)

/**
* Attempts to parse the given JSON value as a [BlastN] CLI configuration.
*
Expand Down Expand Up @@ -96,6 +108,17 @@ object Diamond {
@JvmStatic
fun blastP(): BlastP = BlastPImpl()

/**
* Creates a new, blank [BlastP] CLI configuration instance and passes it to
* the given action before returning it.
*
* @param action Action which may be used to mutate the new `BlastP` instance.
*
* @return The new `BlastP` instance.
*/
@JvmStatic
inline fun blastP(action: BlastP.() -> Unit) = blastP().apply(action)

/**
* Attempts to parse the given JSON value as a [BlastP] CLI configuration.
*
Expand Down Expand Up @@ -150,6 +173,17 @@ object Diamond {
@JvmStatic
fun blastX(): BlastX = BlastXImpl()

/**
* Creates a new, blank [BlastX] CLI configuration instance and passes it to
* the given action before returning it.
*
* @param action Action which may be used to mutate the new `BlastX` instance.
*
* @return The new `BlastX` instance.
*/
@JvmStatic
inline fun blastX(action: BlastX.() -> Unit) = blastX().apply(action)

/**
* Attempts to parse the given JSON value as a [BlastX] CLI configuration.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.veupathdb.lib.cli.diamond.commands

import com.fasterxml.jackson.annotation.JsonGetter
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import org.veupathdb.lib.cli.diamond.DiamondCommand
import org.veupathdb.lib.cli.diamond.util.DiamondCommandConfigDeserializer

@JsonDeserialize(using = DiamondCommandConfigDeserializer::class)
sealed interface DiamondCommandConfig {
/**
* Diamond subcommand.
*/
@get:JsonGetter("tool")
val tool: DiamondCommand
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.veupathdb.lib.cli.diamond.opts
import com.fasterxml.jackson.annotation.JsonGetter
import com.fasterxml.jackson.annotation.JsonSetter
import io.foxcapades.lib.cli.builder.flag.CliFlag
import org.veupathdb.lib.cli.diamond.opts.output_format.OutputFormatOptions
import org.veupathdb.lib.cli.diamond.opts.fields.output_format.OutputFormatOptions

interface OutputFormatOptionContainer {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

class BlastTabFormat(override var formatFields: Iterable<FormatField> = emptyList())
: BlastTabFormatBase<Iterable<FormatField>>(OutputFormat.BlastTab)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import com.fasterxml.jackson.annotation.JsonValue
import org.veupathdb.lib.jackson.Json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

private var formatFieldCache: Map<String, FormatField>? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

class JsonFlatFormat(override var formatFields: Iterable<FormatField> = emptyList())
: BlastTabFormatBase<Iterable<FormatField>>(OutputFormat.BlastTab)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import io.foxcapades.lib.cli.builder.arg.Argument
import io.foxcapades.lib.cli.builder.arg.format.ArgumentFormatter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ArrayNode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import com.fasterxml.jackson.databind.annotation.JsonDeserialize

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.veupathdb.lib.cli.diamond.opts.output_format
package org.veupathdb.lib.cli.diamond.opts.fields.output_format

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package org.veupathdb.lib.cli.diamond.opts.impl
import io.foxcapades.lib.cli.builder.flag.flag
import io.foxcapades.lib.cli.builder.flag.toggleFlag
import org.veupathdb.lib.cli.diamond.opts.OutputFormatOptionContainer
import org.veupathdb.lib.cli.diamond.opts.output_format.OutFormatFormatter
import org.veupathdb.lib.cli.diamond.opts.output_format.OutputFormatOptions
import org.veupathdb.lib.cli.diamond.opts.fields.output_format.OutFormatFormatter
import org.veupathdb.lib.cli.diamond.opts.fields.output_format.OutputFormatOptions

internal class OutputFormatOptionContainerImpl : OutputFormatOptionContainer {
override var outputFormat by flag<OutputFormatOptions> { formatter = OutFormatFormatter }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.veupathdb.lib.cli.diamond.util

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.module.kotlin.contains
import org.veupathdb.lib.cli.diamond.DiamondCommand
import org.veupathdb.lib.cli.diamond.commands.DiamondCommandConfig
import org.veupathdb.lib.cli.diamond.commands.impl.BlastPImpl
import org.veupathdb.lib.cli.diamond.commands.impl.BlastXImpl
import org.veupathdb.lib.jackson.Json

internal class DiamondCommandConfigDeserializer : JsonDeserializer<DiamondCommandConfig>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): DiamondCommandConfig {
val obj = p.readValueAs(ObjectNode::class.java)

if ("tool" !in obj)
throw JsonMappingException(
p,
"cannot deserialize given JSON into a DIAMOND command configuration; the given object has no \"tool\" property"
)

return when (DiamondCommand.fromJson(obj["tool"])) {
DiamondCommand.BlastP -> Json.parse<BlastPImpl>(obj)
DiamondCommand.BlastX -> Json.parse<BlastXImpl>(obj)
else -> TODO("unsupported type")
}
}
}

0 comments on commit bff9a73

Please sign in to comment.