Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark payload invalid if any mandatory variable is not set #337

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
1 change: 1 addition & 0 deletions protos/passenger-count.proto
LofhJann marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ message Payload {
optional int32 stop = 15;
optional string route = 16;
optional VehicleCounts vehicleCounts = 17;
optional bool invalid = 18;
}

message VehicleCounts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public static PassengerCountParser newInstance() {

@NotNull
public Optional<PassengerCount.Payload> parsePayload(@NotNull ApcJson json) {
final PassengerCount.Payload payload = parsePassengerCountPayload(json);

if (payload.hasInvalid()) {
Copy link
Contributor

@LofhJann LofhJann Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer "isInvalid" instead of "hasInvalid" here, semantically makes more sense in this context.

Guess it's auto-generated by protobuf builder though so it might not be worth the hassle to change

return Optional.empty();
}

return Optional.of(payload);
}

@NotNull
public PassengerCount.Payload parsePassengerCountPayload(@NotNull ApcJson json) {
final Apc payload = json.apc;

// Required attributes
Expand All @@ -42,15 +53,15 @@ public Optional<PassengerCount.Payload> parsePayload(@NotNull ApcJson json) {

if (payload.oper == null) {
log.warn("Value for oper is null");
//oper value is mandatory -> return empty
return Optional.empty();
//oper value is mandatory -> this payload is not valid
payloadBuilder.setInvalid(true);
}
payloadBuilder.setOper(payload.oper);

if (payload.veh == null) {
log.warn("Value for veh is null");
//veh value is mandatory -> return empty
return Optional.empty();
//veh value is mandatory -> this payload is not valid
payloadBuilder.setInvalid(true);
}
payloadBuilder.setVeh(payload.veh);

Expand Down Expand Up @@ -104,7 +115,7 @@ public Optional<PassengerCount.Payload> parsePayload(@NotNull ApcJson json) {

if (payload.vehiclecounts == null) {
log.warn("Field 'vehiclecounts' is null for vehicle {}/{}", payload.oper, payload.veh);
return Optional.empty();
payloadBuilder.setInvalid(true);
}

PassengerCount.VehicleCounts.Builder vehicleBuilder = PassengerCount.VehicleCounts.newBuilder();
Expand Down Expand Up @@ -138,7 +149,7 @@ public Optional<PassengerCount.Payload> parsePayload(@NotNull ApcJson json) {
}

payloadBuilder.setVehicleCounts(vehicleBuilder);
return Optional.of(payloadBuilder.build());
return payloadBuilder.build();
}

@NotNull
Expand Down
121 changes: 111 additions & 10 deletions src/main/java/fi/hsl/common/passengercount/proto/PassengerCount.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.