Skip to content

Commit

Permalink
Improve information available from diffs
Browse files Browse the repository at this point in the history
This change adds a lot more details to the result of a ModelDiff,
including access to the raw Differences between the models, access to
the old and new models through the Differences object, access to the old
validation events and new validation events, and access to the diff
events. Helper methods were added to ModelDiff.Result to detect which
validation events were introduced by a change,  which validation
events were resolved by a change, and if there are breaking changes in
the diff (e.g., events that are ERROR or DANGER). Note that any kind of
suppression of diff events would need to be applied and managed outside
of this helper functionality.

Added some missing equals and hashCode implementations.
  • Loading branch information
mtdowling committed Sep 22, 2020
1 parent 9c05fa2 commit 5879d77
Show file tree
Hide file tree
Showing 5 changed files with 397 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@

package software.amazon.smithy.diff;

import java.util.Objects;
import software.amazon.smithy.model.FromSourceLocation;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
Expand Down Expand Up @@ -64,4 +65,23 @@ public Node getNewValue() {
public SourceLocation getSourceLocation() {
return getNewValue().getSourceLocation();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof ChangedMetadata)) {
return false;
} else {
ChangedMetadata that = (ChangedMetadata) o;
return getKey().equals(that.getKey())
&& Objects.equals(getOldValue(), that.getOldValue())
&& Objects.equals(getNewValue(), that.getNewValue());
}
}

@Override
public int hashCode() {
return Objects.hash(getKey(), getOldValue(), getNewValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import software.amazon.smithy.model.FromSourceLocation;
Expand Down Expand Up @@ -138,6 +139,26 @@ public Map<ShapeId, Pair<Trait, Trait>> getTraitDifferences() {
return traitDiff;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof ChangedShape)) {
return false;
} else {
// If the shapes are equal, then the changed traits are equal, so
// there's no need to compare the traitDiff property.
ChangedShape<?> that = (ChangedShape<?>) o;
return Objects.equals(getOldShape(), that.getOldShape())
&& Objects.equals(getNewShape(), that.getNewShape());
}
}

@Override
public int hashCode() {
return Objects.hash(getOldShape(), getNewShape());
}

/**
* Finds the trait differences between the old and new shape.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
Expand Down Expand Up @@ -161,6 +162,25 @@ public Stream<ChangedMetadata> changedMetadata() {
return changedMetadata.stream();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof Differences)) {
return false;
} else {
// The differences between the models are always equivalent if the
// models are equivalent, so no need to compare them.
Differences that = (Differences) o;
return getOldModel().equals(that.getOldModel()) && getNewModel().equals(that.getNewModel());
}
}

@Override
public int hashCode() {
return Objects.hash(getOldModel(), getNewModel());
}

private static void detectShapeChanges(Model oldModel, Model newModel, Differences differences) {
for (Shape oldShape : oldModel.toSet()) {
newModel.getShape(oldShape.getId()).ifPresent(newShape -> {
Expand Down
Loading

0 comments on commit 5879d77

Please sign in to comment.