Configure array/object indentation for YAML in Jackson - kotlin

I'm trying to generate a YAML file from an input Map I'm using Jackson and the YamlFactory utility provided by Jackson to do so. I'm attempting to configure the indentation property for the YAML output, but it doesn't seem like that's being respected at all.
Here's how my code looks like:
fun getSdkResultAsGenericObject(sdkResult: Any?): Any? {
if (sdkResult == null) {
return null
}
var genericObj: Any?
val stringified = genericSdkObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(sdkResult)
.replace("\n", "")
val isArray = stringified.startsWith("[")
genericObj = if (isArray) {
genericSdkObjectMapper.readValue(stringified, List::class.java)
} else {
genericSdkObjectMapper.readValue(stringified, LinkedHashMap::class.java)
}
val defaultYaml = resultYamlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(genericObj )
}
The declaration of the resultYamlMapper is like this:
val yamlFactory = YAMLFactory()
.configure(YAMLGenerator.Feature.SPLIT_LINES, false)
.configure(YAMLGenerator.Feature.INDENT_ARRAYS, true)
val resultYamlMapper = ObjectMapper(YamlFactory())
The documentation says that the INDENT_ARRAYS feature uses 2 spaces by default. I'm trying to understand how I can configure that? I need 4 spaces in the resultant YAML. I tried setting a pretty print writer:
val yamlFactory = YAMLFactory()
.configure(YAMLGenerator.Feature.SPLIT_LINES, false)
.configure(YAMLGenerator.Feature.INDENT_ARRAYS, true)
val resultYamlMapper = ObjectMapper(YamlFactory())
val arrayIndenter = DefaultIndenter(" ", DefaultIndenter.SYS_LF)
val objectIndenter = DefaultIndenter(" ", DefaultIndenter.SYS_LF)
resultYamlMapper.setDefaultPrettyPrinter(DefaultPrettyPrinter().withObjectIndenter(objectIndenter).withArrayIndenter(arrayIndenter))
But this doesn't seem to be respected at all. Any thoughts? Or does Jackson not let you configure the indentation at all?

The docs show that the PrettyPrinter interface is only for the JsonGenerator.
If you want to customize your YAML output, you have to use the SnakeYAML API directly (which is used by Jackson for YAML processing). SnakeYAML has similar features to Jackson and there is little reason to use Jackson if you only want to process YAML. Most importantly, it lets you configure YAML formatting.

Related

Kotlin Annotation processor doesn't add import for generated files

I have an Annotation-processor, which should generate a class MyGeneratedClass containing a variable of another class MyEntity.
My code inside the processfunction:
val elementsWithAnnotation = roundEnv.getElementsAnnotatedWith(MyClass::class.java)
if (elementsWithAnnotation.isEmpty()) {
return true
}
val fileName = "MyGeneratedClass"
val packageName = "me.myname.sdk.generated"
val classBuilder = TypeSpec.classBuilder(fileName)
for (element in elementsWithAnnotation) {
val ann = element.getAnnotation(MyClass::class.java)
println("package: "+ ann.javaClass.packageName)
val variableBuilder =
PropertySpec.varBuilder(
name = element.simpleName.toString(),
type = ClassName("", element.asType().asTypeName().asNullable().toString()),
).initializer("null")
classBuilder
.addProperty(variableBuilder.build())
}
val file = FileSpec.builder(packageName, fileName)
.addType(classBuilder.build())
.build()
val generatedDirectory = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME]
file.writeTo(File(generatedDirectory, "$fileName.kt"))
return true
But the generated code misses the import MyEntity
package me.myname.sdk.generated
class MyGeneratedClass {
var MyEntity: MyEntity? = null
}
When looking inside the generated file, IntelliJ suggests me to import MyEntity, which resolves the error. But how can I achieve, that the import MyEntity statement is being added when generating the file?
looking at the kotlinpoet documentation https://square.github.io/kotlinpoet/1.x/kotlinpoet/kotlinpoet/com.squareup.kotlinpoet/-class-name/index.html
seems like the first argument in your code, which is a empty string is the package name you are missing in the generated code.
in my experience kotlinpoet is much happier to generate code that in in packages. it sometimes does silly things with types in the root/default package.

