havardh / javaflow Goto Github PK
View Code? Open in Web Editor NEWJava model to flowtype converter
Java model to flowtype converter
This issue is motivated by the need to insert /* eslint-disable no-use-before-define */
.
This rule is violated as the types are sorted in the output.
The enum type is printed with an additional trailing newline.
Two consecutive types should be separated by a single empty newline.
Migrate circleci to 2.0 version using this guide
Let's say we have the following java types:
public abstract class BaseDto {
private int id;
public int getId() {
return id;
}
}
public class UserDto extends BaseDto {
private String name;
public String getName() {
return name;
}
}
then javaflow
will correctly generate
export UserDto = {
id: number,
name: string
};
However, when BaseDto
is not from the same project, but actually only included via Jar file, javaflow
will not be able to parse BaseDto (source code is not available) and the resulting type UserDto will be missing the id
field.
#60 filters out static fields by default.
Lets add --includeStaticFields
as a command line flag to make javaflow
include static fields.
The following model is not handled correctly:
public class ModelWithTypeParams {
class Child<T> {
private T field;
public T getField() {
return field;
}
}
private Child<String> child1;
private Child<Integer> child2;
public Child<String> getChild1() {
return child1;
}
public Child<Integer> getChild2() {
return child2;
}
}
as I can see, flow supports generics so I think it might be enough if we "simply" generate this:
export type Child<T> = {
field: T
}
export type ModelWithTypeParams = {
child1: Child<string>,
child2: Child<number>
}
But maybe I'm missing something obvious here?
For example:
package domain.document;
import domain.person.Person;
public class Document {
private long id;
private Person person;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
package domain.person;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import domain.document.Document;
import java.util.Set;
public class Person {
private long id;
@JsonProperty("documents")
private Set<Document> documentSet;
@JsonIgnore
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set<Document> getDocumentSet() {
return documentSet;
}
public void setDocumentSet(Set<Document> documentSet) {
this.documentSet = documentSet;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Result:
javaflow domain/
/* eslint-disable no-use-before-define */
/* Generated by javaflow 1.5.0 */
export type Document = {
id: number,
person: Person,
};
export type Person = {
id: number,
documentSet: Array<Document>,
password: string,
};
But want:
id: number,
person: Person,
};
export type Person = {
id: number,
documents: Array<Document>,
};
Need some option for mark field as nullable. And after javaflow get something like this:
export type Document = {
id: number,
person: Person,
};
export type Person = {
id: number,
documentSet: Array<Document>,
someNullableField: ?string,
};
jpomykala@Jakub-MBP:assortment/shortages ‹dev*›$ javaflow RefillFilterParams.java > param.js
Error: Could not find or load main class com.github.havardh.javaflow.JavaFlow
Caused by: java.lang.ClassNotFoundException: com.github.havardh.javaflow.JavaFlow
Build command: gradle assemble
Version: 1.4.2 (same on 1.5.2-SNAPSHOT)
import java.time.LocalDate;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class RefillFilterParams {
private LocalDate startDate;
private LocalDate endDate;
}
Version: javaflow -version
/* @flow /
/ eslint-disable no-use-before-define /
/ Generated by javaflow 1.5.0 */
Example:
package domain.document;
import domain.person.*;
public class Document {
private long id;
private Person person;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
package domain.person;
import domain.document.*;
import java.util.Set;
public class Person {
private long id;
private Set<Document> documentSet;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set<Document> getDocumentSet() {
return documentSet;
}
public void setDocumentSet(Set<Document> documentSet) {
this.documentSet = documentSet;
}
}
Command for execute: javaflow domain/
Result:
Exception in thread "main" com.github.havardh.javaflow.exceptions.AggregatedException: Verification failed:
Could not find types: {domain.document.Document=[person: domain.document.Person]}
at com.github.havardh.javaflow.Execution.verify(Execution.java:119)
at java.util.stream.ReferencePipeline$11$1.accept(ReferencePipeline.java:372)
at java.util.stream.ReferencePipeline$11$1.accept(ReferencePipeline.java:373)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:419)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.github.havardh.javaflow.Execution.run(Execution.java:98)
at com.github.havardh.javaflow.JavaFlow.main(JavaFlow.java:71)
Currently, the main focus for generated fields in types is the Java field. But consider the following Java class:
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getName() {
return firstName + " " + lastName;
}
}
then usually (at least when using Jackson API), instances of that class will result in JSON like this:
{
"firstName": "John",
"lastName": "Smith",
"name": "John Smith"
}
However, as there is no field name
in class User
, the resulting type will not include the name field:
export User = {
firstName: string,
lastName: string
};
I noticed that during type generation, static fields are not excluded, so for example this Java class:
public class SomeClass {
private static final long serialVersionUID = 3210616069586284583L;
public static final String CONSTANT = "some constant";
private String someField;
}
currently results in this type:
export type SomeClass = {
serialVersionUID: number,
SOME_CONSTANT: string,
someField: string
};
whereas I would expect it to result in this type:
export type SomeClass = {
someField: string
};
WDYT?
When field type contains generic, the generic type is not transformed to the corresponding type.
For example, expected transformation of Map<String, List<Integer>> field;
is {[key: string]: Array<number>
Actual result is wrong {[key: string]: List<Integer>}
Created also a failing test for it in this pull request: #47
I am not sure how to solve it. Probably we will need to use some recursion and extract type from the string literal in TypeFactory.
Javaflow generates flow types based on the naming of the field. Json libraries like the one in Jackson generates the json payload based on getters.
We should either change javaflow to look at the getter names or keep it simple and throw an error/warning when inconsistencies are found.
Sort types before writing them to flow types.
This will make the output more identical between runs.
Currently the order is based on the input order.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.