Skip to content

Commit

Permalink
add support for format (apache#25)
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Le Dem <julien@apache.org>
  • Loading branch information
julienledem committed Feb 27, 2021
1 parent 0eeef64 commit e09e9e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
6 changes: 3 additions & 3 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ repositories {
}

ext {
jackson_version = "2.11.3"
jackson_databind_version = "2.11.3"
jackson_version = "2.12.0"
}

configurations {
Expand All @@ -29,7 +28,8 @@ configurations {
dependencies {
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
codeGenerator project(':generator')
testImplementation 'junit:junit:4.13'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -63,11 +65,11 @@ public void generate(PrintWriter printWriter) throws IOException {

}

builder.addField(FieldSpec.builder(ClassName.get(String.class), "producer", PRIVATE, FINAL).build());
builder.addField(FieldSpec.builder(ClassName.get(URI.class), "producer", PRIVATE, FINAL).build());
builder.addMethod(MethodSpec.constructorBuilder()
.addModifiers(PUBLIC)
.addParameter(
ParameterSpec.builder(ClassName.get(String.class), "producer").build()
ParameterSpec.builder(ClassName.get(URI.class), "producer").build()
)
.addCode("this.producer = producer;\n")
.build());
Expand Down Expand Up @@ -115,7 +117,7 @@ private void generateTypes(TypeSpec.Builder builder) {
fieldNames.add(f.getName());
if (f.getName().equals("_schemaURL")) {
String schemaURL = baseURL + "#/definitions/" + type.getName();
constructor.addCode("this.$N = $S;\n", f.getName(), schemaURL);
constructor.addCode("this.$N = URI.create($S);\n", f.getName(), schemaURL);
} else {
constructor.addJavadoc("@param $N $N\n", f.getName(), f.getDescription() == null ? "the " + f.getName() : f.getDescription());
constructor.addParameter(
Expand Down Expand Up @@ -188,6 +190,16 @@ public static TypeName getTypeName(Type type) {
@Override
public TypeName visit(PrimitiveType primitiveType) {
if (primitiveType.getName().equals("string")) {
if (primitiveType.getFormat() != null) {
String format = primitiveType.getFormat();
if (format.equals("uri")) {
return ClassName.get(URI.class);
} else if (format.equals("date-time")) {
return ClassName.get(ZonedDateTime.class);
} else {
throw new RuntimeException("Unknown format: " + primitiveType.getFormat());
}
}
return ClassName.get(String.class);
}
throw new RuntimeException("Unknown primitive: " + primitiveType.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ interface Type {
}

static class PrimitiveType implements Type {
private String name;
private final String name;
private final String format;

public PrimitiveType(String name) {
public PrimitiveType(String name, String format) {
super();
this.name = name;
this.format = format;
}

@Override
Expand All @@ -72,6 +74,10 @@ public String toString() {
public String getName() {
return name;
}

public String getFormat() {
return format;
}
}

public static class Field {
Expand Down Expand Up @@ -313,8 +319,9 @@ private Type resolveType(Schema typeNode) {
return resolveObjectType(schema);
} else if (typeNode.has("type")) {
String fieldType = typeNode.get("type").asText();
String fieldFormat = typeNode.has("format") ? typeNode.get("format").asText() : null;
if (fieldType.equals("string")) {
return new PrimitiveType(fieldType);
return new PrimitiveType(fieldType, fieldFormat);
} else if (fieldType.equals("object")) {
return resolveObjectType(typeNode);
} else if (fieldType.equals("array")) {
Expand Down
15 changes: 12 additions & 3 deletions client/src/test/java/io/openlineage/client/OpenLineageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import static org.junit.Assert.assertEquals;

import java.net.URI;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import io.openlineage.client.OpenLineage.InputDataset;
import io.openlineage.client.OpenLineage.Job;
Expand All @@ -23,21 +28,25 @@ public class OpenLineageTest {

@Test
public void jsonSerialization() throws JsonProcessingException {
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));

String producer = "producer";
URI producer = URI.create("producer");
OpenLineage ol = new OpenLineage(producer);
String runId = "runId";
RunFacets runFacets = ol.newRunFacets(null, null);
RunFacets runFacets = ol.newRunFacets(ol.newNominalTimeRunFacet(now, now), null);
Run run = ol.newRun(runId, runFacets);
String name = "jobName";
String namespace = "namespace";
JobFacets jobFacets = ol.newJobFacets(null, null, null);
Job job = ol.newJob(namespace, name, jobFacets);
List<InputDataset> inputs = Arrays.asList(ol.newInputDataset("ins", "input", null, null));
List<OutputDataset> outputs = Arrays.asList(ol.newOutputDataset("ons", "output", null, null));
RunEvent runStateUpdate = ol.newRunEvent("START", "123", run, job, inputs, outputs);
RunEvent runStateUpdate = ol.newRunEvent("START", now, run, job, inputs, outputs);

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String json = mapper.writeValueAsString(runStateUpdate);
RunEvent read = mapper.readValue(json, RunEvent.class);
Expand Down

0 comments on commit e09e9e5

Please sign in to comment.