Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/main/java/build/buf/protovalidate/CustomDeclarations.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ static List<Decl> create() {
// Add 'now' variable declaration
decls.add(Decls.newVar("now", Decls.newObjectType(TimestampT.TimestampType.typeName())));

// Add 'getField' function declaration
decls.add(
Decls.newFunction(
"getField",
Decls.newOverload(
"get_field_any_string", Arrays.asList(Decls.Any, Decls.String), Decls.Any)));

// Add 'isIp' function declaration
decls.add(
Decls.newFunction(
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/build/buf/protovalidate/CustomOverload.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.google.common.base.Splitter;
import com.google.common.net.InetAddresses;
import com.google.common.primitives.Bytes;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import jakarta.mail.internet.AddressException;
Expand All @@ -35,6 +37,7 @@
import org.projectnessie.cel.common.types.ListT;
import org.projectnessie.cel.common.types.StringT;
import org.projectnessie.cel.common.types.Types;
import org.projectnessie.cel.common.types.pb.DefaultTypeAdapter;
import org.projectnessie.cel.common.types.ref.TypeEnum;
import org.projectnessie.cel.common.types.ref.Val;
import org.projectnessie.cel.common.types.traits.Lister;
Expand All @@ -43,6 +46,7 @@
/** Defines custom function overloads (the implementation). */
final class CustomOverload {

private static final String OVERLOAD_GET_FIELD = "getField";
private static final String OVERLOAD_FORMAT = "format";
private static final String OVERLOAD_UNIQUE = "unique";
private static final String OVERLOAD_STARTS_WITH = "startsWith";
Expand All @@ -65,6 +69,7 @@ final class CustomOverload {
*/
static Overload[] create() {
return new Overload[] {
getField(),
format(),
unique(),
startsWith(),
Expand All @@ -82,6 +87,30 @@ static Overload[] create() {
};
}

/**
* Creates a custom function overload for the "getField" operation.
*
* @return The {@link Overload} instance for the "getField" operation.
*/
private static Overload getField() {
return Overload.binary(
OVERLOAD_GET_FIELD,
(msgarg, namearg) -> {
if (msgarg.type().typeEnum() != TypeEnum.Object
|| namearg.type().typeEnum() != TypeEnum.String) {
return Err.newErr("no such overload");
}
Message message = msgarg.convertToNative(Message.class);
String fieldName = namearg.convertToNative(String.class);
Descriptors.FieldDescriptor field =
message.getDescriptorForType().findFieldByName(fieldName);
if (field == null) {
return Err.newErr("no such field: " + fieldName);
}
return DefaultTypeAdapter.Instance.nativeToValue(message.getField(field));
});
}

/**
* Creates a custom binary function overload for the "format" operation.
*
Expand Down