Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum customization & export statements #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

MartinEden
Copy link

Hi,

I've added two new (small) features that I needed:

  • I wanted to be able to generate TS definitions like this:
export interface Foo {
    bar: int;
}

This is so I can use them independently, as opposed to as type definitions for other code. To this end I've added a new optional parameter addExportStatements. When true, export statements are added.

Secondly, I wanted to be able to customise enum output, so instead of getting ALLCAPS enums, as is the convention in Java/Kotlin, I could have lowercase enums to match my JSON API.

So I've added an enumTransformer parameter. It is a function that takes the enum class (KClass<*>) and the enum value and returns a string. For my task (lowercase enums) I didn't need per-class behaviour, nor is it needed in the default case. However, I thought that some users might want to format some enums differently than others, so I added that as a parameter.

If you're happy with the thrust of these changes, I will write unit tests for them as well, if you would like me too. I didn't want to write unit tests for them until I found out whether you were happy with this API/approach.

Thanks for writing a really useful library!

@ntrrgc
Copy link
Owner

ntrrgc commented Apr 20, 2017

Hi and thanks for your contribution.

I like the part about adding export: indeed it's useful when using modules instead of global definitions.

I also like being able to tweak the output of enum generation, but I think it would be better to add that functionality in ClassTransformer instead of adding a new parameter in TypeScriptGenerator:

For one, enums are classes too. Also, ClassTransformers are chainable and composable, so you could add several of these, or wrap them with FilteredClassTransformer. Furthermore, since it's an interface instead of an anonymous lambda type, it's trivial to add new transformation methods, so adding another one, for instance... one for modifying the enum constant list would be rather easy and clean.

Please add unit tests here: https://github.com/ntrrgc/ts-generator/blob/master/src/test/kotlin/me/ntrrgc/tsGenerator/tests/generatorTests.kt

It's quite simple: you just have to make a call that generates the code, plus the code that should be generated. assertGeneratedCode() is smart enough to decode the TypeScript code so that little changes like different property order don't make the test fail.

@MartinEden
Copy link
Author

Okay. When I get time (hopefully soon) I will:

  • Investigate using ClassTransformer for enum rewriting
  • Write unit tests

@hpoul
Copy link
Contributor

hpoul commented Apr 29, 2018

FWIW regarding the export thing, it's pretty easy to do that already without any change in the api.. I simply used:

TypeScriptGenerator(...).individualDefinitions.joinToString(System.lineSeparator()) { "export ${it}" }

It's not even all that hack'ish i think :) (For my part I also added // noinspection TsLint${System.lineSeparator()} :-) because I don't care about lint warnings in generated code.. so i think it's really good to have such transformation functionality, rather than boolean options :) )

@ntrrgc
Copy link
Owner

ntrrgc commented Aug 3, 2021

Can you separate the last commit about bintray shutdown into its own pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants