[File] [Json] Support writer

This commit is contained in:
qianmoQ 2024-06-09 11:21:59 +08:00
parent e8ee704b9e
commit ca1026aa07
3 changed files with 87 additions and 7 deletions

1
.gitignore vendored
View File

@ -63,3 +63,4 @@ parser/datacap-parser-trino/src/main/java/io/edurt/datacap/parser/antlr
# Skip file #
file/datacap-file-txt/*.txt
file/datacap-file-json/*.json

View File

@ -1,18 +1,53 @@
package io.edurt.datacap.file.json
import com.fasterxml.jackson.core.JsonEncoding
import com.fasterxml.jackson.core.JsonFactory
import io.edurt.datacap.common.utils.DateUtils
import io.edurt.datacap.file.File
import io.edurt.datacap.file.FileConvert.formatFile
import io.edurt.datacap.file.model.FileRequest
import io.edurt.datacap.file.model.FileResponse
import org.slf4j.LoggerFactory.getLogger
class JsonFile : File
{
override fun writer(request: FileRequest): FileResponse
{
TODO("Not yet implemented")
class JsonFile : File {
private val log = getLogger(this::class.java)
override fun writer(request: FileRequest): FileResponse {
val response = FileResponse()
try {
log.info("${name()} writer origin path [ ${request.path} ]")
log.info("${name()} writer start time [ ${DateUtils.now()} ]")
val file = formatFile(request, name())
log.info("${name()} writer file absolute path [ ${file.absolutePath} ]")
val factory = JsonFactory()
factory.createGenerator(file, JsonEncoding.UTF8)
.use { generator ->
generator.writeStartArray()
request.columns
.forEach { column ->
generator.writeStartObject()
for (headerIndex in request.headers.indices) {
when (column) {
is List<*> -> generator.writeObjectField(request.headers[headerIndex] as String, column[headerIndex])
else -> generator.writeObjectField(request.headers[headerIndex] as String, column.toString())
}
}
generator.writeEndObject()
}
generator.writeEndArray()
}
log.info("${name()} writer end time [ ${DateUtils.now()} ]")
response.successful = true
} catch (e: Exception) {
response.successful = false
response.message = e.message
}
return response
}
override fun reader(request: FileRequest): FileResponse
{
override fun reader(request: FileRequest): FileResponse {
TODO("Not yet implemented")
}
}

View File

@ -0,0 +1,44 @@
package io.edurt.datacap.file.json
import com.google.inject.Guice.createInjector
import com.google.inject.Injector
import io.edurt.datacap.file.FileFilter
import io.edurt.datacap.file.FileManager
import io.edurt.datacap.file.model.FileRequest
import org.junit.Before
import org.junit.Test
import org.slf4j.LoggerFactory.getLogger
import kotlin.test.assertTrue
class JsonFileTest {
private val log = getLogger(this::class.java)
private val name = "Json"
private var injector: Injector? = null
private val request: FileRequest = FileRequest()
@Before
fun before() {
injector = createInjector(FileManager())
request.name = "test"
request.path = System.getProperty("user.dir")
request.headers = listOf("name", "age")
val l1 = listOf("Test", 12)
val l2 = listOf("Test1", 121)
request.columns = listOf(l1, l2)
}
@Test
fun testWriter() {
injector?.let { injector ->
FileFilter.findNotify(injector, name)
.ifPresent { file ->
assertTrue {
file.writer(request)
.successful == true
}
}
}
}
}