use of org.finos.legend.pure.m4.coreinstance.primitive.BooleanCoreInstance in project legend-pure by finos.
the class FromJson method execute.
@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, final Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, final Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, final VariableContext variableContext, final CoreInstance functionExpressionToUseInStack, final Profiler profiler, final InstantiationContext instantiationContext, final ExecutionSupport executionSupport, final Context context, final ProcessorSupport processorSupport) throws PureExecutionException {
final SourceInformation si = functionExpressionToUseInStack.getSourceInformation();
Class startingClass = (Class) Instance.getValueForMetaPropertyToOneResolved(params.get(1), M3Properties.values, processorSupport);
CoreInstance config = Instance.getValueForMetaPropertyToOneResolved(params.get(2), M3Properties.values, processorSupport);
String typeKeyName = ((StringCoreInstance) config.getValueForMetaPropertyToOne("typeKeyName")).getValue();
Map<String, Class> keyLookup = new UnifiedMap<String, Class>();
for (CoreInstance o : config.getValueForMetaPropertyToMany("typeLookup")) {
keyLookup.put(o.getValueForMetaPropertyToOne("first").getName(), (Class) _Package.getByUserPath(o.getValueForMetaPropertyToOne("second").getName(), functionExecution.getProcessorSupport()));
}
Boolean failOnUnknownProperties = ((BooleanCoreInstance) config.getValueForMetaPropertyToOne("failOnUnknownProperties")).getValue();
final ConstraintsOverride constraintsOverride = (ConstraintsOverride) config.getValueForMetaPropertyToOne("constraintsHandler");
String jsonText = PrimitiveUtilities.getStringValue(Instance.getValueForMetaPropertyToOneResolved(params.get(0), M3Properties.values, processorSupport));
return JsonDeserializer.fromJson(jsonText, startingClass, new JsonDeserializationContext(new JsonDeserializationCache(), si, processorSupport, typeKeyName, keyLookup, failOnUnknownProperties, new ObjectFactory() {
@Override
public <T extends Any> T newObject(Class<T> clazz, Map<String, RichIterable<?>> properties) {
CoreInstance instance = FromJson.this.repository.newEphemeralAnonymousCoreInstance(functionExpressionToUseInStack.getSourceInformation(), clazz);
for (Entry<String, RichIterable<?>> eachKeyValue : properties.entrySet()) {
FastList<CoreInstance> values = new FastList<>();
for (Object eachValue : eachKeyValue.getValue()) {
if (eachValue instanceof String) {
values.add(FromJson.this.repository.newStringCoreInstance((String) eachValue));
} else if (eachValue instanceof Boolean) {
values.add(FromJson.this.repository.newBooleanCoreInstance((Boolean) eachValue));
} else if (eachValue instanceof Integer) {
values.add(FromJson.this.repository.newIntegerCoreInstance((int) eachValue));
} else if (eachValue instanceof Long) {
values.add(FromJson.this.repository.newIntegerCoreInstance((long) eachValue));
} else if (eachValue instanceof BigDecimal) {
values.add(FromJson.this.repository.newDecimalCoreInstance((BigDecimal) eachValue));
} else if (eachValue instanceof Double || eachValue instanceof Number) {
values.add(FromJson.this.repository.newFloatCoreInstance(BigDecimal.valueOf((Double) eachValue)));
} else if (eachValue instanceof Year || eachValue instanceof YearMonth) {
values.add(FromJson.this.repository.newDateCoreInstance((PureDate) eachValue));
} else if (eachValue instanceof StrictDate) {
values.add(FromJson.this.repository.newStrictDateCoreInstance((PureDate) eachValue));
} else if (eachValue instanceof DateTime || eachValue instanceof LatestDate || eachValue instanceof PureDate) {
values.add(FromJson.this.repository.newDateTimeCoreInstance((PureDate) eachValue));
} else if (eachValue instanceof InstanceValue) {
InstanceValue asInstanceValue = (InstanceValue) eachValue;
if (Measure.isUnitOrMeasureInstance(asInstanceValue, processorSupport)) {
values.add(asInstanceValue);
} else {
values.add((CoreInstance) asInstanceValue._values().getFirst());
}
} else if (eachValue instanceof EnumInstance) {
values.add((CoreInstance) eachValue);
} else {
throw new PureExecutionException(si, "Unknown type from output of JsonDeserializer for property: " + eachKeyValue.getKey());
}
}
Instance.setValuesForProperty(instance, eachKeyValue.getKey(), values, processorSupport);
}
DeserializationUtils.replaceReverseProperties(instance, processorSupport, si);
CoreInstance override = processorSupport.newAnonymousCoreInstance(si, M3Paths.ConstraintsGetterOverride);
if (constraintsOverride != null) {
Instance.addValueToProperty(override, M3Properties.constraintsManager, constraintsOverride._constraintsManager(), processorSupport);
Instance.addValueToProperty(instance, M3Properties.elementOverride, override, processorSupport);
}
CoreInstance value = ValueSpecificationBootstrap.wrapValueSpecification(instance, true, processorSupport);
return (T) DefaultConstraintHandler.handleConstraints(clazz, value, si, functionExecution, resolvedTypeParameters, resolvedMultiplicityParameters, variableContext, functionExpressionToUseInStack, profiler, instantiationContext, executionSupport);
}
public <T extends Any> T newUnitInstance(CoreInstance propertyType, String unitTypeString, Number unitValue) {
CoreInstance retrievedUnit = processorSupport.package_getByUserPath(unitTypeString);
if (!processorSupport.type_subTypeOf(retrievedUnit, propertyType)) {
throw new PureExecutionException("Cannot match unit type: " + unitTypeString + " as subtype of type: " + PackageableElement.getUserPathForPackageableElement(propertyType));
}
FastList<CoreInstance> params = new FastList<>();
params.add(ValueSpecificationBootstrap.wrapValueSpecification(retrievedUnit, false, processorSupport));
params.add(NumericUtilities.toPureNumberValueExpression(unitValue, false, repository, processorSupport));
return (T) new NewUnit(repository, functionExecution).execute(params, resolvedTypeParameters, resolvedMultiplicityParameters, variableContext, functionExpressionToUseInStack, profiler, instantiationContext, executionSupport, context, processorSupport);
}
}));
}
use of org.finos.legend.pure.m4.coreinstance.primitive.BooleanCoreInstance in project legend-pure by finos.
the class ModelRepository method newBooleanCoreInstance.
private BooleanCoreInstance newBooleanCoreInstance(boolean value, CoreInstance classifier, int internalSyntheticId) {
AtomicReference<BooleanCoreInstance> cache = value ? this.cachedTrue : this.cachedFalse;
BooleanCoreInstance instance = cache.get();
if (instance == null) {
cache.compareAndSet(null, PrimitiveCoreInstance.newBooleanCoreInstance(value, classifier, internalSyntheticId));
instance = cache.get();
}
return instance;
}
Aggregations