
com.sap.cds.reflect.impl.CdsOperationReader Maven / Gradle / Ivy
/*********************************************************************
* © 2019-2022 SAP SE or an SAP affiliate company. All rights reserved. *
*********************************************************************/
package com.sap.cds.reflect.impl;
import static com.sap.cds.reflect.impl.CdsModelReader.findType;
import static com.sap.cds.reflect.impl.CdsModelReader.readType;
import static com.sap.cds.util.CdsModelUtils.getDoc;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.sap.cds.CdsException;
import com.sap.cds.impl.parser.TokenParser;
import com.sap.cds.reflect.CdsAction;
import com.sap.cds.reflect.CdsFunction;
import com.sap.cds.reflect.CdsType;
import com.sap.cds.reflect.impl.reader.issuecollector.IssueCollector;
import com.sap.cds.reflect.impl.reader.issuecollector.IssueCollectorFactory;
import com.sap.cds.reflect.impl.reader.model.CdsConstants;
import com.sap.cds.util.StructuredTypeResolver;
public class CdsOperationReader {
private static final Logger logger = LoggerFactory.getLogger(CdsOperationReader.class);
private static final IssueCollector issueCollector = IssueCollectorFactory
.getIssueCollector(CdsOperationReader.class);
private CdsOperationReader() {
}
public static CdsOperationBuilder readAction(CdsModelReader.Config config, String qualifiedName, String name,
JsonNode csn) {
return new CdsActionBuilder(false, CdsAnnotationReader.read(config, csn), qualifiedName, name,
config.readDocs() ? getDoc(csn) : null);
}
public static CdsOperationBuilder readFunction(CdsModelReader.Config config, String qualifiedName, String name,
JsonNode csn) {
return new CdsFunctionBuilder(false, CdsAnnotationReader.read(config, csn), qualifiedName, name,
config.readDocs() ? getDoc(csn) : null);
}
// used in CdsModelReader
public static List readParameterList(CdsModelReader.Config config, String name,
JsonNode jsonStructuredType,
CdsModelBuilder model, StructuredTypeResolver structResolver) {
List paramList = new LinkedList<>();
JsonNode params;
if (jsonStructuredType.has(CdsConstants.PARAMS)) {
params = jsonStructuredType.get(CdsConstants.PARAMS);
} else {
return paramList;
}
Iterator> fields = params.fields();
while (fields.hasNext()) {
Entry next = fields.next();
JsonNode field = next.getValue();
String paramName = next.getKey();
try {
CdsTypeBuilder> type = findType(field, model)
.orElseGet(() -> readType(config, "", field, model, structResolver));
Function defValProvider = (t) -> TokenParser
.defaultValue(field.get(CdsConstants.DEFAULT), t);
boolean notNull = Optional.ofNullable(field.get(CdsConstants.NOT_NULL)).map(n -> n.asBoolean(false))
.orElse(false);
paramList
.add(new CdsParameterBuilder(CdsAnnotationReader.read(config, field), paramName, type,
defValProvider,
config.readDocs() ? getDoc(field) : null, notNull));
} catch (InvalidCsnException e) {
String pathToElement = name + "." + paramName;
logger.debug(pathToElement + ": ", e);
issueCollector.error(pathToElement, e.getMessage());
}
}
return paramList;
}
public static CdsTypeBuilder> readReturnType(CdsModelReader.Config config, JsonNode jsonStructuredType,
CdsModelBuilder model,
StructuredTypeResolver structResolver) {
if (jsonStructuredType.has(CdsConstants.RETURNS)) {
JsonNode returns = jsonStructuredType.get(CdsConstants.RETURNS);
return findType(returns, model).orElseGet(() -> readType(config, "", returns, model, structResolver));
} else if (jsonStructuredType.get(CdsConstants.KIND).asText().equalsIgnoreCase(CdsConstants.FUNCTION)) {
throw new CdsException("Missing return type function. Please add the return type in the CDS model.");
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy