Skip to content

Commit

Permalink
Merge branch 'master' of github.com:digipost/digipost-data-types into…
Browse files Browse the repository at this point in the history
… PickupNotice

* 'master' of github.com:digipost/digipost-data-types:
  Opplegget rundt Simple og Complex-type var litt naivt og støttet kun 1 package-nivå og at klasse og parentklasse lå i samme i genereringen av README.md.
  • Loading branch information
eivinhb committed Oct 18, 2018
2 parents cda2656 + 2a23c2b commit 19504fb
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 58 deletions.
59 changes: 34 additions & 25 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ Appointment represents a meeting set for a specific place and time
|endTime|ZonedDateTime|no|ISO8601 full DateTime. Default value 30 minutes after startTime|
|arrivalTime|String|no|Free text but can contain a ISO8601 DateTime. Example: Please arrive 15 minutes early|
|place|String|no|The name of the place. Example: Oslo City Røntgen|
|address|[Address](#address)|no||
|address|[Address](#appointmentaddress)|no||
|subTitle|String|no|Example: MR-undersøkelse av høyre kne|
|info|List|no|Additional sections of information (max 2) with a title and text|

### Address
### Appointment.Address

|Name|Type|Required|Description|
|----|----|--------|-----------|
Expand Down Expand Up @@ -68,7 +68,7 @@ Details about a Residence, and may be joined with Residence to retrieve the core

|Name|Type|Required|Description|
|----|----|--------|-----------|
|residence|[Residence](#residence)|yes||
|residence|[Residence](#boligdetaljerresidence)|yes||
|hjemmelshavere|List|no|List of people with legal rights associated with the residence|
|bruksareal|Integer|no|BRA for bolig|
|antallOppholdsrom|Integer|no|Number of rooms, bathroom, kitchen and storage rooms excluded|
Expand All @@ -79,16 +79,16 @@ Details about a Residence, and may be joined with Residence to retrieve the core
|andelsnummer|String|no||
|heftelser|List|no||

### Residence
### Boligdetaljer.Residence

|Name|Type|Required|Description|
|----|----|--------|-----------|
|address|[ResidenceAddress](#residenceaddress)|yes||
|matrikkel|[Matrikkel](#matrikkel)|no||
|address|[ResidenceAddress](#boligdetaljerresidenceaddress)|yes||
|matrikkel|[Matrikkel](#boligdetaljermatrikkel)|no||
|source|String|no||
|externalId|String|no||

### ResidenceAddress
### Boligdetaljer.ResidenceAddress

|Name|Type|Required|Description|
|----|----|--------|-----------|
Expand All @@ -98,7 +98,7 @@ Details about a Residence, and may be joined with Residence to retrieve the core
|postalCode|String|no||
|city|String|no||

### Matrikkel
### Boligdetaljer.Matrikkel

|Name|Type|Required|Description|
|----|----|--------|-----------|
Expand Down Expand Up @@ -360,26 +360,35 @@ Receipt represents a document containing details about a purchase
|merchantChain|String|no|Optional name of the chain that the merchant is a part of|
|merchantName|String|yes|Name of the store or merchant. Example: Grünerløkka Hip Coffee|
|merchantPhoneNumber|String|no||
|merchantAddress|Address|no|Address of the store or merchant|
|merchantAddress|[Address](#receiptaddress)|no|Address of the store or merchant|
|organizationNumber|String|no|Organization number of the sales point|
|barcode|[Barcode](#barcode)|no||
|barcode|[Barcode](#receiptbarcode)|no||
|payments|List|no|List of payments done during this purchase|
|items|List|no|The individual items sold|
|taxiDetails|[TaxiDetails](#taxidetails)|no|Details for taxi receipts|
|customer|[Customer](#customer)|no|Name and address of customer|
|delivery|[Delivery](#delivery)|no|Name and address of delivery|
|taxiDetails|[TaxiDetails](#receipttaxidetails)|no|Details for taxi receipts|
|customer|[Customer](#receiptcustomer)|no|Name and address of customer|
|delivery|[Delivery](#receiptdelivery)|no|Name and address of delivery|
|orderNumber|String|no||
|membershipNumber|String|no||
|comment|String|no||

### Barcode
### Receipt.Address

|Name|Type|Required|Description|
|----|----|--------|-----------|
|streetAddress|String|no|E.g. Storgata 11|
|postalCode|String|no||
|city|String|no||
|country|String|no||

### Receipt.Barcode

|Name|Type|Required|Description|
|----|----|--------|-----------|
|barcodeValue|String|no|The barcode on this receipt|
|barcodeType|String|no||

### TaxiDetails
### Receipt.TaxiDetails

|Name|Type|Required|Description|
|----|----|--------|-----------|
Expand All @@ -396,29 +405,29 @@ Receipt represents a document containing details about a purchase
|totalTimeBeforeBoardingInSeconds|Integer|no||
|totalTimeInSeconds|Integer|no||
|totalTimeWithPassengerInSeconds|Integer|no||
|vat|[VatDetails](#vatdetails)|no||
|vat|[VatDetails](#receiptvatdetails)|no||

### VatDetails
### Receipt.VatDetails

|Name|Type|Required|Description|
|----|----|--------|-----------|
|levels|List|no||
|sum|BigDecimal|no||

### Customer
### Receipt.Customer

|Name|Type|Required|Description|
|----|----|--------|-----------|
|name|String|no||
|address|Address|no||
|address|[Address](#receiptaddress)|no||
|phoneNumber|String|no||

### Delivery
### Receipt.Delivery

|Name|Type|Required|Description|
|----|----|--------|-----------|
|name|String|no||
|address|Address|no||
|address|[Address](#receiptaddress)|no||
|terms|String|no||

### XML
Expand Down Expand Up @@ -532,12 +541,12 @@ Residence is a way of linking separate data for the same residence

|Name|Type|Required|Description|
|----|----|--------|-----------|
|address|[ResidenceAddress](#residenceaddress)|yes||
|matrikkel|[Matrikkel](#matrikkel)|no||
|address|[ResidenceAddress](#residenceresidenceaddress)|yes||
|matrikkel|[Matrikkel](#residencematrikkel)|no||
|source|String|no||
|externalId|String|no||

### ResidenceAddress
### Residence.ResidenceAddress

|Name|Type|Required|Description|
|----|----|--------|-----------|
Expand All @@ -547,7 +556,7 @@ Residence is a way of linking separate data for the same residence
|postalCode|String|no||
|city|String|no||

### Matrikkel
### Residence.Matrikkel

|Name|Type|Required|Description|
|----|----|--------|-----------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public boolean isComplex() {
public int compareTo(ComplexType o) {
return getTypeName().compareTo(o.getTypeName());
}

public boolean hasSameNameAs(ComplexType ct){
return this.getTypeName().equals(ct.getTypeName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.digipost.api.datatypes.documentation;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME) @Target({PACKAGE})
public @interface DataTypePackage {
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ private static <T> Function<? super Class<? extends T>, ComplexType> getTypeInfo
}

private static List<FieldInfo> getFieldInfos(Class<?> dataType) {
return Stream.of(dataType.getDeclaredFields()).filter(f -> !isStatic(f.getModifiers())).map(getFieldInfo(dataType)).collect(toList());
return Stream.of(dataType.getDeclaredFields()).filter(f -> !isStatic(f.getModifiers())).map(getFieldInfo()).collect(toList());
}

private static Function<Field, FieldInfo> getFieldInfo(final Class<?> parentType) {
return field -> new FieldInfo(field.getName(), resolveTypeOfField(parentType, field.getType()), isMandatory(field), getDescription(field));
private static Function<Field, FieldInfo> getFieldInfo() {
return field -> new FieldInfo(field.getName(), resolveTypeOfField(field.getType()), isMandatory(field), getDescription(field));
}

private static String getDescription(AnnotatedElement element) {
Expand All @@ -44,9 +44,9 @@ private static boolean isMandatory(Field field) {
final boolean notNull = field.getAnnotationsByType(NotNull.class).length > 0;
return requiredXmlElement || notNull;
}

private static FieldType resolveTypeOfField(Class<?> parentType, Class<?> fieldType) {
if (fieldType.getPackage().equals(parentType.getPackage())) {
private static FieldType resolveTypeOfField(Class<?> fieldType) {
if (fieldType.getPackage().isAnnotationPresent(DataTypePackage.class)) {
return new ComplexType(fieldType, getDescription(fieldType), getFieldInfos(fieldType), null);
} else {
return new SimpleType(fieldType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import static java.lang.System.lineSeparator;
Expand All @@ -30,7 +32,7 @@ public MarkdownPrinter(JAXBContext jaxbContext, boolean printJsonExamples) {


public String print(List<ComplexType> typeInfos) {
return printHeader(typeInfos) + LLF +
return printHeader(typeInfos) + LLF +
printTypes(typeInfos) + LF;
}

Expand All @@ -39,19 +41,19 @@ private String printTypes(List<ComplexType> typeInfos) {
}

private String printHeader(List<ComplexType> typeInfos) {
return heading(2, "Data types") + LLF +
return heading(2, "Data types") + LLF +
"|Type|Description|" + LF +
"|----|-----------|" + LF +
typeInfos.stream().map(t ->
"|" + printLink(t) + "|" + t.getDescription() + "|"
"|" + printLink(t, t) + "|" + t.getDescription() + "|"
).collect(joining(LF));
}

private String printTypeOverview(ComplexType typeInfo) {
return heading(2, typeInfo.getTypeName()) + LLF +
return heading(2, typeInfo.getTypeName()) + LLF +
typeInfo.getDescription() + LLF +
heading(3, "Fields") + LLF +
printFields(typeInfo.getFields()) + LLF +
printFields(typeInfo, typeInfo.getFields(), new HashSet<>()) + LLF +
(this.printJsonExamples ? printJsonExample(typeInfo.getExample()) + LLF : "") +
printXmlExample(typeInfo.getExample());
}
Expand All @@ -62,7 +64,7 @@ private String printXmlExample(Object example) {
final Marshaller marshaller = jaxb.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(example, writer);
return heading(3, "XML") + LLF +
return heading(3, "XML") + LLF +
code("xml", writer.toString().trim());
} catch (JAXBException e) {
throw new RuntimeException(e);
Expand All @@ -75,36 +77,46 @@ private String code(String type, String s) {

private String printJsonExample(Object example) {
try {
return heading(3, "JSON") + LLF +
return heading(3, "JSON") + LLF +
code("json",
DataTypesJsonMapper.getMapper()
.configure(SerializationFeature.INDENT_OUTPUT, true)
.writeValueAsString(example).trim());
DataTypesJsonMapper.getMapper()
.configure(SerializationFeature.INDENT_OUTPUT, true)
.writeValueAsString(example).trim());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private String printTypeInfo(ComplexType type) {
return heading(3, type.getTypeName()) + LLF +
printFields(type.getFields());
private String printTypeInfo(ComplexType parent, ComplexType type, Set<ComplexType> printed) {
printed.add(type);
return heading(3, parent.getTypeName() + "." + type.getTypeName()) + LLF +
printFields(parent, type.getFields(), printed);
}

private String printFields(List<FieldInfo> fields) {
final String subTypes = fields.stream().map(FieldInfo::getType).filter(FieldType::isComplex).map(t -> (ComplexType) t).map(this::printTypeInfo).collect(joining(LLF));
return "|Name|Type|Required|Description|" + LF +
private String printFields(ComplexType parent, List<FieldInfo> fields, Set<ComplexType> printed) {

final String subTypes = fields.stream()
.map(FieldInfo::getType)
.filter(FieldType::isComplex)
.map(t -> (ComplexType) t)
.filter(o -> !printed.contains(o))
.map(type -> printTypeInfo(parent, type, printed))
.collect(joining(LLF));
return "|Name|Type|Required|Description|" + LF +
"|----|----|--------|-----------|" + LF +
fields.stream().map(this::printField).collect(joining(LF)) +
fields.stream().map(f -> printField(parent, f)).collect(joining(LF)) +
(subTypes.isEmpty() ? subTypes : LLF + subTypes);
}

private String printField(FieldInfo f) {
final String type = f.getType().isComplex() ? printLink(f.getType()) : f.getType().getTypeName();
private String printField(ComplexType parent, FieldInfo f) {
final String type = f.getType().isComplex() ? printLink(parent, (ComplexType) f.getType()) : f.getType().getTypeName();
return "|" + f.getName() + "|" + type + "|" + (f.isMandatory() ? "yes" : "no") + "|" + f.getDescription() + "|";
}

private String printLink(FieldType f) {
return "[" + f.getTypeName() + "](#" + f.getTypeName().toLowerCase().replaceAll(" ", "-") + ")";
private String printLink(ComplexType parent, ComplexType field) {
String prefix = (!parent.hasSameNameAs(field)) ? parent.getTypeName().toLowerCase().replaceAll(" ", "-") : "";

return "[" + field.getTypeName() + "](#" + prefix + field.getTypeName().toLowerCase().replaceAll(" ", "-") + ")";
}

private String heading(int level, String heading) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@XmlSchema(namespace = DIGIPOST_DATATYPES_NAMESPACE, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class)
@DataTypePackage
package no.digipost.api.datatypes.types;

import no.digipost.api.datatypes.documentation.DataTypePackage;
import no.digipost.api.datatypes.marshalling.ZonedDateTimeXmlAdapter;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class),
@XmlJavaTypeAdapter(MoneyBigDecimalXmlAdapter.class)
})
@DataTypePackage
package no.digipost.api.datatypes.types.receipt;

import no.digipost.api.datatypes.documentation.DataTypePackage;
import no.digipost.api.datatypes.marshalling.MoneyBigDecimalXmlAdapter;
import no.digipost.api.datatypes.marshalling.ZonedDateTimeXmlAdapter;

Expand All @@ -16,3 +18,4 @@
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import static no.digipost.api.datatypes.marshalling.DataTypesJAXBContext.DIGIPOST_DATATYPES_NAMESPACE;

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package no.digipost.api.datatypes.documentation;

import no.digipost.api.datatypes.DataType;
import no.digipost.api.datatypes.types.MetaData;
import no.digipost.api.datatypes.types.ShortTextMessage;
import org.junit.Test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Stream;

Expand All @@ -24,17 +26,26 @@ public class DocumentationTest {
@Test
public void should_build_type_structure_for_test_data_type() {
final Stream<ComplexType> types = DocumentationStructureBuilder.<DataType>buildTypeStructure(
singleton(ShortTextMessage.class), t -> ShortTextMessage.EXAMPLE);
singleton(ShortTextMessage.class), t -> ShortTextMessage.EXAMPLE);
System.out.println(MetaData.EXAMPLE);
assertThat(types.collect(toSet()), contains(
new ComplexType(ShortTextMessage.class, "150 character short message",
Collections.singletonList(new FieldInfo("message", new SimpleType(String.class), true, "Your short message goes here")),
ShortTextMessage.EXAMPLE)));
new ComplexType(ShortTextMessage.class, "150 character short message",
Arrays.asList(
new FieldInfo("message", new SimpleType(String.class), true, "Your short message goes here"),
new FieldInfo("metaData",
new ComplexType(
MetaData.class
, "Metainformation"
, Collections.singletonList(new FieldInfo("value", new SimpleType(String.class), true, "Your extra information"))
, null), false, "Some metadata for shortTextMessage")
),
ShortTextMessage.EXAMPLE)));
}

@Test
public void should_print_docs_for_test_data_type() throws IOException, JAXBException {
final String docs = new MarkdownPrinter(JAXBContext.newInstance(ShortTextMessage.class), true).print(DocumentationStructureBuilder.<DataType>buildTypeStructure(
singleton(ShortTextMessage.class), t -> ShortTextMessage.EXAMPLE).collect(toList()));
singleton(ShortTextMessage.class), t -> ShortTextMessage.EXAMPLE).collect(toList()));

assertThat(docs, is(new String(toByteArray(getClass().getResource("testdoc.md")), UTF_8)));
}
Expand Down
Loading

0 comments on commit 19504fb

Please sign in to comment.