All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.reflect.impl.CdsElementReader Maven / Gradle / Ivy

There is a newer version: 3.8.0
Show newest version
/*******************************************************************
 * © 2020 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 java.util.Iterator;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
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 CdsElementReader {
	private static final IssueCollector issueCollector = IssueCollectorFactory
			.getIssueCollector(CdsElementReader.class);

	private final String pathToElement;
	private final String elementName;
	private final CdsModelBuilder model;
	private final CdsModelReader.Config config;

	public CdsElementReader(CdsModelReader.Config config, String pathToElement, String elementName, CdsModelBuilder model) {
		this.pathToElement = pathToElement;
		this.elementName = elementName;
		this.model = model;
		this.config = config;
	}

	public CdsElementBuilder read(JsonNode csn, StructuredTypeResolver structResolver) {
		// remove element default val before reading the type
		JsonNode defaultVal = ((ObjectNode) csn).remove(CdsConstants.DEFAULT);
		Object defaultValue = CdsSimpleTypeReader.defaultValue(defaultVal);
		CdsTypeBuilder type = findType(csn, model).orElseGet(() -> readType(config, pathToElement, csn, model, structResolver));
		Iterator> fields = csn.fields();
		boolean isKey = false;
		boolean isVirtual = false;
		boolean isLocalized = false;
		boolean notNull = false;
		String doc = null;
		while (fields.hasNext()) {
			String property = fields.next().getKey();
			if (!CdsAnnotationReader.isAnnotation(property)) {
				switch (property) {
				case CdsConstants.ITEMS:
				case CdsConstants.TYPE:
				case CdsConstants.LENGTH:
				case CdsConstants.PRECISION:
				case CdsConstants.SCALE:
				case CdsConstants.ORIGIN:
				case CdsConstants.ENUM:
				case CdsConstants.CDS_LOCALIZED:
				case CdsConstants.ELEMENTS:
				case CdsConstants.INDEXNUM:
				case CdsConstants.$INFERRED:
				case CdsConstants.VALUE:
				case CdsConstants.ON:
				case CdsConstants.TARGET:
				case CdsConstants.TARGET_ASPECT:
				case CdsConstants.CARDINALITY:
					break;
				case CdsConstants.LOCALIZEDCSN:
					isLocalized = csn.get(property).asBoolean();
					break;
				case CdsConstants.KEYS:
					break;
				case CdsConstants.KEY:
					isKey = csn.get(property).asBoolean();
					break;
				case CdsConstants.VIRTUAL:
					isVirtual = csn.get(property).asBoolean();
					break;
				case CdsConstants.NOT_NULL:
					notNull = csn.get(property).asBoolean();
					break;
				case CdsConstants.DOC:
					if(config.readDocs()) {
						doc = csn.get(property).asText();
					}
					break;
				default:
					issueCollector.unrecognized(pathToElement,
							"The element '%s' contains an unrecognized property '%s'.", pathToElement, property);
				}
			}
		}
		return new CdsElementBuilder<>(CdsAnnotationReader.read(config, csn), elementName, type, isKey, isVirtual, notNull,
				isLocalized, defaultValue, doc);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy