https://kotlinlang.org logo
#scripting
Title
# scripting
e

Eugen Martynov

03/05/2023, 2:37 PM
Can I use kotlin serialization in kotlin script?
e

Eugen Martynov

03/05/2023, 2:40 PM
Thank you!
m

mbonnin

03/05/2023, 2:40 PM
Most of the times, I use
kotlinx-serialization-json
without the compiler plugin. It's not as type safe but for scripts, it's often "good enough"
👌 2
e

Eugen Martynov

03/05/2023, 2:41 PM
I need xml
m

mbonnin

03/05/2023, 2:41 PM
You can do stuff like
Copy code
"{}".parseToJsonElement()
I'm assuming XML would have the same primitives
Gives you some
XMLNode
item
e

Eugen Martynov

03/05/2023, 2:42 PM
Let me try
m

mbonnin

03/05/2023, 2:42 PM
Then you can cast/get children as needed
e

ephemient

03/05/2023, 2:42 PM
not much of a reason to use kxs over java.xml if you're not going to use the generated serializers though
m

mbonnin

03/05/2023, 2:42 PM
true
e

Eugen Martynov

03/05/2023, 2:42 PM
Yeah, that is my next option is just take java library
m

mbonnin

03/05/2023, 2:43 PM
one reason is that it's only one maven coordinates to remember 😄
e

ephemient

03/05/2023, 2:44 PM
java.xml module is part of the JDK, no maven coordinates at all 😛
👍 1
m

mbonnin

03/05/2023, 2:44 PM
Ahaha even better !
Requires Java 9 😱
e

ephemient

03/05/2023, 2:45 PM
oh it was there pre-modularization, just not as a module
m

mbonnin

03/05/2023, 2:45 PM
👍
For some reason I developped some PTSD whenever I see
javax.
😂 1
But that's fine, I'm working on it!
e

ephemient

03/05/2023, 2:47 PM
I don't think there's any object mapping built in. I guess Jackson would work if you need, since that's all reflection-based and not compile-time
👍 1
m

mbonnin

03/05/2023, 2:57 PM
FWIW, I just remembered I wrote this: https://github.com/martinbonnin/xoxo It's very simple, not tested a lot (I think I'm the only user 😄 ) but feedbacks are welcome!
v

Vampire

03/05/2023, 3:25 PM
You could even use serialization with serializers fine in a Kotlin script. You just cannot define new serializable objects that have default serilalizers provided by the compiler plugin as you cannot right now apply the compiler plugin. But you could have serializable objects with custom serializers and you can use classes that were compiled with the compiler plugin.
e

Eugen Martynov

03/05/2023, 4:00 PM
Oke people, thank you. I’m just trying to cook a quick scrip to automate translations of some different yaml files with DeepL
The XML is needed to prevent DeepL from attempting to translate integers
I already spent around a weekend or more on this problem and looks like I have to give up.
m

mbonnin

03/05/2023, 4:03 PM
Put it back at to the end of the pet projects circular queue 😄
That is harder than I thought - I’m trying to translate every yaml scalar from a file, but some are html (so xml). The yaml structure differs per file in the different content folders, and on top DeepL has a limit on the request size.
i

ilya.chernikov

03/06/2023, 8:02 AM
@Vampire
You just cannot define new serializable objects that have default serilalizers provided by the compiler plugin as you cannot right now apply the compiler plugin.
I'm not sure I understand, what is missing. There are two tests in the Kotlin repo that run this script - https://github.com/JetBrains/kotlin/blob/1.8.20/libraries/tools/kotlin-main-kts-test/testData/hello-kotlinx-serialization.main.kts - which uses plugin to generate serializers. You can add the plugin either via CLI, if cli compiler is used, or via compiler configuration.
The ticket mentioned above probably cannot be assumed as solved, because there are still difficulties in obtaining and applying the plugin for the script scenario, but it seems incorrect to say that you cannot apply the plugin.
v

Vampire

03/06/2023, 11:07 AM
Yeah, you are right @ilya.chernikov. If you call
kotlinc
explicitly, or call the script from a bash script or similar, or call the script programmatically. But there is no way to make it work with calling
./hello-kotlinx-serialization.main.kts
. See the Ticket @ephemient linked above. Note: the shebang hack is not appropriate for general usage. You either require a non-standard environment variable or an absolute path, and more important using more than one argument in the shebang line must be avoided for general usage, it is undefined and not portable and will behave majority different on different systems.
e

ephemient

03/06/2023, 1:49 PM
you could probably pull off some sort of polyglot trick. for example,
Copy code
#!/bin/sh
//bin/true<<.
/*
.
# any shell script goes here
kotlin "$0" "$@"
exit $?
*/
// any Kotlin script goes here
println("Hello, world!")
where you can put an arbitrarily complex shell script in there that Kotlin will skip over
v

