1414
1515package build .buf .protovalidate ;
1616
17+ import com .google .protobuf .Descriptors ;
18+ import com .google .protobuf .Message ;
1719import java .util .HashSet ;
1820import java .util .Set ;
1921import java .util .regex .Pattern ;
2325import org .projectnessie .cel .common .types .ListT ;
2426import org .projectnessie .cel .common .types .StringT ;
2527import org .projectnessie .cel .common .types .Types ;
28+ import org .projectnessie .cel .common .types .pb .DefaultTypeAdapter ;
2629import org .projectnessie .cel .common .types .ref .TypeEnum ;
2730import org .projectnessie .cel .common .types .ref .Val ;
2831import org .projectnessie .cel .common .types .traits .Lister ;
3134/** Defines custom function overloads (the implementation). */
3235final class CustomOverload {
3336
37+ private static final String OVERLOAD_GET_FIELD = "getField" ;
3438 private static final String OVERLOAD_FORMAT = "format" ;
3539 private static final String OVERLOAD_UNIQUE = "unique" ;
3640 private static final String OVERLOAD_STARTS_WITH = "startsWith" ;
@@ -58,6 +62,7 @@ final class CustomOverload {
5862 */
5963 static Overload [] create () {
6064 return new Overload [] {
65+ celGetField (),
6166 celFormat (),
6267 celUnique (),
6368 celStartsWith (),
@@ -75,6 +80,30 @@ static Overload[] create() {
7580 };
7681 }
7782
83+ /**
84+ * Creates a custom function overload for the "getField" operation.
85+ *
86+ * @return The {@link Overload} instance for the "getField" operation.
87+ */
88+ private static Overload celGetField () {
89+ return Overload .binary (
90+ OVERLOAD_GET_FIELD ,
91+ (msgarg , namearg ) -> {
92+ if (msgarg .type ().typeEnum () != TypeEnum .Object
93+ || namearg .type ().typeEnum () != TypeEnum .String ) {
94+ return Err .newErr ("no such overload" );
95+ }
96+ Message message = msgarg .convertToNative (Message .class );
97+ String fieldName = namearg .convertToNative (String .class );
98+ Descriptors .FieldDescriptor field =
99+ message .getDescriptorForType ().findFieldByName (fieldName );
100+ if (field == null ) {
101+ return Err .newErr ("no such field: " + fieldName );
102+ }
103+ return DefaultTypeAdapter .Instance .nativeToValue (message .getField (field ));
104+ });
105+ }
106+
78107 /**
79108 * Creates a custom binary function overload for the "format" operation.
80109 *
0 commit comments