Skip to content

Commit

Permalink
[Ada] Adding Ada support for client code generator (#6602)
Browse files Browse the repository at this point in the history
* Ada language support (generic generator)

Implement the AbstractAdaCodegen class with the Ada keywords and global
behavior for Ada language

* Ada language support (main generator)

Implement the AdaCodegen class for the Ada client and server code generator
Initial implementation based on a mix from CppRestClientCodegen, NodeJSServerCodegen

* Ada language support: register the AdaCodegen generator class

* Ada language license template

* Ada language model templates (spec and body)

* Ada language client spec and body templates

* Ada language server spec and body templates

* Fix escaping Ada keywords for parameter name

* Generate GNAT project and update type mappings
- Use 'p_' as escape prefix for parameter names because identifiers are not allowed to start with '_'
- Add GNAT project file generation
- Update the type mappings to use the Swagger package types

* Fix generation of operations with no parameters

* Fix instantiation of Ada.Containers.Vectors package in generated model files

* New template for the GNAT project file generation

* Fix datatype generation for Ada language
- Override the getTypeDeclaration function to specify the datatype
- Fix definition of language predefined types

* Add a Serialize procedure declaration for the Ada model generation

* Add a Serialize procedure for the Ada template model generation

* Fix operation name and parameter name for Ada
- Declare and implement toAdaIdentifier function to verify and turn some identifier
  to Ada identifier following Ada style
- Override toOperationId and use toAdaIdentifier for generation of operation name
- Update toParamName to use toAdaIdentifier

* Media type support for Ada code generator
- Implement postProcessMediaTypes function to add a 'adaMediaType' member
  to each media type and map it to some Ada enumeration
- Post process the 'produces' and 'consumes' media type of an operation

* Use x-has-notes extension to avoid emitting notes when they are empty

* First generation for Ada client operation body

* Add a x-has-uniq-produces and x-has-uniq-consumes to media types
to indicate that the list contains only one item, this is necessary
for Ada generator for generation of arrays with one item only

* Add a postProcessParameter for Ada code generator to emit a x-is-model-type attribute

* Update Ada client body template for the serialization of data

* Fix postProcessParameter in Ada code generator to take into account file parameters

* Update the Ada client body to support form parameters

* Fix type name used for mapped types in the Ada generator

* Declare a Deserialize procedure for the Ada generated model

* Override the fromOperation for Ada client code generator
- Emit a x-codegen-response extension with the response description type
- Emit a x-id-model-type extension for each type property

* Update the Ada client package spec template to declare the result type

* Add support to extract response and return result in Ada client code

* Fix Ada postProcessModels to handle container properties and emit a correct x-is-model-type attribute

* Add support for Deserialize procedure in the Ada model generator

* Fix indentation of generated Ada client body package

* Add projectName option to configure the GNAT project name

* Update the GNAT project name for the Ada code generator

* Cleanup implementation and remove unused code

* Cleanup implementation and remove unused code

* Fix javadoc errors

* Use 'ada' for the language name to follow swagger-codegen convention
Add (beta) to the help description
Fix a NPE that occurs with incomplete yaml descriptions
  • Loading branch information
stcarrez authored and wing328 committed Oct 4, 2017
1 parent f7bd2c5 commit 90010da
Show file tree
Hide file tree
Showing 11 changed files with 777 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package io.swagger.codegen.languages;

import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.models.properties.Property;

import java.util.Arrays;

abstract public class AbstractAdaCodegen extends DefaultCodegen implements CodegenConfig {

public AbstractAdaCodegen() {
super();

/*
* Reserved words. Override this with reserved words specific to your language
*/
setReservedWordsLowerCase(
Arrays.asList(
"abort",
"abs",
"abstract",
"accept",
"access",
"aliased",
"all",
"and",
"array",
"at",
"begin",
"body",
"case",
"constant",
"declare",
"delay",
"digits",
"do",
"else",
"elsif",
"end",
"entry",
"exception",
"exit",
"for",
"function",
"generic",
"goto",
"if",
"in",
"interface",
"is",
"limited",
"loop",
"mod",
"new",
"not",
"null",
"of",
"or",
"others",
"out",
"overriding",
"package",
"pragma",
"private",
"procedure",
"protected",
"raise",
"range",
"record",
"rem",
"renames",
"requeue",
"return",
"reverse",
"select",
"separate",
"some",
"subtype",
"synchronized",
"tagged",
"task",
"terminate",
"then",
"type",
"until",
"use",
"when",
"while",
"with",
"xor")
);
}

/**
* Turn a parameter name, operation name into an Ada identifier.
*
* Ada programming standard avoid the camelcase syntax and prefer the underscore
* notation. We also have to make sure the identifier is not a reserved keyword.
* When this happens, we add the configurable prefix. The function translates:
*
* body - P_Body
* petId - Pet_Id
* updatePetWithForm - Update_Pet_With_Form
*
* @param name the parameter name.
* @param prefix the optional prefix in case the parameter name is a reserved keyword.
* @return the Ada identifier to be used.
*/
protected String toAdaIdentifier(String name, String prefix) {
// We cannot use reserved keywords for identifiers
if (isReservedWord(name)) {
LOGGER.warn("Identifier '" + name + "' is a reserved word, renamed to " + prefix + name);
name = prefix + name;
}
StringBuilder result = new StringBuilder();
boolean needUpperCase = true;
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (needUpperCase) {
needUpperCase = false;
result.append(Character.toUpperCase(c));

} else if (Character.isUpperCase((c))) {
if (!needUpperCase) {
result.append('_');
}
result.append(c);
needUpperCase = false;
} else {
result.append(c);
if (c == '_') {
needUpperCase = true;
}
}
}
return result.toString();
}

@Override
public String toOperationId(String operationId) {
return toAdaIdentifier(sanitizeName(operationId), "Call_");
}

@Override
public String toVarName(String name) {
return toAdaIdentifier(sanitizeName(name), "P_");
}

@Override
public String toParamName(String name) {
return toAdaIdentifier(super.toParamName(name), "P_");
}

@Override
public CodegenProperty fromProperty(String name, Property p) {
CodegenProperty property = super.fromProperty(name, p);
String nameInCamelCase = property.nameInCamelCase;
nameInCamelCase = sanitizeName(nameInCamelCase);
property.nameInCamelCase = nameInCamelCase;
return property;
}
}
Loading

0 comments on commit 90010da

Please sign in to comment.