-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
46 changed files
with
3,672 additions
and
805 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
common/src/main/java/dev/cel/common/internal/CelLiteDescriptorPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dev.cel.common.internal; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.errorprone.annotations.Immutable; | ||
import com.google.protobuf.MessageLite; | ||
import dev.cel.common.annotations.Internal; | ||
import dev.cel.protobuf.CelLiteDescriptor; | ||
import dev.cel.protobuf.CelLiteDescriptor.FieldDescriptor; | ||
import dev.cel.protobuf.CelLiteDescriptor.MessageDescriptor; | ||
import java.util.Optional; | ||
|
||
/** Descriptor pool for {@link CelLiteDescriptor}s. */ | ||
@Immutable | ||
@Internal | ||
public final class CelLiteDescriptorPool { | ||
private final ImmutableMap<String, MessageDescriptor> protoFqnToMessageInfo; | ||
private final ImmutableMap<String, MessageDescriptor> protoJavaClassNameToMessageInfo; | ||
|
||
public static CelLiteDescriptorPool newInstance(ImmutableSet<CelLiteDescriptor> descriptors) { | ||
return new CelLiteDescriptorPool(descriptors); | ||
} | ||
|
||
public Optional<MessageDescriptor> findDescriptorByTypeName(String protoFqn) { | ||
return Optional.ofNullable(protoFqnToMessageInfo.get(protoFqn)); | ||
} | ||
|
||
public Optional<MessageDescriptor> findDescriptor(MessageLite msg) { | ||
String className = msg.getClass().getName(); | ||
return Optional.ofNullable(protoJavaClassNameToMessageInfo.get(className)); | ||
} | ||
|
||
private static MessageDescriptor newMessageInfo(WellKnownProto wellKnownProto) { | ||
ImmutableMap.Builder<String, FieldDescriptor> fieldInfoMap = ImmutableMap.builder(); | ||
switch (wellKnownProto) { | ||
case JSON_STRUCT_VALUE: | ||
fieldInfoMap.put( | ||
"fields", | ||
new FieldDescriptor( | ||
"google.protobuf.Struct.fields", | ||
"MESSAGE", | ||
"Fields", | ||
FieldDescriptor.ValueType.MAP.toString(), | ||
FieldDescriptor.Type.MESSAGE.toString(), | ||
String.valueOf(false), | ||
"com.google.protobuf.Struct$FieldsEntry", | ||
"google.protobuf.Struct.FieldsEntry")); | ||
break; | ||
case BOOL_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.BoolValue", | ||
"BOOLEAN", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.BOOL)); | ||
break; | ||
case BYTES_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.BytesValue", | ||
"BYTE_STRING", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.BYTES)); | ||
break; | ||
case DOUBLE_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.DoubleValue", | ||
"DOUBLE", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.DOUBLE)); | ||
break; | ||
case FLOAT_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.FloatValue", | ||
"FLOAT", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.FLOAT)); | ||
break; | ||
case INT32_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.Int32Value", | ||
"INT", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.INT32)); | ||
break; | ||
case INT64_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.Int64Value", | ||
"LONG", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.INT64)); | ||
break; | ||
case STRING_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.StringValue", | ||
"STRING", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.STRING)); | ||
break; | ||
case UINT32_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.UInt32Value", | ||
"INT", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.UINT32)); | ||
break; | ||
case UINT64_VALUE: | ||
fieldInfoMap.put( | ||
"value", | ||
newPrimitiveFieldInfo( | ||
"google.protobuf.UInt64Value", | ||
"LONG", | ||
FieldDescriptor.ValueType.SCALAR, | ||
FieldDescriptor.Type.UINT64)); | ||
break; | ||
case JSON_VALUE: | ||
case JSON_LIST_VALUE: | ||
case DURATION_VALUE: | ||
case TIMESTAMP_VALUE: | ||
// TODO: Complete these | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return new MessageDescriptor( | ||
wellKnownProto.typeName(), wellKnownProto.javaClassName(), fieldInfoMap.buildOrThrow()); | ||
} | ||
|
||
private static FieldDescriptor newPrimitiveFieldInfo( | ||
String fullyQualifiedProtoName, | ||
String javaTypeName, | ||
FieldDescriptor.ValueType valueType, | ||
FieldDescriptor.Type protoFieldType) { | ||
return new FieldDescriptor( | ||
fullyQualifiedProtoName + ".value", | ||
javaTypeName, | ||
"Value", | ||
valueType.toString(), | ||
protoFieldType.toString(), | ||
String.valueOf(false), | ||
"", | ||
fullyQualifiedProtoName); | ||
} | ||
|
||
private CelLiteDescriptorPool(ImmutableSet<CelLiteDescriptor> descriptors) { | ||
ImmutableMap.Builder<String, MessageDescriptor> protoFqnMapBuilder = ImmutableMap.builder(); | ||
ImmutableMap.Builder<String, MessageDescriptor> protoJavaClassNameMapBuilder = | ||
ImmutableMap.builder(); | ||
for (WellKnownProto wellKnownProto : WellKnownProto.values()) { | ||
MessageDescriptor wktMessageInfo = newMessageInfo(wellKnownProto); | ||
protoFqnMapBuilder.put(wellKnownProto.typeName(), wktMessageInfo); | ||
protoJavaClassNameMapBuilder.put(wellKnownProto.javaClassName(), wktMessageInfo); | ||
} | ||
|
||
for (CelLiteDescriptor descriptor : descriptors) { | ||
protoFqnMapBuilder.putAll(descriptor.getProtoTypeNamesToDescriptors()); | ||
protoJavaClassNameMapBuilder.putAll(descriptor.getProtoJavaClassNameToDescriptors()); | ||
} | ||
|
||
this.protoFqnToMessageInfo = protoFqnMapBuilder.buildOrThrow(); | ||
this.protoJavaClassNameToMessageInfo = protoJavaClassNameMapBuilder.buildOrThrow(); | ||
} | ||
} |
Oops, something went wrong.