|
36 | 36 | import com.google.protobuf.UInt64Value;
|
37 | 37 | import com.google.protobuf.Value;
|
38 | 38 | import dev.cel.common.annotations.Internal;
|
| 39 | +import java.util.Optional; |
39 | 40 | import java.util.function.Function;
|
40 |
| -import org.jspecify.annotations.Nullable; |
41 | 41 |
|
42 | 42 | /**
|
43 | 43 | * WellKnownProto types used throughout CEL. These types are specially handled to ensure that
|
|
46 | 46 | */
|
47 | 47 | @Internal
|
48 | 48 | public enum WellKnownProto {
|
49 |
| - ANY_VALUE("google.protobuf.Any", Any.class.getName()), |
50 |
| - DURATION("google.protobuf.Duration", Duration.class.getName()), |
51 |
| - JSON_LIST_VALUE("google.protobuf.ListValue", ListValue.class.getName()), |
52 |
| - JSON_STRUCT_VALUE("google.protobuf.Struct", Struct.class.getName()), |
53 |
| - JSON_VALUE("google.protobuf.Value", Value.class.getName()), |
54 |
| - TIMESTAMP("google.protobuf.Timestamp", Timestamp.class.getName()), |
| 49 | + ANY_VALUE("google.protobuf.Any", Any.class), |
| 50 | + DURATION("google.protobuf.Duration", Duration.class), |
| 51 | + JSON_LIST_VALUE("google.protobuf.ListValue", ListValue.class), |
| 52 | + JSON_STRUCT_VALUE("google.protobuf.Struct", Struct.class), |
| 53 | + JSON_VALUE("google.protobuf.Value", Value.class), |
| 54 | + TIMESTAMP("google.protobuf.Timestamp", Timestamp.class), |
55 | 55 | // Wrapper types
|
56 |
| - FLOAT_VALUE("google.protobuf.FloatValue", FloatValue.class.getName(), /* isWrapperType= */ true), |
57 |
| - INT32_VALUE("google.protobuf.Int32Value", Int32Value.class.getName(), /* isWrapperType= */ true), |
58 |
| - INT64_VALUE("google.protobuf.Int64Value", Int64Value.class.getName(), /* isWrapperType= */ true), |
59 |
| - STRING_VALUE( |
60 |
| - "google.protobuf.StringValue", StringValue.class.getName(), /* isWrapperType= */ true), |
61 |
| - BOOL_VALUE("google.protobuf.BoolValue", BoolValue.class.getName(), /* isWrapperType= */ true), |
62 |
| - BYTES_VALUE("google.protobuf.BytesValue", BytesValue.class.getName(), /* isWrapperType= */ true), |
63 |
| - DOUBLE_VALUE( |
64 |
| - "google.protobuf.DoubleValue", DoubleValue.class.getName(), /* isWrapperType= */ true), |
65 |
| - UINT32_VALUE( |
66 |
| - "google.protobuf.UInt32Value", UInt32Value.class.getName(), /* isWrapperType= */ true), |
67 |
| - UINT64_VALUE( |
68 |
| - "google.protobuf.UInt64Value", UInt64Value.class.getName(), /* isWrapperType= */ true), |
| 56 | + FLOAT_VALUE("google.protobuf.FloatValue", FloatValue.class, /* isWrapperType= */ true), |
| 57 | + INT32_VALUE("google.protobuf.Int32Value", Int32Value.class, /* isWrapperType= */ true), |
| 58 | + INT64_VALUE("google.protobuf.Int64Value", Int64Value.class, /* isWrapperType= */ true), |
| 59 | + STRING_VALUE("google.protobuf.StringValue", StringValue.class, /* isWrapperType= */ true), |
| 60 | + BOOL_VALUE("google.protobuf.BoolValue", BoolValue.class, /* isWrapperType= */ true), |
| 61 | + BYTES_VALUE("google.protobuf.BytesValue", BytesValue.class, /* isWrapperType= */ true), |
| 62 | + DOUBLE_VALUE("google.protobuf.DoubleValue", DoubleValue.class, /* isWrapperType= */ true), |
| 63 | + UINT32_VALUE("google.protobuf.UInt32Value", UInt32Value.class, /* isWrapperType= */ true), |
| 64 | + UINT64_VALUE("google.protobuf.UInt64Value", UInt64Value.class, /* isWrapperType= */ true), |
69 | 65 | // These aren't explicitly called out as wrapper types in the spec, but behave like one, because
|
70 | 66 | // they are still converted into an equivalent primitive type.
|
71 | 67 |
|
72 |
| - EMPTY("google.protobuf.Empty", Empty.class.getName(), /* isWrapperType= */ true), |
73 |
| - FIELD_MASK("google.protobuf.FieldMask", FieldMask.class.getName(), /* isWrapperType= */ true), |
| 68 | + EMPTY("google.protobuf.Empty", Empty.class, /* isWrapperType= */ true), |
| 69 | + FIELD_MASK("google.protobuf.FieldMask", FieldMask.class, /* isWrapperType= */ true), |
74 | 70 | ;
|
75 | 71 |
|
76 |
| - private static final ImmutableMap<String, WellKnownProto> WELL_KNOWN_PROTO_MAP; |
| 72 | + private static final ImmutableMap<String, WellKnownProto> TYPE_NAME_TO_WELL_KNOWN_PROTO_MAP = |
| 73 | + stream(WellKnownProto.values()) |
| 74 | + .collect(toImmutableMap(WellKnownProto::typeName, Function.identity())); |
77 | 75 |
|
78 |
| - static { |
79 |
| - WELL_KNOWN_PROTO_MAP = |
80 |
| - stream(WellKnownProto.values()) |
81 |
| - .collect(toImmutableMap(WellKnownProto::typeName, Function.identity())); |
82 |
| - } |
| 76 | + private static final ImmutableMap<Class<?>, WellKnownProto> |
| 77 | + CLASS_TO_NAME_TO_WELL_KNOWN_PROTO_MAP = |
| 78 | + stream(WellKnownProto.values()) |
| 79 | + .collect(toImmutableMap(WellKnownProto::messageClass, Function.identity())); |
83 | 80 |
|
84 |
| - private final String wellKnownProtoFullName; |
85 |
| - private final String javaClassName; |
| 81 | + private final String wellKnownProtoTypeName; |
| 82 | + private final Class<?> clazz; |
86 | 83 | private final boolean isWrapperType;
|
87 | 84 |
|
| 85 | + /** Gets the fully qualified prototype name (ex: google.protobuf.FloatValue) */ |
88 | 86 | public String typeName() {
|
89 |
| - return wellKnownProtoFullName; |
| 87 | + return wellKnownProtoTypeName; |
90 | 88 | }
|
91 | 89 |
|
92 |
| - public String javaClassName() { |
93 |
| - return this.javaClassName; |
| 90 | + /** Gets the underlying java class for this WellKnownProto. */ |
| 91 | + public Class<?> messageClass() { |
| 92 | + return clazz; |
94 | 93 | }
|
95 | 94 |
|
96 |
| - public static @Nullable WellKnownProto getByTypeName(String typeName) { |
97 |
| - return WELL_KNOWN_PROTO_MAP.get(typeName); |
| 95 | + public static Optional<WellKnownProto> getByTypeName(String typeName) { |
| 96 | + return Optional.ofNullable(TYPE_NAME_TO_WELL_KNOWN_PROTO_MAP.get(typeName)); |
98 | 97 | }
|
99 | 98 |
|
100 |
| - public static boolean isWrapperType(String typeName) { |
101 |
| - WellKnownProto wellKnownProto = getByTypeName(typeName); |
102 |
| - if (wellKnownProto == null) { |
103 |
| - return false; |
104 |
| - } |
| 99 | + public static Optional<WellKnownProto> getByClass(Class<?> clazz) { |
| 100 | + return Optional.ofNullable(CLASS_TO_NAME_TO_WELL_KNOWN_PROTO_MAP.get(clazz)); |
| 101 | + } |
105 | 102 |
|
106 |
| - return wellKnownProto.isWrapperType(); |
| 103 | + /** |
| 104 | + * Returns true if the provided {@code typeName} is a well known type, and it's a wrapper. False |
| 105 | + * otherwise. |
| 106 | + */ |
| 107 | + public static boolean isWrapperType(String typeName) { |
| 108 | + return getByTypeName(typeName).map(WellKnownProto::isWrapperType).orElse(false); |
107 | 109 | }
|
108 | 110 |
|
109 | 111 | public boolean isWrapperType() {
|
110 | 112 | return isWrapperType;
|
111 | 113 | }
|
112 | 114 |
|
113 |
| - WellKnownProto(String wellKnownProtoFullName, String javaClassName) { |
114 |
| - this(wellKnownProtoFullName, javaClassName, /* isWrapperType= */ false); |
| 115 | + WellKnownProto(String wellKnownProtoTypeName, Class<?> clazz) { |
| 116 | + this(wellKnownProtoTypeName, clazz, /* isWrapperType= */ false); |
115 | 117 | }
|
116 | 118 |
|
117 |
| - WellKnownProto(String wellKnownProtoFullName, String javaClassName, boolean isWrapperType) { |
118 |
| - this.wellKnownProtoFullName = wellKnownProtoFullName; |
119 |
| - this.javaClassName = javaClassName; |
| 119 | + WellKnownProto(String wellKnownProtoFullName, Class<?> clazz, boolean isWrapperType) { |
| 120 | + this.wellKnownProtoTypeName = wellKnownProtoFullName; |
| 121 | + this.clazz = clazz; |
120 | 122 | this.isWrapperType = isWrapperType;
|
121 | 123 | }
|
122 | 124 | }
|
0 commit comments