Vampire

03/06/2023, 3:37 PM
Oh, amazing. Ugly, but amazing. 😄
Except that I don't like the
.
as end of comment marker that confused me and that it does not work properly in Git Bash on Windows. 😄 There the
//bin/true
does not work and needs quite some time, probably because it searches for a computer called
bin
Ah, you can use three slashes, then it works on both
e

ephemient

03/06/2023, 4:04 PM
grrrr windows paths
Copy code
#!/bin/sh
""":""" /*
kotlin "$0" "$@"
exit $?
*/
println("Hello, world!")
this would also work, using shell built-ins
v

Vampire

03/06/2023, 4:05 PM
Yay, thought about how to do it with colon, but didn't think about that quote trick, "nice" 😄
Hm, no, it doesn't work with that
Doesn't compile, probably because of the
@file:DependsOn
that is now preceded by a String literal
e

ephemient

03/06/2023, 4:09 PM
oh I see, if you need annotations that is tricky
v

Vampire

03/06/2023, 4:09 PM
So it seems the "best" way might indeed be
Copy code
#!/bin/sh

///bin/true <<EOC
/*
EOC
kotlinc ...
exit $?
*/

...
Now the only thing left is that you need to have the path to the serialization plugin, or require an envrionment variable, or need to add some logic to download it or something similar. Could be so easy if the feature request would be implmented. 😄
e

ephemient

03/06/2023, 4:13 PM
"just" implement a maven resolver in shell and download the kotlin compiler and plugin 🤪
v

Vampire

03/06/2023, 4:13 PM
Yep, exactly. 😄
But still a nice trick to have an executable called
my-cool-tool
that is a main.kts script 🙂
Thx
p

Paul Woitaschek

03/24/2023, 6:51 PM
Did you try just throwing it at chatGPT and say sth like: translate this to XYZ?
e

Eugen Martynov

03/24/2023, 7:13 PM
Nice, it is second suggestion try to use ai for thisbtask
Promise to try soon
v

Vampire

03/24/2023, 7:49 PM
Well, chatGPT is a fancy toy and nothing more imho. Imo you can only ask it to do work you could also do yourself and know exactly what the result should look like to fix the non-sense it produces, but just are too lazy to write yourself from ground up. All tries to let chatGPT generate code so far that I've seen was producing more ore less non-sense. It produced totally wrong code or missed important parts it only added after you told it to, or used methods on APIs that do not exist and never did exist at all. Besides that its data base is older than 2 years which is really very long in software development terms.
But maybe for language translations it is better, idk 😄
e

ephemient

03/24/2023, 10:31 PM
it's prone to making stuff up, but so is DeepL
v

Vampire

03/25/2023, 1:43 AM
How is a translator prone to making things up?
e

ephemient

03/25/2023, 1:54 AM
I've seen it add context to translations that completely doesn't exist in the source
🧐 1
also https://medium.com/mlearning-ai/deepl-wont-translate-this-phrase-a8310fcb2520 shows it producing words that don't exist in the target
🧐 1
v

Vampire

03/25/2023, 2:00 AM
Ok, I guess I keep sticking with Google translate. :-D
e

ephemient

03/25/2023, 2:02 AM
Google Translate has failures too. the big thing with DeepL and ChatGPT is that their output tends to sound more fluent even when it's totally wrong
v

Vampire

03/25/2023, 4:05 AM
Yeah, which is a big problem. Even worse than people copying SO answers blindly.
p

Paul Woitaschek

03/25/2023, 6:01 AM
It's fine, you just need a human going over it in the end
v

Vampire

03/25/2023, 1:14 PM
That's what I said. You can only use it for things you could have done yourself, so that you can see where the non-sense is and fix it. So if you let the tool translate something from Arabic to Russian, you need someone speaking both languages to fix up the result, that could have done the work himself right away.
e

Eugen Martynov

03/25/2023, 1:47 PM
Bjorn, I can clean room floor with vacuum cleaner just by myself or I can finish the corners after vaccum cleaner robot. That are both possible ways to finish that task.
v

Vampire

03/25/2023, 2:05 PM
Yes, that's exactly what I said. If you could have done it yourself but are too lazy or whatever, you can let the tool do it and fix its non-sense. But if you don't have arms or are blind (not speaking Arabic or Russian), then you cannot fix the non-sense and should not depend on the result.
And please don't get "lazy" wrong, it is a positive "lazy". I'm lazy too, otherwise I would use a text editor, not an IDE for example, or wouldn't write scripts for repetitive work.
173 Views