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

com.regnosys.rosetta.generator.jsonschema.JsonSchemaEnumGenerator Maven / Gradle / Ivy

There is a newer version: 11.27.1
Show newest version
package com.regnosys.rosetta.generator.jsonschema;

import com.google.inject.Inject;
import com.regnosys.rosetta.rosetta.RosettaEnumValue;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@SuppressWarnings("all")
public class JsonSchemaEnumGenerator {
  @Inject
  @Extension
  private JsonSchemaGeneratorHelper _jsonSchemaGeneratorHelper;

  public Map generateEnumDefinitions(final List enumList) {
    HashMap _xblockexpression = null;
    {
      final HashMap result = CollectionLiterals.newHashMap();
      for (final RosettaEnumeration enumeration : enumList) {
        {
          final String enumDefinitionContents = this.generateEnumDefinition(enumeration);
          result.put(this._jsonSchemaGeneratorHelper.getFilename(enumeration), enumDefinitionContents);
        }
      }
      _xblockexpression = result;
    }
    return _xblockexpression;
  }

  public String generateEnumDefinition(final RosettaEnumeration enumeration) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("{");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("\"$schema\": \"http://json-schema.org/draft-04/schema#\",");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("\"$anchor\": \"");
    String _namespace = this._jsonSchemaGeneratorHelper.getNamespace(enumeration);
    _builder.append(_namespace, "  ");
    _builder.append("\",");
    _builder.newLineIfNotEmpty();
    _builder.append("  ");
    _builder.append("\"type\": \"string\",");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("\"title\": \"");
    String _name = enumeration.getName();
    _builder.append(_name, "  ");
    _builder.append("\",");
    _builder.newLineIfNotEmpty();
    {
      String _definition = enumeration.getDefinition();
      boolean _tripleNotEquals = (_definition != null);
      if (_tripleNotEquals) {
        _builder.append("  ");
        _builder.append("\"description\": \"");
        String _definition_1 = enumeration.getDefinition();
        _builder.append(_definition_1, "  ");
        _builder.append("\",");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("  ");
    _builder.append("\"enum\": [");
    _builder.newLine();
    _builder.append("    ");
    {
      List _allEnumsValues = this.allEnumsValues(enumeration);
      boolean _hasElements = false;
      for(final RosettaEnumValue enumValue : _allEnumsValues) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(",\n", "    ");
        }
        _builder.append("\"");
        String _enumValueString = this.getEnumValueString(enumValue);
        _builder.append(_enumValueString, "    ");
        _builder.append("\"");
      }
    }
    _builder.newLineIfNotEmpty();
    _builder.append("  ");
    _builder.append("],");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("\"oneOf\": [");
    _builder.newLine();
    _builder.append("    ");
    {
      List _allEnumsValues_1 = this.allEnumsValues(enumeration);
      boolean _hasElements_1 = false;
      for(final RosettaEnumValue enumValue_1 : _allEnumsValues_1) {
        if (!_hasElements_1) {
          _hasElements_1 = true;
        } else {
          _builder.appendImmediate(",\n", "    ");
        }
        String _generateEnumValue = this.generateEnumValue(enumValue_1);
        _builder.append(_generateEnumValue, "    ");
      }
    }
    _builder.newLineIfNotEmpty();
    _builder.append("  ");
    _builder.append("]");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder.toString();
  }

  public String generateEnumValue(final RosettaEnumValue enumValue) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("{");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("\"enum\": [");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("\"");
    String _enumValueString = this.getEnumValueString(enumValue);
    _builder.append(_enumValueString, "    ");
    _builder.append("\"");
    _builder.newLineIfNotEmpty();
    _builder.append("  ");
    _builder.append("],");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("\"title\": \"");
    String _name = enumValue.getName();
    _builder.append(_name, "  ");
    _builder.append("\"");
    {
      String _definition = enumValue.getDefinition();
      boolean _tripleNotEquals = (_definition != null);
      if (_tripleNotEquals) {
        _builder.append(",");
        _builder.newLineIfNotEmpty();
        _builder.append("  ");
        _builder.append("\"description\": \"");
        String _definition_1 = enumValue.getDefinition();
        _builder.append(_definition_1, "  ");
        _builder.append("\"");
      }
    }
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder.toString();
  }

  private String getEnumValueString(final RosettaEnumValue enumValue) {
    String _display = enumValue.getDisplay();
    boolean _tripleNotEquals = (_display != null);
    if (_tripleNotEquals) {
      return enumValue.getDisplay();
    } else {
      return enumValue.getName();
    }
  }

  private List allEnumsValues(final RosettaEnumeration enumeration) {
    final ArrayList enumValues = new ArrayList();
    RosettaEnumeration e = enumeration;
    while ((e != null)) {
      {
        final Consumer _function = (RosettaEnumValue it) -> {
          enumValues.add(it);
        };
        e.getEnumValues().forEach(_function);
        e = e.getParent();
      }
    }
    final Function1 _function = (RosettaEnumValue it) -> {
      return it.getName();
    };
    return IterableExtensions.sortBy(enumValues, _function);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy