Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into 1235-generic-dependent-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pdaulbyscottlogic authored Oct 2, 2019
2 parents 00a8683 + 684426e commit f007f84
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 143 deletions.
4 changes: 2 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Need an ICLA? Unsure if you are covered under an existing CCLA? Email [help@fino
* Please use [Markdown formatting](https://help.github.com/categories/writing-on-github/)
liberally to assist in readability.
* [Code fences](https://help.github.com/articles/creating-and-highlighting-code-blocks/) for exception stack traces and log entries, for example, massively improve readability.
* Further information regarding our Definition of Ready can be found [here] (https://github.com/finos/datahelix/blob/master/docs/developer/DefinitionOfReady.md)
* Further information regarding our Definition of Ready can be found [here](https://github.com/finos/datahelix/blob/master/docs/developer/DefinitionOfReady.md)

# Contributing Pull Requests (Code & Docs)
To make review of PRs easier, please:
Expand All @@ -46,7 +46,7 @@ To make review of PRs easier, please:
* Minimise non-functional changes (e.g. whitespace).
* Ensure all new files include a header comment block containing the [Apache License v2.0 and your copyright information](http://www.apache.org/licenses/LICENSE-2.0#apply).
* If necessary (e.g. due to 3rd party dependency licensing requirements), update the [NOTICE file](https://github.com/finos/datahelix/blob/master/NOTICE) with any new attribution or other notices
* Further information regarding our Definition of Done can be found [here] (https://github.com/finos/datahelix/blob/master/docs/developer/DefinitionOfDone.md)
* Further information regarding our Definition of Done can be found [here](https://github.com/finos/datahelix/blob/master/docs/developer/DefinitionOfDone.md)

## Commit and PR Messages

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.scottlogic.deg.common.util.NumberUtils;

import java.math.BigDecimal;
import java.util.Objects;

/**
* Granularity expressions could be interpreted differently depending on other constraints on a field (eg, type constraints),
Expand Down Expand Up @@ -56,4 +57,18 @@ public static ParsedGranularity parse(Object granularityExpression) {
public BigDecimal getNumericGranularity() {
return this.numericGranularity;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ParsedGranularity that = (ParsedGranularity) o;
return Objects.equals(numericGranularity, that.numericGranularity);
}

@Override
public int hashCode() {
return Objects.hash(numericGranularity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,16 @@
* limitations under the License.
*/


package com.scottlogic.deg.common.profile.constraints.delayed;

import com.scottlogic.deg.common.date.TemporalAdjusterGenerator;
import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.constraintdetail.AtomicConstraintType;
import com.scottlogic.deg.common.profile.constraints.Constraint;

public class DelayedAtomicConstraint implements Constraint {

private final Field field;
private final AtomicConstraintType underlyingConstraint;
private final Field otherField;

private final TemporalAdjusterGenerator offsetGenerator;

private final Integer offsetUnit;
public DelayedAtomicConstraint(Field field, AtomicConstraintType underlyingConstraint, Field otherField, TemporalAdjusterGenerator offsetGenerator, Integer offsetUnit) {
this.field = field;
this.underlyingConstraint = underlyingConstraint;
this.otherField = otherField;
this.offsetGenerator = offsetGenerator;
this.offsetUnit = offsetUnit;
}

public DelayedAtomicConstraint(Field field, AtomicConstraintType underlyingConstraint, Field otherField) {
this(field, underlyingConstraint, otherField, null, null);
}

static void validateFieldsAreDifferent(Field first, Field second) {
if (first.equals(second)) {
throw new IllegalArgumentException("Cannot have a relational field referring to itself");
}
}

public Field getField(){
return field;
}

public AtomicConstraintType getUnderlyingConstraint(){
return underlyingConstraint;
}

public Field getOtherField(){
return otherField;
}

public DelayedAtomicConstraint negate() {
return new DynamicNotConstraint(this);
}

public TemporalAdjusterGenerator getOffsetGenerator() {
return offsetGenerator;
}
public interface DelayedAtomicConstraint extends Constraint {
Field getField();

public Integer getOffsetUnit() {
return offsetUnit;
}
AtomicConstraintType getUnderlyingConstraint();

Field getOtherField();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package com.scottlogic.deg.common.profile.constraints.delayed;

import com.scottlogic.deg.common.date.TemporalAdjusterGenerator;
import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.constraintdetail.AtomicConstraintType;

public class DelayedDateAtomicConstraint implements DelayedAtomicConstraint {

private final Field field;
private final AtomicConstraintType underlyingConstraint;
private final Field otherField;

private final TemporalAdjusterGenerator offsetGenerator;

private final Integer offsetUnit;
public DelayedDateAtomicConstraint(Field field, AtomicConstraintType underlyingConstraint, Field otherField, TemporalAdjusterGenerator offsetGenerator, Integer offsetUnit) {
this.field = field;
this.underlyingConstraint = underlyingConstraint;
this.otherField = otherField;
this.offsetGenerator = offsetGenerator;
this.offsetUnit = offsetUnit;
}

public DelayedDateAtomicConstraint(Field field, AtomicConstraintType underlyingConstraint, Field otherField) {
this(field, underlyingConstraint, otherField, null, null);
}

@Override
public Field getField(){
return field;
}

@Override
public AtomicConstraintType getUnderlyingConstraint(){
return underlyingConstraint;
}

@Override
public Field getOtherField(){
return otherField;
}

@Override
public DelayedAtomicConstraint negate() {
throw new UnsupportedOperationException("negate is unsupported on a DelayedDateAtomicConstraint");
}

public TemporalAdjusterGenerator getOffsetGenerator() {
return offsetGenerator;
}

public Integer getOffsetUnit() {
return offsetUnit;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@

package com.scottlogic.deg.generator.fieldspecs;

import com.scottlogic.deg.common.ValidationException;
import com.scottlogic.deg.common.profile.constraints.delayed.*;
import com.scottlogic.deg.generator.fieldspecs.relations.*;

public class FieldRelationsFactory {

public FieldSpecRelations construct(DelayedAtomicConstraint constraint) {
if (constraint instanceof DynamicNotConstraint) {
throw new ValidationException("related field constraints cannot yet be negated");
}

switch (constraint.getUnderlyingConstraint()) {
case IS_EQUAL_TO_CONSTANT:
return constructEqualToDate(constraint);
return constructEqualToDate((DelayedDateAtomicConstraint)constraint);
case IS_BEFORE_CONSTANT_DATE_TIME:
return constructBeforeDate(constraint, false);
case IS_BEFORE_OR_EQUAL_TO_CONSTANT_DATE_TIME:
Expand Down Expand Up @@ -59,7 +54,7 @@ private FieldSpecRelations constructAfterDate(DelayedAtomicConstraint constraint
inclusive);
}

private FieldSpecRelations constructEqualToDate(DelayedAtomicConstraint constraint) {
private FieldSpecRelations constructEqualToDate(DelayedDateAtomicConstraint constraint) {
if (constraint.getOffsetUnit() != null) {
return new EqualToOffsetDateRelation(
constraint.getField(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ private void checkForAlphaGenerationDataTypes() {
description = "Defines whether to overwrite/replace existing output files")
boolean overwriteOutputFiles = false;

@CommandLine.Option(
names = {"--ndjson"},
description = "Defines whether JSON output is in NDJ (newline-delimited JSON) format- defaults true for stdOut")
private Boolean ndjson;

@CommandLine.Option(
names = { "--disable-schema-validation" },
description = "Disables schema validation")
Expand Down Expand Up @@ -152,6 +157,11 @@ public boolean useStdOut() {
return outputPath == null;
}

@Override
public boolean useNdJson() {
return (ndjson == null && this.useStdOut()) || ndjson;
}

@Override
public Path getOutputPath() {
return outputPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public boolean useStdOut() {
return false;
}

@Override
public boolean useNdJson() {
return false;
}

@Override
public OutputFormat getOutputFormat() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public interface OutputConfigSource {
Path getOutputPath();
boolean overwriteOutputFiles();
boolean useStdOut();
boolean useNdJson();
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ protected void configure() {
bind(boolean.class)
.annotatedWith(Names.named("config:canOverwriteOutputFiles"))
.toInstance(outputConfigSource.overwriteOutputFiles());

bind(boolean.class)
.annotatedWith(Names.named("config:useNdJson"))
.toInstance(outputConfigSource.useNdJson());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@

package com.scottlogic.deg.output.writer.json;

import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.scottlogic.deg.common.output.GeneratedObject;
import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.ProfileFields;
import com.scottlogic.deg.common.output.GeneratedObject;
import com.scottlogic.deg.output.writer.DataSetWriter;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -40,24 +36,16 @@ class JsonDataSetWriter implements DataSetWriter {
private final SequenceWriter writer;
private final ProfileFields fields;

private JsonDataSetWriter(SequenceWriter writer, ProfileFields fields) {
JsonDataSetWriter(SequenceWriter writer, ProfileFields fields) {
this.writer = writer;
this.fields = fields;
}

static DataSetWriter open(OutputStream stream, ProfileFields fields) throws IOException {
ObjectWriter objectWriter = new ObjectMapper().writer(new DefaultPrettyPrinter());
SequenceWriter writer = objectWriter.writeValues(stream);
writer.init(true);

return new JsonDataSetWriter(writer, fields);
}

@Override
public void writeRow(GeneratedObject row) throws IOException {
Map<Field, Object> jsonObject = new HashMap<>();
fields.forEach(field -> jsonObject
.put(field , convertValue(row.getFormattedValue(field))));
.put(field, convertValue(row.getFormattedValue(field))));

writer.write(jsonObject);
}
Expand All @@ -78,7 +66,7 @@ private static Object convertValue(Object value) {
} else if (value instanceof String) {
return value;
} else if (value instanceof OffsetDateTime) {
return standardDateFormat.format((OffsetDateTime)value);
return standardDateFormat.format((OffsetDateTime) value);
} else {
return value.toString();
}
Expand Down
Loading

0 comments on commit f007f84

Please sign in to comment.