Skip to content

Commit 27708e0

Browse files
Merge pull request #154 from SpineEventEngine/value-mismatch
Value Mismatch
2 parents 6fb03c0 + 72fa493 commit 27708e0

File tree

7 files changed

+503
-7
lines changed

7 files changed

+503
-7
lines changed

.idea/codeStyleSettings.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.base;
22+
23+
import com.google.protobuf.Any;
24+
import com.google.protobuf.Message;
25+
26+
import javax.annotation.Nullable;
27+
28+
import static org.spine3.protobuf.Values.pack;
29+
import static org.spine3.protobuf.Messages.toAny;
30+
31+
32+
/**
33+
* Factories for constructing {@link ValueMismatch} instances for different types of attributes.
34+
*/
35+
public class Mismatch {
36+
37+
private Mismatch() {
38+
}
39+
40+
/**
41+
* Creates a {@link ValueMismatch} instance for a string attribute.
42+
*
43+
* @param expected the value expected by a command, or {@code null} if the command expects not populated field
44+
* @param actual the value found in an entity, or {@code null} if the value is not set
45+
* @param requested the value requested as new one in the original command
46+
* @param version the current version of the entity
47+
* @return info on the mismatch
48+
*/
49+
public static ValueMismatch of(@Nullable String expected, @Nullable String actual, String requested, int version) {
50+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
51+
if (expected != null) {
52+
builder.setExpected(pack(expected));
53+
}
54+
55+
if (actual != null) {
56+
builder.setActual(pack(actual));
57+
}
58+
59+
builder.setRequested(pack(requested));
60+
builder.setVersion(version);
61+
62+
return builder.build();
63+
}
64+
65+
/**
66+
* Creates a {@link ValueMismatch} instance for a integer attribute.
67+
*
68+
* @param expected the value expected by a command
69+
* @param actual the value actual in an entity
70+
* @param requested the value requested as new one in the original command
71+
* @param version the current version of the entity
72+
* @return info on the mismatch
73+
*/
74+
public static ValueMismatch of(int expected, int actual, int requested, int version) {
75+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
76+
builder.setExpected(pack(expected));
77+
builder.setActual(pack(actual));
78+
builder.setRequested(pack(requested));
79+
builder.setVersion(version);
80+
81+
return builder.build();
82+
}
83+
84+
/**
85+
* Creates a {@link ValueMismatch} instance for a long integer attribute.
86+
*
87+
* @param expected the value expected by a command
88+
* @param actual the value actual in an entity
89+
* @param requested the value requested as new one in the original command
90+
* @param version the current version of the entity
91+
* @return info on the mismatch
92+
*/
93+
public static ValueMismatch of(long expected, long actual, long requested, int version) {
94+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
95+
builder.setExpected(pack(expected));
96+
builder.setActual(pack(actual));
97+
builder.setRequested(pack(requested));
98+
builder.setVersion(version);
99+
100+
return builder.build();
101+
}
102+
103+
/**
104+
* Creates a {@link ValueMismatch} instance for a float attribute.
105+
*
106+
* @param expected the value expected by a command
107+
* @param actual the value actual in an entity
108+
* @param requested the value requested as new one in the original command
109+
* @param version the current version of the entity
110+
* @return info on the mismatch
111+
*/
112+
public static ValueMismatch of(float expected, float actual, float requested, int version) {
113+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
114+
builder.setExpected(pack(expected));
115+
builder.setActual(pack(actual));
116+
builder.setRequested(pack(requested));
117+
builder.setVersion(version);
118+
119+
return builder.build();
120+
}
121+
122+
/**
123+
* Creates a {@link ValueMismatch} instance for a double attribute.
124+
*
125+
* @param expected the value expected by a command
126+
* @param actual the value actual in an entity
127+
* @param requested the value requested as new one in the original command
128+
* @param version the current version of the entity
129+
* @return info on the mismatch
130+
*/
131+
public static ValueMismatch of(double expected, double actual, double requested, int version) {
132+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
133+
builder.setExpected(pack(expected));
134+
builder.setActual(pack(actual));
135+
builder.setRequested(pack(requested));
136+
builder.setVersion(version);
137+
138+
return builder.build();
139+
}
140+
141+
/**
142+
* Creates a {@link ValueMismatch} instance for a boolean attribute.
143+
*
144+
* @param expected the value expected by a command
145+
* @param actual the value actual in an entity
146+
* @param requested the value requested as new one in the original command
147+
* @param version the current version of the entity
148+
* @return info on the mismatch
149+
*/
150+
public static ValueMismatch of(boolean expected, boolean actual, boolean requested, int version) {
151+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
152+
builder.setExpected(pack(expected));
153+
builder.setActual(pack(actual));
154+
builder.setRequested(pack(requested));
155+
builder.setVersion(version);
156+
157+
return builder.build();
158+
}
159+
160+
/**
161+
* Creates a {@link ValueMismatch} instance for a Message attribute.
162+
*
163+
* @param expected the value expected by a command, or {@code null} if the command expects not populated field
164+
* @param actual the value actual in an entity, or {@code null} if the value is not set
165+
* @param requested the value requested as new one in the original command
166+
* @param version the current version of the entity
167+
* @return info on the mismatch
168+
*/
169+
public static ValueMismatch of(@Nullable Message expected, @Nullable Message actual, Message requested, int version) {
170+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
171+
172+
if (expected != null) {
173+
builder.setExpected(toAny(expected));
174+
}
175+
176+
if (actual != null) {
177+
builder.setActual(toAny(actual));
178+
}
179+
180+
final Any requestedAny = toAny(requested);
181+
182+
builder.setRequested(requestedAny);
183+
builder.setVersion(version);
184+
185+
return builder.build();
186+
}
187+
}

