-
Notifications
You must be signed in to change notification settings - Fork 44
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
Generate typescript definitions from content model #241
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Mapping library | ||
|
||
This library is responsible for mapping source ingestion message content strings to a payload structure | ||
that can be sent via network APIs. | ||
|
||
## Motivation | ||
|
||
We have learned that the safest way of handling ingestion message data from `n` sources with up to `m` | ||
content schemata each is to keep the data "as is" and map the content to a usable schema dynamically. | ||
|
||
This gives us the ability to address mapping bugs by fixing code (cheap) rather than fixing | ||
streaming data (expensive). | ||
|
||
## Typescript definitions | ||
|
||
To allow web clients an easy way of mapping this payload structure, we provide automatically generated Typescript | ||
definitions using [this Typescript generator](https://github.com/vojtechhabarta/typescript-generator). | ||
|
||
The following command puts the generated definitions in `frontend/components/src/content.ts` and has to be run | ||
after every change to the content model in `backend/lib/mapping/src/main/java/co/airy/mapping/model/`: | ||
|
||
```shell script | ||
bazel run //backend/lib/mapping:ts-generator | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package co.airy.ts_generator; | ||
|
||
import cz.habarta.typescript.generator.Input; | ||
import cz.habarta.typescript.generator.JsonLibrary; | ||
import cz.habarta.typescript.generator.Output; | ||
import cz.habarta.typescript.generator.Settings; | ||
import cz.habarta.typescript.generator.TypeScriptFileType; | ||
import cz.habarta.typescript.generator.TypeScriptGenerator; | ||
import cz.habarta.typescript.generator.TypeScriptOutputKind; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
final Settings settings = new Settings(); | ||
|
||
settings.outputKind = TypeScriptOutputKind.module; // Needed to generate `export` statements | ||
settings.outputFileType = TypeScriptFileType.implementationFile; | ||
settings.jsonLibrary = JsonLibrary.jackson2; | ||
settings.setExcludeFilter(List.of("java.io.Serializable"), List.of("**.**Builder")); | ||
|
||
final TypeScriptGenerator generator = new TypeScriptGenerator(settings); | ||
|
||
final Input.Parameters parameters = new Input.Parameters(); | ||
|
||
parameters.debug = false; | ||
parameters.classNamePatterns = List.of("co.airy.mapping.model.**"); | ||
|
||
final File output = new File(System.getenv().get("BUILD_WORKSPACE_DIRECTORY") + "/frontend/components/src/content.ts"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the same spirit, everything here is hardcoded on purpose. |
||
settings.validateFileName(output); | ||
|
||
generator.generateTypeScript(Input.from(parameters), Output.to(output)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
// Generated using typescript-generator version 2.26.723 on 2020-11-06 17:30:31. | ||
|
||
export interface Content { | ||
type: "text"; | ||
} | ||
|
||
export interface Text extends Content { | ||
type: "text"; | ||
text: string; | ||
} | ||
|
||
export type ContentUnion = Text; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this, we can build a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucapette and @paulodiniz as long as we don't want to create ts definitions for anything else (which I think makes sense, because this problem is so unique), I would keep the generator as a binary that is local to this package.