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

Provide an easier way to set default TypeScript elements, so generated code can be customised more easily #23

Open
aSemy opened this issue Apr 9, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@aSemy
Copy link
Contributor

aSemy commented Apr 9, 2022

If a client already has some standard type aliases, then I want to re-use those type aliases in my generated code.

@Serializable
data class Position(
  val x: Double,
  val y: Double,
)
// provided by library
export type double = number

// I want generated code to use 'double'
export interface Position {
  x: double;
  y: double;
}

If I hack around a bit, I can come up with something that works. But it's not pretty, or intuitive

fun main() {
  val tsGenerator = KxsTsGenerator()

  val libraryDoubleTypeRef = TsTypeRef.Declaration(TsElementId("Double"), null, false)

  tsGenerator.descriptorElements += Double.serializer().descriptor to setOf(
    TsDeclaration.TsTypeAlias(
      TsElementId("Double"),
      TsTypeRef.Declaration(TsElementId("double"), null, false),
    )
  )

  tsGenerator.descriptorTypeRef += mapOf(
    Double.serializer().descriptor to libraryDoubleTypeRef
  )

  println(tsGenerator.generate(Position.serializer()))
}

@Serializable
data class Position(
  val x: Double,
  val y: Double,
)

This produces the correct output...

export interface Position {
  x: Double;
  y: Double;
}

export type Double = double;

Good luck figuring that out if you're new to KXS, and didn't happen to build this library...

Potential solutions

Users should be able to set overrides for SerialDescriptors, similar to the existing serializerDescriptors and descriptorElements in KxsTsGenerator. For example

val gen = KxsTsGenerator()

gen.addOverride(
  Double.serializer().descriptor to existingTypeAlias("double"),
)
@aSemy aSemy added the enhancement New feature or request label Apr 9, 2022
aSemy added a commit that referenced this issue Apr 11, 2022
…nd update knit to allow for TS Compile to be disabled
aSemy added a commit that referenced this issue Apr 13, 2022
* allow for overriding of elements (partial support for #23 and #22), and update knit to allow for TS Compile to be disabled

* make TsMapTypeConverter more clear, split inline/non-inline

* code tidy

* fix bug where overrides weren't consistently found and applied if the target was nullable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant