Skip to content
Draft
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
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<description>Schema2proto - XSD schema to protobuf.</description>
<modules>
<module>schema2proto-lib</module>
<module>schema2proto-wire</module>
<module>schema2proto-maven-plugin</module>
<module>schema2proto-xsdproto</module>
<module>schema2proto-xsom</module>
Expand Down Expand Up @@ -59,11 +58,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>schema2proto-wire</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>schema2proto-xsom</artifactId>
Expand Down Expand Up @@ -189,6 +183,11 @@
<artifactId>wire-runtime-jvm</artifactId>
<version>${wire.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.wire</groupId>
<artifactId>wire-schema-jvm</artifactId>
<version>${wire.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
8 changes: 6 additions & 2 deletions schema2proto-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>no.entur</groupId>
<artifactId>schema2proto-wire</artifactId>
<groupId>com.squareup.wire</groupId>
<artifactId>wire-schema-jvm</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.squareup.wire.schema.EnumConstant;
import com.squareup.wire.schema.EnumType;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.ProtoFile;

import no.entur.schema2proto.compatibility.protolock.ProtolockEnum;
import no.entur.schema2proto.compatibility.protolock.ProtolockEnumConstant;
import no.entur.schema2proto.wire.MutableEnumConstant;
import no.entur.schema2proto.wire.MutableEnumType;
import no.entur.schema2proto.wire.MutableProtoFile;

public class EnumConflictChecker {

private final static Logger LOGGER = LoggerFactory.getLogger(EnumConflictChecker.class);
private boolean failIfRemovedFieldsTriggered;

public boolean tryResolveEnumConflicts(ProtoFile file, EnumType enumType, ProtolockEnum protolockEnum) {
public boolean tryResolveEnumConflicts(MutableProtoFile file, MutableEnumType enumType, ProtolockEnum protolockEnum) {
SortedSet<ProtolockEnumConstant> lockEnumConstants = Collections.unmodifiableSortedSet(new TreeSet<>(Arrays.asList(protolockEnum.getEnumFields()))); // from
// proto.lock
SortedSet<ProtolockEnumConstant> xsdEnumConstants = Collections.unmodifiableSortedSet(
Expand Down Expand Up @@ -90,7 +90,7 @@ public boolean tryResolveEnumConflicts(ProtoFile file, EnumType enumType, Protol

AtomicInteger nextId = new AtomicInteger(maxLockId + 1);

for (EnumConstant constant : enumType.constants()) {
for (MutableEnumConstant constant : enumType.constants()) {
if (!lockNames.contains(constant.getName())) {
while (enumType.reserveds().stream().anyMatch(s -> s.matchesTag(nextId.get()))) {
nextId.incrementAndGet();
Expand All @@ -108,7 +108,7 @@ public boolean tryResolveEnumConflicts(ProtoFile file, EnumType enumType, Protol
return failIfRemovedFieldsTriggered;
}

private void reserveEnumConstant(ProtoFile file, EnumType e, ProtolockEnumConstant newEnumConstant) {
private void reserveEnumConstant(MutableProtoFile file, MutableEnumType e, ProtolockEnumConstant newEnumConstant) {

String reservationDoc = "Reservation added by schema2proto";
Location loc = new Location("", "", 0, 0);
Expand All @@ -123,7 +123,7 @@ private void reserveEnumConstant(ProtoFile file, EnumType e, ProtolockEnumConsta
failIfRemovedFieldsTriggered = true;
}

private static Optional<EnumConstant> getConstant(EnumType e, String intrudingConstantName) {
private static Optional<MutableEnumConstant> getConstant(MutableEnumType e, String intrudingConstantName) {
return e.constants().stream().filter(z -> z.getName().equals(intrudingConstantName)).findFirst();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@
import org.slf4j.LoggerFactory;

import com.google.common.collect.BiMap;
import com.squareup.wire.schema.Field;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.MessageType;
import com.squareup.wire.schema.ProtoFile;

import no.entur.schema2proto.compatibility.protolock.ProtolockField;
import no.entur.schema2proto.compatibility.protolock.ProtolockMessage;
import no.entur.schema2proto.wire.MutableField;
import no.entur.schema2proto.wire.MutableMessageType;
import no.entur.schema2proto.wire.MutableProtoFile;

public class FieldConflictChecker {

private final Logger LOGGER = LoggerFactory.getLogger(FieldConflictChecker.class);
private boolean failIfRemovedFieldsTriggered;

public boolean tryResolveFieldConflicts(ProtoFile file, MessageType protoMessage, ProtolockMessage protolockMessage) {
public boolean tryResolveFieldConflicts(MutableProtoFile file, MutableMessageType protoMessage, ProtolockMessage protolockMessage) {

// Compute helper maps
SortedSet<ProtolockField> lockFileFields = Collections.unmodifiableSortedSet(getFields(protolockMessage)); // from proto.lock
Expand Down Expand Up @@ -84,7 +84,7 @@ public boolean tryResolveFieldConflicts(ProtoFile file, MessageType protoMessage
if (originalIdForField == null) {
originalIdForField = findNextAvailableFieldNum(protoMessage, protoMessageFieldAsLockFields, lockFileFields).get();
}
Optional<Field> intrudingField = getField(protoMessage, protoMessageFieldAsLockField.getName());
Optional<MutableField> intrudingField = getField(protoMessage, protoMessageFieldAsLockField.getName());
intrudingField.get().updateTag(originalIdForField);

//
Expand Down Expand Up @@ -164,8 +164,8 @@ public boolean tryResolveFieldConflicts(ProtoFile file, MessageType protoMessage

}

private void updateFieldTag(AtomicInteger nextAvailableFieldNum, int overlappingId, Optional<Field> intrudingField, Optional<Field> existingField,
Integer idFromLockFile) {
private void updateFieldTag(AtomicInteger nextAvailableFieldNum, int overlappingId, Optional<MutableField> intrudingField,
Optional<MutableField> existingField, Integer idFromLockFile) {
intrudingField.ifPresent(x -> {
if (idFromLockFile != null) {
x.updateTag(idFromLockFile);
Expand All @@ -178,7 +178,7 @@ private void updateFieldTag(AtomicInteger nextAvailableFieldNum, int overlapping
}

@NotNull
private AtomicInteger findNextAvailableFieldNum(MessageType e, SortedSet<ProtolockField> xsdFields, SortedSet<ProtolockField> lockFields) {
private AtomicInteger findNextAvailableFieldNum(MutableMessageType e, SortedSet<ProtolockField> xsdFields, SortedSet<ProtolockField> lockFields) {
AtomicInteger nextAvailableFieldNum = new AtomicInteger(
xsdFields.stream().max(Comparator.comparing(ProtolockField::getId)).orElse(new ProtolockField(0, null)).getId() + 1);

Expand Down Expand Up @@ -207,15 +207,15 @@ private boolean isReserved(ProtolockMessage protolockMessage, ProtolockField fie
return reservedFieldNum;
}

private Optional<Field> getField(MessageType e, String fieldName) {
private Optional<MutableField> getField(MutableMessageType e, String fieldName) {
return e.fieldsAndOneOfFields().stream().filter(z -> z.name().equals(fieldName)).findFirst();
}

private Optional<Field> getField(MessageType e, Integer fieldId) {
private Optional<MutableField> getField(MutableMessageType e, Integer fieldId) {
return e.fieldsAndOneOfFields().stream().filter(z -> z.tag() == fieldId).findFirst();
}

private void reserveField(ProtoFile file, MessageType e, ProtolockField newField) {
private void reserveField(MutableProtoFile file, MutableMessageType e, ProtolockField newField) {

String reservationDoc = "Reservation added by schema2proto";
Location loc = new Location("", "", 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.squareup.wire.schema.EnumType;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.MessageType;
import com.squareup.wire.schema.ProtoFile;

import no.entur.schema2proto.compatibility.protolock.ProtolockDefinition;
import no.entur.schema2proto.compatibility.protolock.ProtolockDefinitions;
import no.entur.schema2proto.compatibility.protolock.ProtolockEnum;
import no.entur.schema2proto.compatibility.protolock.ProtolockFile;
import no.entur.schema2proto.compatibility.protolock.ProtolockMessage;
import no.entur.schema2proto.wire.MutableEnumType;
import no.entur.schema2proto.wire.MutableMessageType;
import no.entur.schema2proto.wire.MutableProtoFile;

public class ProtolockBackwardsCompatibilityChecker {

Expand All @@ -66,7 +66,7 @@ public ProtolockDefinitions getDefinitions() {
return definitions;
}

private void copyReservations(ProtolockMessage protolockMessage, MessageType protoMessage) {
private void copyReservations(ProtolockMessage protolockMessage, MutableMessageType protoMessage) {
if (protolockMessage.getReservedIds() != null && protolockMessage.getReservedIds().length > 0) {
Arrays.stream(protolockMessage.getReservedIds()).forEach(reservedId -> protoMessage.addReserved(reservationDoc, reservationLocation, reservedId));
}
Expand All @@ -75,7 +75,7 @@ private void copyReservations(ProtolockMessage protolockMessage, MessageType pro
.forEach(reservedName -> protoMessage.addReserved(reservationDoc, reservationLocation, reservedName));
}

protoMessage.nestedTypes().stream().filter(z -> z instanceof MessageType).map(k -> (MessageType) k).forEach(nestedType -> {
protoMessage.nestedTypes().stream().filter(z -> z instanceof MutableMessageType).map(k -> (MutableMessageType) k).forEach(nestedType -> {
if (protolockMessage.getMessages() != null) {
Arrays.stream(protolockMessage.getMessages()).forEach(nestedProtolockMessage -> {
if (nestedProtolockMessage.getName().equals(nestedType.getName())) {
Expand All @@ -87,7 +87,7 @@ private void copyReservations(ProtolockMessage protolockMessage, MessageType pro

}

private void copyReservations(ProtolockEnum protolockEnum, EnumType protoEnum) {
private void copyReservations(ProtolockEnum protolockEnum, MutableEnumType protoEnum) {
LOGGER.debug("Copying reservations for message {} and enum {}", protolockEnum, protoEnum);
if (protolockEnum.getReservedIds() != null && protolockEnum.getReservedIds().length > 0) {
Arrays.stream(protolockEnum.getReservedIds()).forEach(reservedId -> protoEnum.addReserved(reservationDoc, reservationLocation, reservedId));
Expand All @@ -97,7 +97,7 @@ private void copyReservations(ProtolockEnum protolockEnum, EnumType protoEnum) {
}
}

public boolean resolveBackwardIncompatibilities(ProtoFile protoFile) {
public boolean resolveBackwardIncompatibilities(MutableProtoFile protoFile) {
LOGGER.debug("Trying to resolve backward incompabilities in file {}", protoFile);

AtomicBoolean failIfRemovedFieldsTriggered = new AtomicBoolean(false);
Expand All @@ -107,7 +107,7 @@ public boolean resolveBackwardIncompatibilities(ProtoFile protoFile) {
if (protolockFile != null) {

// For each enum on file level (global enums)
protoFile.types().stream().filter(type -> type instanceof EnumType).map(enumType -> (EnumType) enumType).forEach(enumType -> {
protoFile.types().stream().filter(type -> type instanceof MutableEnumType).map(enumType -> (MutableEnumType) enumType).forEach(enumType -> {
if (protolockFile.getEnums() != null) {
Arrays.stream(protolockFile.getEnums()).filter(pe -> pe.getName().equals(enumType.name())).findFirst().ifPresent(protolockEnum -> {
copyReservations(protolockEnum, enumType);
Expand All @@ -119,7 +119,7 @@ public boolean resolveBackwardIncompatibilities(ProtoFile protoFile) {
}
});

protoFile.types().stream().filter(z -> z instanceof MessageType).map(ke -> (MessageType) ke).forEach(e -> {
protoFile.types().stream().filter(z -> z instanceof MutableMessageType).map(ke -> (MutableMessageType) ke).forEach(e -> {
// For each root level message in file
ProtolockMessage protolockMessage = getProtolockMessage(protolockFile, e);
if (protolockMessage != null) {
Expand All @@ -134,7 +134,7 @@ public boolean resolveBackwardIncompatibilities(ProtoFile protoFile) {
return failIfRemovedFieldsTriggered.get();
}

private boolean resolveBackwardIncompatibilities(ProtoFile protoFile, ProtolockMessage protolockMessage, MessageType protoMessage) {
private boolean resolveBackwardIncompatibilities(MutableProtoFile protoFile, ProtolockMessage protolockMessage, MutableMessageType protoMessage) {
LOGGER.debug("Resolving backward compabilities in file {}, message {}", protoFile.name(), protoMessage);

AtomicBoolean failIfRemovedFieldsTriggered = new AtomicBoolean(false);
Expand All @@ -149,7 +149,7 @@ private boolean resolveBackwardIncompatibilities(ProtoFile protoFile, ProtolockM
failIfRemovedFieldsTriggered.set(true);
}

protoMessage.nestedTypes().stream().filter(type -> type instanceof MessageType).map(r -> (MessageType) r).forEach(nestedProtoMessage -> {
protoMessage.nestedTypes().stream().filter(type -> type instanceof MutableMessageType).map(r -> (MutableMessageType) r).forEach(nestedProtoMessage -> {
ProtolockMessage nestedProtolockMessage = getNestedProtolockMessage(protolockMessage, nestedProtoMessage);
if (nestedProtolockMessage != null) {
if (resolveBackwardIncompatibilities(protoFile, nestedProtolockMessage, nestedProtoMessage)) {
Expand All @@ -162,11 +162,11 @@ private boolean resolveBackwardIncompatibilities(ProtoFile protoFile, ProtolockM

}

private boolean tryResolveEnumConflicts(ProtoFile protoFile, MessageType protoMessage, ProtolockMessage protolockMessage) {
private boolean tryResolveEnumConflicts(MutableProtoFile protoFile, MutableMessageType protoMessage, ProtolockMessage protolockMessage) {
LOGGER.debug("Trying to resolve enum conflicts in file {}, message {}", protoFile.name(), protoMessage);
AtomicBoolean failIfRemovedFieldsTriggered = new AtomicBoolean(false);
// For each enum in proto, try to find mismatching enum values and resolve
protoMessage.nestedTypes().stream().filter(type -> type instanceof EnumType).map(type -> (EnumType) type).forEach(enumType -> {
protoMessage.nestedTypes().stream().filter(type -> type instanceof MutableEnumType).map(type -> (MutableEnumType) type).forEach(enumType -> {
// Find matching in protolockmessage
if (protolockMessage.getEnums() != null) {
Arrays.stream(protolockMessage.getEnums()).filter(e -> e.getName().equals(enumType.name())).findFirst().ifPresent(protolockEnum -> {
Expand All @@ -180,15 +180,15 @@ private boolean tryResolveEnumConflicts(ProtoFile protoFile, MessageType protoMe
return failIfRemovedFieldsTriggered.get();
}

private ProtolockMessage getProtolockMessage(ProtolockFile protolockFile, MessageType protoMessage) {
private ProtolockMessage getProtolockMessage(ProtolockFile protolockFile, MutableMessageType protoMessage) {
if (protolockFile != null && protolockFile.getMessages() != null) {
return Arrays.stream(protolockFile.getMessages()).filter(message -> message.getName().equals(protoMessage.getName())).findFirst().orElse(null);
}

return null;
}

private ProtolockMessage getNestedProtolockMessage(ProtolockMessage protolockMessage, MessageType nestedProtoMessage) {
private ProtolockMessage getNestedProtolockMessage(ProtolockMessage protolockMessage, MutableMessageType nestedProtoMessage) {
if (protolockMessage != null && protolockMessage.getMessages() != null) {
return Arrays.stream(protolockMessage.getMessages())
.filter(subMessage -> subMessage.getName().equals(nestedProtoMessage.getName()))
Expand All @@ -199,7 +199,7 @@ private ProtolockMessage getNestedProtolockMessage(ProtolockMessage protolockMes
}
}

private ProtolockFile getProtolockFile(ProtoFile protoFile) {
private ProtolockFile getProtolockFile(MutableProtoFile protoFile) {
String fullPath = protoFile.toString();
if (!fullPath.contains("/")) {
// Assume no package in filename yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@
*/
package no.entur.schema2proto.generateproto;

import com.squareup.wire.schema.Field;
import com.squareup.wire.schema.MessageType;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSComponent;

import no.entur.schema2proto.wire.MutableField;
import no.entur.schema2proto.wire.MutableMessageType;

public class LocalType {
XSComponent xsComponent;
MessageType localType;
MessageType enclosingType;
Field referencingField;
MutableMessageType localType;
MutableMessageType enclosingType;
MutableField referencingField;
String targetPackage;
XSComplexType enclosingComplexType;

public LocalType(XSComponent xsComponent, MessageType localType, MessageType enclosingType, Field referencingField, String targetPackage,
XSComplexType enclosingComplexType) {
public LocalType(XSComponent xsComponent, MutableMessageType localType, MutableMessageType enclosingType, MutableField referencingField,
String targetPackage, XSComplexType enclosingComplexType) {
this.xsComponent = xsComponent;
this.localType = localType;
this.enclosingType = enclosingType;
Expand Down
Loading
Loading