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

Generate typescript definitions from content model #241

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ maven_install(
"com.fasterxml.jackson.core:jackson-databind:2.10.0",
"com.fasterxml.jackson.module:jackson-module-afterburner:2.10.0",
"com.jayway.jsonpath:json-path:2.4.0",
"cz.habarta.typescript-generator:typescript-generator-core:2.26.723",
"io.confluent:kafka-avro-serializer:5.5.1",
"io.confluent:kafka-schema-registry-client:5.5.1",
"io.confluent:kafka-schema-registry:5.5.1",
Expand Down
12 changes: 11 additions & 1 deletion backend/lib/mapping/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_java//java:defs.bzl", "java_library")
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
load("//tools/build:junit5.bzl", "junit5")

lib_deps = [
Expand All @@ -19,6 +19,16 @@ java_library(
deps = lib_deps,
)

java_binary(
Copy link
Contributor Author

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.

name = "ts-generator",
srcs = glob(["src/ts-generator/**/*.java"]),
main_class = "co.airy.ts_generator.Main",
deps = [
":mapping",
"@maven//:cz_habarta_typescript_generator_typescript_generator_core",
],
)

[
junit5(
file = file,
Expand Down
24 changes: 24 additions & 0 deletions backend/lib/mapping/README.md
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
Expand Up @@ -4,8 +4,8 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
@JsonSubTypes({
@JsonSubTypes.Type(value = Text.class, name = "text")
)
})
public abstract class Content {
}
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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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));
}
}
12 changes: 9 additions & 3 deletions docs/docs/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ This is a [paginated](#pagination) endpoint.
"last_message": {
id: "{UUID}",
content: {
text: "{String}"
text: "{String}",
type: "text"
// Determines the schema of the content
},
// typed source message model
state: "{String}",
Expand Down Expand Up @@ -318,7 +320,9 @@ This is a [paginated](#pagination) endpoint and messages are sorted from oldest
{
id: "{UUID}",
content: {
text: "{String}"
text: "{String}",
type: "text"
// Determines the schema of the content
},
// typed source message model
state: "{String}",
Expand Down Expand Up @@ -361,7 +365,9 @@ Sends a message to a conversation and returns a payload.
{
id: "{UUID}",
content: {
text: "{String}"
text: "{String}",
type: "text"
// Determines the schema of the content
},
// typed source message model
state: "{String}",
Expand Down
4 changes: 3 additions & 1 deletion docs/docs/api/websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Incoming payloads notify connected clients that a message was created or updated
message: {
id: "{UUID}",
content: {
text: "{String}"
text: "{String}",
type: "text"
// Determines the schema of the content
},
// typed source message model
state: "{String}",
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ Identifies the participant that sent the message. Interpretation is based on the

- `channelId` uuid

- `content` string
- `content` string Immutable string version of the ingested content. APIs dynamically parse and map it to a schema
using the mapping library. [Read more]()

- `offset` long sequence number of message within a conversation

Expand Down
14 changes: 14 additions & 0 deletions frontend/components/src/content.ts
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, we can build a switch { case } function in the components lib that takes a Content payload and depending on the type field returns a react component.

3 changes: 3 additions & 0 deletions frontend/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ export * from "./components/cta";
export * from "./components/general";
export * from "./components/inputs";
export * from "./components/loaders";

// export the content model
export * from "./content";
Loading