This article is from GW Advent Calendar "Calendar to Challenge Something New" / 67) 5/6 article.
When it comes to formulating APIs and creating SDKs, the question of which language to support comes up. I want to minimize the effort to create the SDK, so if possible, I would like to create it in one language and automatically convert it to another language.
Can be converted automatically! !! Although it doesn't go as far as that, I tried to put together a library that seems to be quite regrettable.
JavaScript -> TypeScript
There is a story that 60% of Javascript people have also used TypeScript, but the old library is still JavaScript, isn't it? If you can't migrate because of the migration cost, it may be convenient if you can use automatic conversion.
For the time being, this library was convenient for converting from JavaScript to TypeScript.
https://github.com/gregjacobs/js-to-ts-converter
I'm disassembling the JavaScript AST and rebuilding TypeScript. It's the opposite of TypeScript compilation. It is written that the type is guessed from the surroundings, but it is almost useless and all are any. But it seems to move for the time being! It will convert up to that point. (* It doesn't work. There were some errors)
Once you get used to AST, it's not that difficult, so it's easy to do something like Partially customize according to your project.
JavaScript <-> Python
JavaScript and TypeScript are the same language, right? You may hear that, but JavaScript and Python are completely different languages.
Jiphy looked good for this
https://github.com/timothycrosley/jiphy
I can't read the contents, but I think this is also probably building and converting AST. It is also a high point that there is a list of syntax that can be converted.
Any program at the level listed in this list will work fine, but the sad thing is that it doesn't support class.
Jiphy does not implement stdlib components, classes, etc. It's SYNTAX ONLY.
Since the class does not support it, you will need to use it in a really small program or fix the class later.
TypeScript -> Java, Python, C#
It is amazing that it supports various languages jsii https://github.com/aws/jsii
Until now, the conversion was just the conversion of the program contents, but this one has a slightly different mechanism. TypeScript (or node.js) always runs in the background, and its interface is converted to Java, Python, C #.
I thought I was calling a Python function, but it was passed through to the node.js function running behind the scenes! It's like that.
Since there is only one language running in the background, the bug encounter rate seems to be low.
It's a fairly epoch-making mechanism, and I'm sorry to say that It also has the disadvantages of consuming a lot of memory and limiting the types that can be used for interface arguments (because it is dynamically translated into languages).
Also, the biggest problem is that if you don't create a project exclusively for this, it will be difficult to deal with it later. You have to create a program according to the language specifications set by jsii. For example
--It is necessary to write a program that takes on all the restrictions of each language.
(Even if you want only python, you are subject to java restrictions and c # restrictions)
--The naming convention is fixed. Example: getXXX () is prohibited. Switch is a reserved word in C #, so use is prohibited.
--Callbacks and TypeScript-specific writing are prohibited.
--default export
, ʻexport =` is prohibited (probably because it will not be named)
etc. But it's a pretty good library if it works from the beginning.
I tried 3 language conversion libraries. All are regrettable! I've come to the place, so I'm hoping that it will be complete in a year or two.
Recommended Posts