Skip to content

Commit bb31fd8

Browse files
authored
feat: add grpc-validation-utils module (#41)
1 parent 27870c2 commit bb31fd8

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
`java-library`
3+
}
4+
5+
dependencies {
6+
api(project(":grpc-context-utils"))
7+
api("io.grpc:grpc-api")
8+
implementation("com.google.protobuf:protobuf-java-util:3.21.7")
9+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.hypertrace.core.grpcutils.validation;
2+
3+
import com.google.protobuf.Descriptors.FieldDescriptor;
4+
import com.google.protobuf.Message;
5+
import com.google.protobuf.util.JsonFormat;
6+
import io.grpc.Status;
7+
import org.hypertrace.core.grpcutils.context.RequestContext;
8+
9+
public class GrpcValidatorUtils {
10+
private static final JsonFormat.Printer JSON_PRINTER = JsonFormat.printer();
11+
12+
private GrpcValidatorUtils() {}
13+
14+
public static void validateRequestContextOrThrow(RequestContext requestContext) {
15+
if (requestContext.getTenantId().isEmpty()) {
16+
throw Status.INVALID_ARGUMENT
17+
.withDescription("Missing expected Tenant ID")
18+
.asRuntimeException();
19+
}
20+
}
21+
22+
public static <T extends Message> void validateNonDefaultPresenceOrThrow(
23+
T source, int fieldNumber) {
24+
FieldDescriptor descriptor = source.getDescriptorForType().findFieldByNumber(fieldNumber);
25+
26+
if (descriptor.isRepeated()) {
27+
validateNonDefaultPresenceRepeatedOrThrow(source, descriptor);
28+
} else if (!source.hasField(descriptor)
29+
|| source.getField(descriptor).equals(descriptor.getDefaultValue())) {
30+
throw Status.INVALID_ARGUMENT
31+
.withDescription(
32+
String.format(
33+
"Expected field value %s but not present:%n %s",
34+
descriptor.getFullName(), printMessage(source)))
35+
.asRuntimeException();
36+
}
37+
}
38+
39+
private static <T extends Message> void validateNonDefaultPresenceRepeatedOrThrow(
40+
T source, FieldDescriptor descriptor) {
41+
if (source.getRepeatedFieldCount(descriptor) == 0) {
42+
throw Status.INVALID_ARGUMENT
43+
.withDescription(
44+
String.format(
45+
"Expected at least 1 value for repeated field %s but not present:%n %s",
46+
descriptor.getFullName(), printMessage(source)))
47+
.asRuntimeException();
48+
}
49+
}
50+
51+
public static String printMessage(Message message) {
52+
try {
53+
return JSON_PRINTER.print(message);
54+
} catch (Exception exception) {
55+
return message.toString();
56+
}
57+
}
58+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ include(":grpc-client-rx-utils")
1717
include(":grpc-server-rx-utils")
1818
include(":grpc-context-utils")
1919
include(":grpc-server-utils")
20+
include(":grpc-validation-utils")

0 commit comments

Comments
 (0)