|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 The Authors of CEL-Java |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.projectnessie.cel.types.jackson3; |
| 17 | + |
| 18 | +import static org.projectnessie.cel.common.types.Err.newErr; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import org.projectnessie.cel.common.types.ref.FieldType; |
| 23 | +import org.projectnessie.cel.common.types.ref.Type; |
| 24 | +import org.projectnessie.cel.common.types.ref.TypeAdapterSupport; |
| 25 | +import org.projectnessie.cel.common.types.ref.TypeRegistry; |
| 26 | +import org.projectnessie.cel.common.types.ref.Val; |
| 27 | +import tools.jackson.databind.JavaType; |
| 28 | +import tools.jackson.databind.ObjectMapper; |
| 29 | +import tools.jackson.databind.ValueSerializer; |
| 30 | +import tools.jackson.databind.cfg.GeneratorSettings; |
| 31 | +import tools.jackson.databind.cfg.SerializationContexts; |
| 32 | +import tools.jackson.databind.json.JsonMapper; |
| 33 | +import tools.jackson.databind.ser.SerializationContextExt; |
| 34 | +import tools.jackson.databind.ser.jdk.EnumSerializer; |
| 35 | +import tools.jackson.databind.type.TypeFactory; |
| 36 | + |
| 37 | +/** |
| 38 | + * CEL-Java {@link TypeRegistry} to use Jackson 3 objects as input values for CEL scripts. |
| 39 | + * |
| 40 | + * <p>The implementation does not support the construction of Jackson objects in CEL expressions and |
| 41 | + * therefore returning Jackson objects from CEL expressions is not possible/implemented and results |
| 42 | + * in {@link UnsupportedOperationException}s. |
| 43 | + */ |
| 44 | +public final class Jackson3Registry implements TypeRegistry { |
| 45 | + final ObjectMapper objectMapper; |
| 46 | + private final SerializationContextExt serializationContextExt; |
| 47 | + private final TypeFactory typeFactory; |
| 48 | + private final Map<Class<?>, JacksonTypeDescription> knownTypes = new HashMap<>(); |
| 49 | + private final Map<String, JacksonTypeDescription> knownTypesByName = new HashMap<>(); |
| 50 | + |
| 51 | + private final Map<Class<?>, JacksonEnumDescription> enumMap = new HashMap<>(); |
| 52 | + private final Map<String, JacksonEnumValue> enumValues = new HashMap<>(); |
| 53 | + |
| 54 | + private Jackson3Registry() { |
| 55 | + JsonMapper.Builder b = JsonMapper.builder(); |
| 56 | + SerializationContexts serializationContexts = b.serializationContexts(); |
| 57 | + this.objectMapper = b.build(); |
| 58 | + SerializationContexts forMapper = |
| 59 | + serializationContexts.forMapper( |
| 60 | + objectMapper, |
| 61 | + objectMapper.serializationConfig(), |
| 62 | + objectMapper.tokenStreamFactory(), |
| 63 | + b.serializerFactory()); |
| 64 | + this.serializationContextExt = |
| 65 | + forMapper.createContext(objectMapper.serializationConfig(), GeneratorSettings.empty()); |
| 66 | + this.typeFactory = objectMapper.getTypeFactory(); |
| 67 | + } |
| 68 | + |
| 69 | + public static TypeRegistry newRegistry() { |
| 70 | + return new Jackson3Registry(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public TypeRegistry copy() { |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void register(Object t) { |
| 80 | + Class<?> cls = t instanceof Class ? (Class<?>) t : t.getClass(); |
| 81 | + typeDescription(cls); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void registerType(Type... types) { |
| 86 | + throw new UnsupportedOperationException(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Val enumValue(String enumName) { |
| 91 | + JacksonEnumValue enumVal = enumValues.get(enumName); |
| 92 | + if (enumVal == null) { |
| 93 | + return newErr("unknown enum name '%s'", enumName); |
| 94 | + } |
| 95 | + return enumVal.ordinalValue(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Val findIdent(String identName) { |
| 100 | + JacksonTypeDescription td = knownTypesByName.get(identName); |
| 101 | + if (td != null) { |
| 102 | + return td.type(); |
| 103 | + } |
| 104 | + |
| 105 | + JacksonEnumValue enumVal = enumValues.get(identName); |
| 106 | + if (enumVal != null) { |
| 107 | + return enumVal.ordinalValue(); |
| 108 | + } |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public com.google.api.expr.v1alpha1.Type findType(String typeName) { |
| 114 | + JacksonTypeDescription td = knownTypesByName.get(typeName); |
| 115 | + if (td == null) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + return td.pbType(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public FieldType findFieldType(String messageType, String fieldName) { |
| 123 | + JacksonTypeDescription td = knownTypesByName.get(messageType); |
| 124 | + if (td == null) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + return td.fieldType(fieldName); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Val newValue(String typeName, Map<String, Val> fields) { |
| 132 | + throw new UnsupportedOperationException(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Val nativeToValue(Object value) { |
| 137 | + if (value instanceof Val) { |
| 138 | + return (Val) value; |
| 139 | + } |
| 140 | + Val maybe = TypeAdapterSupport.maybeNativeToValue(this, value); |
| 141 | + if (maybe != null) { |
| 142 | + return maybe; |
| 143 | + } |
| 144 | + |
| 145 | + if (value instanceof Enum) { |
| 146 | + String fq = JacksonEnumValue.fullyQualifiedName((Enum<?>) value); |
| 147 | + JacksonEnumValue v = enumValues.get(fq); |
| 148 | + if (v == null) { |
| 149 | + return newErr("unknown enum name '%s'", fq); |
| 150 | + } |
| 151 | + return v.ordinalValue(); |
| 152 | + } |
| 153 | + |
| 154 | + try { |
| 155 | + return JacksonObjectT.newObject(this, value, typeDescription(value.getClass())); |
| 156 | + } catch (Exception e) { |
| 157 | + throw new RuntimeException("oops", e); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + JacksonEnumDescription enumDescription(Class<?> clazz) { |
| 162 | + if (!Enum.class.isAssignableFrom(clazz)) { |
| 163 | + throw new IllegalArgumentException("only enum allowed here"); |
| 164 | + } |
| 165 | + |
| 166 | + JacksonEnumDescription ed = enumMap.get(clazz); |
| 167 | + if (ed != null) { |
| 168 | + return ed; |
| 169 | + } |
| 170 | + ed = computeEnumDescription(clazz); |
| 171 | + enumMap.put(clazz, ed); |
| 172 | + return ed; |
| 173 | + } |
| 174 | + |
| 175 | + private JacksonEnumDescription computeEnumDescription(Class<?> clazz) { |
| 176 | + ValueSerializer<?> ser = serializationContextExt.findValueSerializer(clazz); |
| 177 | + JavaType javaType = typeFactory.constructType(clazz); |
| 178 | + |
| 179 | + JacksonEnumDescription enumDesc = new JacksonEnumDescription(javaType, (EnumSerializer) ser); |
| 180 | + enumMap.put(clazz, enumDesc); |
| 181 | + |
| 182 | + enumDesc.buildValues().forEach(v -> enumValues.put(v.fullyQualifiedName(), v)); |
| 183 | + |
| 184 | + return enumDesc; |
| 185 | + } |
| 186 | + |
| 187 | + JacksonTypeDescription typeDescription(Class<?> clazz) { |
| 188 | + if (Enum.class.isAssignableFrom(clazz)) { |
| 189 | + throw new IllegalArgumentException("enum not allowed here"); |
| 190 | + } |
| 191 | + |
| 192 | + JacksonTypeDescription td = knownTypes.get(clazz); |
| 193 | + if (td != null) { |
| 194 | + return td; |
| 195 | + } |
| 196 | + td = computeTypeDescription(clazz); |
| 197 | + knownTypes.put(clazz, td); |
| 198 | + return td; |
| 199 | + } |
| 200 | + |
| 201 | + private JacksonTypeDescription computeTypeDescription(Class<?> clazz) { |
| 202 | + ValueSerializer<Object> ser = serializationContextExt.findValueSerializer(clazz); |
| 203 | + JavaType javaType = typeFactory.constructType(clazz); |
| 204 | + |
| 205 | + JacksonTypeDescription typeDesc = new JacksonTypeDescription(javaType, ser, this::typeQuery); |
| 206 | + knownTypesByName.put(clazz.getName(), typeDesc); |
| 207 | + |
| 208 | + return typeDesc; |
| 209 | + } |
| 210 | + |
| 211 | + private com.google.api.expr.v1alpha1.Type typeQuery(JavaType javaType) { |
| 212 | + if (javaType.isEnumType()) { |
| 213 | + return enumDescription(javaType.getRawClass()).pbType(); |
| 214 | + } |
| 215 | + return typeDescription(javaType.getRawClass()).pbType(); |
| 216 | + } |
| 217 | +} |
0 commit comments