Search in sources :

Example 1 with Root_meta_pure_functions_lang_KeyValue_Impl

use of org.finos.legend.pure.generated.Root_meta_pure_functions_lang_KeyValue_Impl in project legend-pure by finos.

the class JsonNativeImplementation method _fromJson.

public static <T> T _fromJson(String json, Class<T> clazz, String _typeKeyName, boolean _failOnUnknownProperties, SourceInformation si, ExecutionSupport es, ConstraintsOverride constraintsHandler, RichIterable<? extends Pair<? extends String, ? extends String>> _typeLookup) {
    java.lang.Class c;
    String targetClassName = null;
    try {
        targetClassName = JavaPackageAndImportBuilder.platformJavaPackage() + ".Root_" + platform_pure_corefunctions_meta.Root_meta_pure_functions_meta_elementToPath_PackageableElement_1__String_1__String_1_(clazz, "_", es);
        c = ((CompiledExecutionSupport) es).getClassLoader().loadClass(targetClassName);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to find  class " + targetClassName, e);
    }
    Map<String, Class> typeLookup = new HashMap<String, Class>();
    for (Pair<? extends String, ? extends String> pair : _typeLookup) {
        typeLookup.put(pair._first(), ((CompiledExecutionSupport) es).getMetadataAccessor().getClass("Root::" + pair._second()));
    }
    return (T) JsonDeserializer.fromJson(json, (Class<? extends Any>) clazz, new JsonDeserializationContext(new JsonDeserializationCache(), si, ((CompiledExecutionSupport) es).getProcessorSupport(), _typeKeyName, typeLookup, _failOnUnknownProperties, new ObjectFactory() {

        public <U extends Any> U newObject(Class<U> clazz, Map<String, RichIterable<?>> properties) {
            FastList<KeyValue> keyValues = new FastList<>();
            for (Map.Entry<String, RichIterable<?>> property : properties.entrySet()) {
                KeyValue keyValue = new Root_meta_pure_functions_lang_KeyValue_Impl("Anonymous");
                keyValue._key(property.getKey());
                for (Object value : property.getValue()) {
                    keyValue._valueAdd(value);
                }
                keyValues.add(keyValue);
            }
            U result = (U) Pure.newObject(clazz, keyValues, null, null, null, null, null, null, es);
            result._elementOverride(constraintsHandler);
            return (U) handleValidation(true, result, si, es);
        }

        public <T extends Any> T newUnitInstance(CoreInstance propertyType, String unitTypeString, Number unitValue) throws Exception {
            CoreInstance unitRetrieved = ((CompiledExecutionSupport) es).getProcessorSupport().package_getByUserPath(unitTypeString);
            if (!((CompiledExecutionSupport) es).getProcessorSupport().type_subTypeOf(unitRetrieved, propertyType)) {
                throw new PureExecutionException("Cannot match unit type: " + unitTypeString + " as subtype of type: " + PackageableElement.getUserPathForPackageableElement(propertyType));
            }
            String unitClassName = UnitProcessor.convertToJavaCompatibleClassName(JavaPackageAndImportBuilder.buildImplUnitInstanceClassNameFromType(unitRetrieved));
            java.lang.Class c = ((CompiledExecutionSupport) es).getClassLoader().loadClass("org.finos.legend.pure.generated." + unitClassName);
            java.lang.Class[] paramClasses = new java.lang.Class[] { String.class, ExecutionSupport.class };
            Method method = c.getMethod("_val", Number.class);
            Object classInstance = c.getConstructor(paramClasses).newInstance("Anonymous_NoCounter", es);
            method.invoke(classInstance, unitValue);
            return (T) classInstance;
        }
    }));
}
Also used : KeyValue(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyValue) HashMap(java.util.HashMap) Any(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Any) JsonDeserializationContext(org.finos.legend.pure.runtime.java.extension.external.json.shared.JsonDeserializationContext) ObjectFactory(org.finos.legend.pure.runtime.java.extension.external.shared.conversion.ObjectFactory) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) FastList(org.eclipse.collections.impl.list.mutable.FastList) Root_meta_pure_functions_lang_KeyValue_Impl(org.finos.legend.pure.generated.Root_meta_pure_functions_lang_KeyValue_Impl) Method(java.lang.reflect.Method) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) RichIterable(org.eclipse.collections.api.RichIterable) JsonDeserializationCache(org.finos.legend.pure.runtime.java.extension.external.json.shared.JsonDeserializationCache) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Class(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)

Aggregations

Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 RichIterable (org.eclipse.collections.api.RichIterable)1 FastList (org.eclipse.collections.impl.list.mutable.FastList)1 Root_meta_pure_functions_lang_KeyValue_Impl (org.finos.legend.pure.generated.Root_meta_pure_functions_lang_KeyValue_Impl)1 KeyValue (org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyValue)1 Any (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Any)1 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)1 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)1 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)1 CompiledExecutionSupport (org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport)1 JsonDeserializationCache (org.finos.legend.pure.runtime.java.extension.external.json.shared.JsonDeserializationCache)1 JsonDeserializationContext (org.finos.legend.pure.runtime.java.extension.external.json.shared.JsonDeserializationContext)1 ObjectFactory (org.finos.legend.pure.runtime.java.extension.external.shared.conversion.ObjectFactory)1