client/src/main/java/org/spine3/protobuf/Values.java

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package org.spine3.protobuf;
2121

22+
import com.google.protobuf.Any;
2223
import com.google.protobuf.BoolValue;
2324
import com.google.protobuf.DoubleValue;
2425
import com.google.protobuf.FloatValue;
@@ -37,7 +38,7 @@ public class Values {
3738
private Values() {}
3839

3940
/**
40-
* Creates a new StringValue wrapping the passed string.
41+
* Creates a new {@code StringValue} wrapping the passed string.
4142
*
4243
* @param value the value to wrap
4344
* @return a new StringValue instance
@@ -50,7 +51,15 @@ public static StringValue newStringValue(String value) {
5051
}
5152

5253
/**
53-
* Creates a new DoubleValue wrapping the passed number.
54+
* Packs the passed value into {@link Any}.
55+
*/
56+
public static Any pack(String value) {
57+
final Any result = Any.pack(newStringValue(value));
58+
return result;
59+
}
60+
61+
/**
62+
* Creates a new {@code DoubleValue} wrapping the passed number.
5463
*
5564
* @param value the value to wrap
5665
* @return a new DoubleValue instance
@@ -63,7 +72,15 @@ public static DoubleValue newDoubleValue(double value) {
6372
}
6473

6574
/**
66-
* Creates a new FloatValue wrapping the passed number.
75+
* Packs the passed value into {@link Any}.
76+
*/
77+
public static Any pack(double value) {
78+
final Any result = Any.pack(newDoubleValue(value));
79+
return result;
80+
}
81+
82+
/**
83+
* Creates a new {@code FloatValue} wrapping the passed number.
6784
*
6885
* @param value the value to wrap
6986
* @return a new FloatValue instance
@@ -76,7 +93,15 @@ public static FloatValue newFloatValue(float value) {
7693
}
7794

7895
/**
79-
* Creates a new Int32Value wrapping the passed number.
96+
* Packs the passed value into {@link Any}.
97+
*/
98+
public static Any pack(float value) {
99+
final Any result = Any.pack(newFloatValue(value));
100+
return result;
101+
}
102+
103+
/**
104+
* Creates a new {@code Int32Value} wrapping the passed number.
80105
*
81106
* @param value the value to wrap
82107
* @return a new Int32Value instance
@@ -89,7 +114,15 @@ public static Int32Value newIntegerValue(int value) {
89114
}
90115

91116
/**
92-
* Creates a new Int64Value wrapping the passed number.
117+
* Packs the passed value into {@link Any}.
118+
*/
119+
public static Any pack(int value) {
120+
final Any result = Any.pack(newIntegerValue(value));
121+
return result;
122+
}
123+
124+
/**
125+
* Creates a new {@code Int64Value} wrapping the passed number.
93126
*
94127
* @param value the value to wrap
95128
* @return a new Int64Value instance
@@ -102,7 +135,15 @@ public static Int64Value newLongValue(long value) {
102135
}
103136

104137
/**
105-
* Creates a new BoolValue wrapping the passed value.
138+
* Packs the passed value into {@link Any}.
139+
*/
140+
public static Any pack(long value) {
141+
final Any result = Any.pack(newLongValue(value));
142+
return result;
143+
}
144+
145+
/**
146+
* Creates a new {@code BoolValue} wrapping the passed value.
106147
*
107148
* @param value the value to wrap
108149
* @return a new BoolValue instance
@@ -113,4 +154,12 @@ public static BoolValue newBoolValue(boolean value) {
113154
.build();
114155
return result;
115156
}
157+
158+
/**
159+
* Packs the passed value into {@link Any}.
160+
*/
161+
public static Any pack(boolean value) {
162+
final Any result = Any.pack(newBoolValue(value));
163+
return result;
164+
}
116165
}

client/src/main/proto/spine/base/failure.proto

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,21 @@ message Failure {
5050
// Additional information on the failure.
5151
map<string, google.protobuf.Value> attributes = 4;
5252
}
53+
54+
// A failure to handle a command because an entity contains a value different than requested in a command.
55+
message ValueMismatch {
56+
// An expected value.
57+
// This field is not populated if the command expects to initialize the attribute.
58+
google.protobuf.Any expected = 1;
59+
60+
// An actual value in the entity.
61+
// This field is not populated if the entity does not have the attribute set.
62+
google.protobuf.Any actual = 2;
63+
64+
// An optional attribute that may contain a value requested as the replacement
65+
// for the `expected` value in the failed command.
66+
google.protobuf.Any requested = 3;
67+
68+
// A version of the entity, which generated the failure.
69+
int32 version = 4;
70+
}

0 commit comments

Comments
 (0)