Micronaut declarative client with base url per environment

I'd like to be able to use Micronaut's declarative client to hit an a different endpoint based on whether I'm in a local development environment vs a production environment.
I'm setting my client's base uri in application.dev.yml:
myserviceclient:
baseUri: http://localhost:1080/endpoint
Reading the docs from Micronaut, they have the developer jumping through quite a few hoops to get a dynamic value piped into the actual client. They're actually quite confusing. So I've created a configuration like this:
#ConfigurationProperties(PREFIX)
class MyServiceClientConfig {
companion object {
const val PREFIX = "myserviceclient"
const val BASE_URL = "http://localhost:1080/endpoint"
}
var baseUri: String? = null
fun toMap(): MutableMap<String, Any> {
val m = HashMap<String, Any>()
if (baseUri != null) {
m["baseUri"] = baseUri!!
}
return m
}
}
But as you can see, that's not actually reading any values from application.yml, it's simply setting a const value as a static on the class. I'd like that BASE_URL value to be dynamic based on which environment I'm in.
To use this class, I've created a declarative client like this:
#Client(MyServiceClientConfig.BASE_URL)
interface MyServiceClient {
#Post("/user/kfc")
#Produces("application/json")
fun sendUserKfc(transactionDto: TransactionDto)
}
The docs show an example where they're interpolating values from the config map that's built like this:
#Get("/api/\${bintray.apiversion}/repos/\${bintray.organization}/\${bintray.repository}/packages")
But how would I make this work in the #Client() annotation?
Nowhere in that example do they show how bintray is getting defined/injected/etc. This appears to be the same syntax that's used with the #Value() annotation. I've tried using that as well, but every value I try to use ends up being null.
This is very frustrating, but I'm sure I'm missing a key piece that will make this all work.
I'm setting my client's base uri in application.dev.yml
You probably want application-dev.yml.
But how would I make this work in the #Client() annotation?
You can put a config key in the #Client value using something like #Client("${myserviceclient.baseUri}").
If you want the url somewhere in your code use this:
#Value("${micronaut.http.services.occupancy.urls}")
private String occupancyUrl;

HOCON config file dynamic substitution

