16
16
17
17
import com .google .common .collect .ImmutableSet ;
18
18
import com .google .errorprone .annotations .Immutable ;
19
+ import com .google .protobuf .MessageLite ;
20
+ import dev .cel .common .internal .CelLiteDescriptorPool ;
19
21
import dev .cel .common .internal .DefaultLiteDescriptorPool ;
20
22
import dev .cel .protobuf .CelLiteDescriptor ;
23
+ import dev .cel .protobuf .CelLiteDescriptor .MessageLiteDescriptor ;
21
24
import java .util .Map ;
22
25
import java .util .Optional ;
23
26
import java .util .Set ;
30
33
*/
31
34
@ Immutable
32
35
public class ProtoMessageLiteValueProvider implements CelValueProvider {
36
+ private final CelLiteDescriptorPool descriptorPool ;
33
37
private final ProtoLiteCelValueConverter protoLiteCelValueConverter ;
34
38
35
39
public ProtoLiteCelValueConverter getProtoLiteCelValueConverter () {
@@ -38,7 +42,17 @@ public ProtoLiteCelValueConverter getProtoLiteCelValueConverter() {
38
42
39
43
@ Override
40
44
public Optional <CelValue > newValue (String structType , Map <String , Object > fields ) {
41
- throw new UnsupportedOperationException ("Message creation is not supported yet." );
45
+ MessageLiteDescriptor descriptor = descriptorPool .findDescriptor (structType ).orElse (null );
46
+ if (descriptor == null ) {
47
+ return Optional .empty ();
48
+ }
49
+
50
+ if (!fields .isEmpty ()) {
51
+ throw new UnsupportedOperationException ("Message creation with prepopulated fields is not supported yet." );
52
+ }
53
+
54
+ MessageLite message = descriptor .newMessageBuilder ().build ();
55
+ return Optional .of (protoLiteCelValueConverter .fromProtoMessageToCelValue (structType , message ));
42
56
}
43
57
44
58
@@ -51,11 +65,13 @@ public static ProtoMessageLiteValueProvider newInstance(Set<CelLiteDescriptor> d
51
65
DefaultLiteDescriptorPool descriptorPool = DefaultLiteDescriptorPool .newInstance (ImmutableSet .copyOf (descriptors ));
52
66
ProtoLiteCelValueConverter protoLiteCelValueConverter =
53
67
ProtoLiteCelValueConverter .newInstance (descriptorPool );
54
- return new ProtoMessageLiteValueProvider (protoLiteCelValueConverter );
68
+ return new ProtoMessageLiteValueProvider (protoLiteCelValueConverter , descriptorPool );
55
69
}
56
70
57
71
private ProtoMessageLiteValueProvider (
58
- ProtoLiteCelValueConverter protoLiteCelValueConverter ) {
72
+ ProtoLiteCelValueConverter protoLiteCelValueConverter ,
73
+ CelLiteDescriptorPool descriptorPool ) {
59
74
this .protoLiteCelValueConverter = protoLiteCelValueConverter ;
75
+ this .descriptorPool = descriptorPool ;
60
76
}
61
77
}
0 commit comments