Search in sources :

Example 1 with BaseType

use of com.webcohesion.enunciate.api.datatype.BaseType in project enunciate by stoicflame.

the class BaseDatatypeNameForMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The datatypeNameFor method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    BaseType baseType = null;
    if (unwrapped instanceof DataType) {
        DataType dataType = (DataType) unwrapped;
        baseType = dataType.getBaseType();
    }
    if (baseType == null) {
        throw new TemplateModelException("No base data type name for: " + unwrapped);
    }
    switch(baseType) {
        case bool:
            return "boolean";
        case number:
            return "number";
        case string:
            return "string";
        default:
            return "object";
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BaseType(com.webcohesion.enunciate.api.datatype.BaseType) DataType(com.webcohesion.enunciate.api.datatype.DataType) TemplateModel(freemarker.template.TemplateModel)

Example 2 with BaseType

use of com.webcohesion.enunciate.api.datatype.BaseType in project enunciate by stoicflame.

the class ReferencedDatatypeNameForMethod method exec.

@SuppressWarnings("rawtypes")
public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The datatypeNameFor method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (!DataTypeReference.class.isAssignableFrom(unwrapped.getClass())) {
        throw new TemplateModelException("No referenced data type name for: " + unwrapped);
    }
    DataTypeReference reference = DataTypeReference.class.cast(unwrapped);
    BaseType baseType = reference.getBaseType();
    String format = reference.getBaseTypeFormat();
    String defaultType = "file";
    if (list.size() > 1) {
        defaultType = FreemarkerUtil.unwrap((TemplateModel) list.get(1)).toString();
    }
    switch(baseType) {
        case bool:
            return "boolean";
        case number:
            if ("int32".equals(format) || "int64".equals(format)) {
                return "integer";
            } else {
                return "number";
            }
        case string:
            return "string";
        default:
            return defaultType;
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) BaseType(com.webcohesion.enunciate.api.datatype.BaseType) TemplateModel(freemarker.template.TemplateModel)

Aggregations

BaseType (com.webcohesion.enunciate.api.datatype.BaseType)2 TemplateModel (freemarker.template.TemplateModel)2 TemplateModelException (freemarker.template.TemplateModelException)2 DataType (com.webcohesion.enunciate.api.datatype.DataType)1 DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)1