Search in sources :

Example 1 with DefaultConfiguratorRegistry

use of io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry in project configuration-as-code-plugin by jenkinsci.

the class SchemaGeneration method generateSchema.

public static JSONObject generateSchema() {
    JSONObject schemaObject = new JSONObject(schemaTemplateObject.toString());
    DefaultConfiguratorRegistry registry = new DefaultConfiguratorRegistry();
    final ConfigurationContext context = new ConfigurationContext(registry);
    JSONObject rootConfiguratorProperties = new JSONObject();
    for (RootElementConfigurator rootElementConfigurator : RootElementConfigurator.all()) {
        JSONObject schemaConfiguratorObjects = new JSONObject();
        Set<Object> elements = new LinkedHashSet<>();
        listElements(elements, rootElementConfigurator.describe(), context, true);
        for (Object configuratorObject : elements) {
            if (configuratorObject instanceof BaseConfigurator) {
                BaseConfigurator baseConfigurator = (BaseConfigurator) configuratorObject;
                List<Attribute> baseConfigAttributeList = baseConfigurator.getAttributes();
                if (baseConfigAttributeList.size() == 0) {
                    String key = baseConfigurator.getName();
                    schemaConfiguratorObjects.put(key, new JSONObject().put("additionalProperties", false).put("type", "object").put("properties", new JSONObject()));
                } else {
                    JSONObject attributeSchema = new JSONObject();
                    for (Attribute attribute : baseConfigAttributeList) {
                        if (attribute.multiple) {
                            generateMultipleAttributeSchema(attributeSchema, attribute, context, baseConfigurator);
                        } else {
                            if (attribute.type.isEnum()) {
                                generateEnumAttributeSchema(attributeSchema, attribute, baseConfigurator);
                            } else {
                                attributeSchema.put(attribute.getName(), generateNonEnumAttributeObject(attribute, baseConfigurator));
                                String key = baseConfigurator.getName();
                                schemaConfiguratorObjects.put(key, new JSONObject().put("additionalProperties", false).put("type", "object").put("properties", attributeSchema));
                            }
                        }
                    }
                }
            } else if (configuratorObject instanceof HeteroDescribableConfigurator) {
                HeteroDescribableConfigurator heteroDescribableConfigurator = (HeteroDescribableConfigurator) configuratorObject;
                String key = heteroDescribableConfigurator.getName();
                schemaConfiguratorObjects.put(key, generateHeteroDescribableConfigObject(heteroDescribableConfigurator));
            } else if (configuratorObject instanceof Attribute) {
                Attribute attribute = (Attribute) configuratorObject;
                if (attribute.type.isEnum()) {
                    generateEnumAttributeSchema(schemaConfiguratorObjects, attribute, null);
                } else {
                    schemaConfiguratorObjects.put(attribute.getName(), generateNonEnumAttributeObject(attribute, null));
                }
            }
        }
        rootConfiguratorProperties.put(rootElementConfigurator.getName(), new JSONObject().put("type", "object").put("additionalProperties", false).put("properties", schemaConfiguratorObjects).put("title", "Configuration base for the " + rootElementConfigurator.getName() + " classifier"));
    }
    schemaObject.put("properties", rootConfiguratorProperties);
    return schemaObject;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JSONObject(org.json.JSONObject) HeteroDescribableConfigurator(io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator) DescribableAttribute(io.jenkins.plugins.casc.impl.attributes.DescribableAttribute) DefaultConfiguratorRegistry(io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry) JSONObject(org.json.JSONObject)

Aggregations

DefaultConfiguratorRegistry (io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry)1 DescribableAttribute (io.jenkins.plugins.casc.impl.attributes.DescribableAttribute)1 HeteroDescribableConfigurator (io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator)1 LinkedHashSet (java.util.LinkedHashSet)1 JSONObject (org.json.JSONObject)1