I'm using HOCON to configure log messages and I'm looking for a way to substitute placeholder values dynamically.
I know that ${?PLACEHOLDER} will read an environment variable and returns an empty string when the PLACEHOLDER environment variable doesn't exist.
Example
This is an example of what I had in mind:
(I'm using config4k to load HOCON )
data class LogMessage(val message: String, val code: String)
fun getMessage(key: String, placeholderValues: Array<String> = arrayOf()): LogMessage {
val config = ConfigFactory.parseString("MY_LOG_MESSAGE {code = ABC-123456, message = My log message with dynamic value %0% and another dynamic value %1% }")
val messageObject = config.extract<LogMessage>(key)
var message = messageObject.message
placeholderValues.forEachIndexed { placeholderIndex, value ->
message = message.replace("%$placeholderIndex%", value)
}
return messageObject.copy(message = message)
}
fun main(args: Array<String>) {
println(getMessage("MY_LOG_MESSAGE", arrayOf("value 1", "value 2")))
// prints: LogMessage(message=My log message with dynamic value value 1 and another dynamic value value 2, code=ABC-123456)
}
Even though this works, it doesn't look like the best solution and I assume there already is a solution for this.
Could someone tell me if there is a built-in solution?
First things first.
HOCON is just a glorified JSON format.
config4k is just a wrapper.
All your work is being done by Typesafe Config, as you've probably noticed.
And judging by their documentation and code, they support placeholders only from withing the file, or from the environment:
This library limits itself to config files. If you want to load config
from a database or something, you would need to write some custom
code.
But for what you're doing, simple String.format() should be enough:
fun interpolate(message: String, vararg args: Any) = String.format(message, *args)
println(interpolate("My message was %s %s %s %s", "a", 1, 3.32, true))
// My message was a 1 3.32 true
Notice that you can use * to destructure your array.

Kotlin enviromental values

I'm learning kotlin, I stumbled across an open-source repo, cloned it and ran on my computer, it is a update tool to update an existing jar, although looking at their code how do they approached to that, they did something like this.
private fun updateJar(frame: LaunchFrame, project: Project) {
val sourcePath = project.jarSourcePath
val jarPath = project.jarPath
val repoName = project.repoName
frame.log("Reading source URL from '$sourcePath'")
val sourceUrl = readTextFile(sourcePath)?.let { URI(it) }
frame.log("Source URL: $sourceUrl")
frame.log("Connecting to GitHub")
val github = GitHub.connectAnonymously()
frame.log("Using repository '$repoName'")
val repo = github.getRepository(repoName)
frame.log("Finding latest release")
val latestRelease = repo.listReleases().first()
val assets = latestRelease.assets
check(assets.size == 1) { "Release must only have one asset" }
val asset = assets.first()
val downloadUrl = URI(asset.browserDownloadUrl)
frame.log("Latest URL: $downloadUrl")
if (sourceUrl == null || sourceUrl != downloadUrl || !verifyJar(jarPath)) {
frame.log("Downloading '$downloadUrl' to '$jarPath'")
downloadFile(downloadUrl, jarPath)
frame.log("Writing '$downloadUrl' to '$sourcePath'")
writeTextFile(sourcePath, downloadUrl.toString())
} else {
frame.log("'$jarPath' is up to date")
}
}
Which looks pretty much straight-forward, but here's the catch, there are no actual URL's for the $sourceUrl and others, can someone shed some light to this question? It starts downloading the repo which is runestar/client but there are no actual links for the exact repo what it's trying to download, so how did they do that?
The values are being extracted using some logic, scattered around various places in the code.
For example, see https://github.com/RuneStar/launcher/blob/3e8dcb59c32d2818c917705c6c4432f9fc12c449/src/main/java/org/runestar/launcher/RuneStar.kt#L15
override val repoName: String get() = "RuneStar/client"
It's not environment variables, and it's not downloading from a URL, it's writing to one on the local filesystem.
The Project interface sets up the file locations and the RuneStar class sets up a specfic Github repository.

How to show IntelliJ JSON editor in dialog?

I want to show below kind of an editor inside a dialog in an plugin that I'm developing for Kotlin and Java. I tried the below code snippet
editorTextField.setEnabled(true);
editorTextField.setOneLineMode(false);
editorTextField.setFileType(new JsonFileType());
Could someone point out how to achieve this?
Particularly I need the line numbers, JSON syntax highlighting and code folding I can see all the code specifications here. Please help me in learning how can I use them in my plugins.
JSON editor :
class JsonOutputDialog(language: Language, project: Project, text: String) : DialogWrapper(project) {
private val panel = JPanel(BorderLayout())
init {
super.setOKActionEnabled(false)
init()
val editorTextField = CustomEditorField(language, project, text)
editorTextField.setOneLineMode(false)
editorTextField.preferredSize = Dimension(800, 600)
editorTextField.isVisible = true
panel.add(editorTextField)
editorTextField.setCaretPosition(0)
}
override fun createCenterPanel() = panel
}
class CustomEditorField(language: Language, project: Project, s: String) : LanguageTextField(language, project, s) {
override fun createEditor(): EditorEx {
val editor = super.createEditor()
editor.setVerticalScrollbarVisible(true)
editor.setHorizontalScrollbarVisible(true)
val settings = editor.settings
settings.isLineNumbersShown = true
settings.isAutoCodeFoldingEnabled = true
settings.isFoldingOutlineShown = true
settings.isAllowSingleLogicalLineFolding = true
settings.isRightMarginShown=true
return editor
}
}
This is what you need to do. The key here is to use LanguageTextField instead of EditorTextField and override the createEditor() method to configure all the options that you are looking for like Line Numbers and Code Folding.