Skip to content

Commit

Permalink
[F#] minor improvements to the generators (#3968)
Browse files Browse the repository at this point in the history
* add license, minor code format

* mark fsharp generator as beta

* update doc
  • Loading branch information
wing328 authored Oct 3, 2019
1 parent 5c24a54 commit 7c7fa68
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 70 deletions.
Empty file modified bin/fsharp-functions-server-petstore.sh
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions docs/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ The following generators are available:
* [cpp-restbed-server](generators/cpp-restbed-server)
* [csharp-nancyfx](generators/csharp-nancyfx)
* [erlang-server](generators/erlang-server)
* [fsharp-functions](generators/fsharp-functions)
* [fsharp-giraffe-server](generators/fsharp-giraffe-server)
* [fsharp-functions (beta)](generators/fsharp-functions)
* [fsharp-giraffe-server (beta)](generators/fsharp-giraffe-server)
* [go-gin-server](generators/go-gin-server)
* [go-server](generators/go-server)
* [graphql-nodejs-express-server](generators/graphql-nodejs-express-server)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.Exception;

import java.io.File;
Expand Down Expand Up @@ -298,7 +299,7 @@ public void processOpts() {
}

if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) {
setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING));
setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING));
}

// This either updates additionalProperties with the above fixes, or sets the default if the option was not specified.
Expand Down Expand Up @@ -345,49 +346,49 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
}

/*
* F# does not allow forward declarations, so files must be imported in the correct order.
* Output of CodeGen models must therefore bein dependency order (rather than alphabetical order, which seems to be the default).
* This could probably be made more efficient if absolutely needed.
*/
* F# does not allow forward declarations, so files must be imported in the correct order.
* Output of CodeGen models must therefore bein dependency order (rather than alphabetical order, which seems to be the default).
* This could probably be made more efficient if absolutely needed.
*/
@SuppressWarnings({"unchecked"})
public Map<String,Object> postProcessDependencyOrders(final Map<String, Object> objs) {

Map<String,Set<String>> dependencies = new HashMap<String,Set<String>>();

List<String> classNames = new ArrayList<String>();

for(String k : objs.keySet()) {
CodegenModel model = ModelUtils.getModelByName(k, objs);
if(model == null || model.classname == null) {
throw new RuntimeException("Null model encountered");
public Map<String, Object> postProcessDependencyOrders(final Map<String, Object> objs) {

Map<String, Set<String>> dependencies = new HashMap<String, Set<String>>();

List<String> classNames = new ArrayList<String>();

for (String k : objs.keySet()) {
CodegenModel model = ModelUtils.getModelByName(k, objs);
if (model == null || model.classname == null) {
throw new RuntimeException("Null model encountered");
}
dependencies.put(model.classname, model.imports);

classNames.add(model.classname);
}

Object[] sortedKeys = classNames.toArray();

for (int i1 = 0; i1 < sortedKeys.length; i1++) {
String k1 = sortedKeys[i1].toString();
for (int i2 = i1 + 1; i2 < sortedKeys.length; i2++) {
String k2 = sortedKeys[i2].toString();
if (dependencies.get(k2).contains(k1)) {
sortedKeys[i2] = k1;
sortedKeys[i1] = k2;
i1 = -1;
break;
}
}
}
dependencies.put(model.classname, model.imports);

classNames.add(model.classname);
}

Object[] sortedKeys = classNames.toArray();

for(int i1 = 0 ; i1 < sortedKeys.length; i1++) {
String k1 = sortedKeys[i1].toString();
for(int i2 = i1 + 1; i2 < sortedKeys.length; i2++) {
String k2 = sortedKeys[i2].toString();
if(dependencies.get(k2).contains(k1)) {
sortedKeys[i2] = k1;
sortedKeys[i1] = k2;
i1 = -1;
break;
}

Map<String, Object> sorted = new LinkedHashMap<String, Object>();
for (int i = sortedKeys.length - 1; i >= 0; i--) {
Object k = sortedKeys[i];
sorted.put(k.toString(), objs.get(k));
}
}

Map<String,Object> sorted = new LinkedHashMap<String,Object>();
for(int i = sortedKeys.length - 1; i >= 0; i--) {
Object k = sortedKeys[i];
sorted.put(k.toString(), objs.get(k));
}

return sorted;

return sorted;
}

/**
Expand Down Expand Up @@ -656,23 +657,23 @@ public void setModelPropertyNaming(String naming) {
}
}


public String getNameUsingModelPropertyNaming(String name) {
switch (CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.valueOf(getModelPropertyNaming())) {
case original:
return name;
case camelCase:
return camelize(name, true);
case PascalCase:
return camelize(name);
case snake_case:
return underscore(name);
default:
throw new IllegalArgumentException("Invalid model property naming '" +
name + "'. Must be 'original', 'camelCase', " +
"'PascalCase' or 'snake_case'");
}
}
switch (CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.valueOf(getModelPropertyNaming())) {
case original:
return name;
case camelCase:
return camelize(name, true);
case PascalCase:
return camelize(name);
case snake_case:
return underscore(name);
default:
throw new IllegalArgumentException("Invalid model property naming '" +
name + "'. Must be 'original', 'camelCase', " +
"'PascalCase' or 'snake_case'");
}
}

@Override
public String toVarName(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. x
*/

package org.openapitools.codegen.languages;

import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
Expand Down Expand Up @@ -28,12 +46,16 @@ public String getName() {
}

public String getHelp() {
return "Generates a fsharp-functions server.";
return "Generates a fsharp-functions server (beta).";
}

public FsharpFunctionsServerCodegen() {
super();

generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.BETA)
.build();

// CLI options
addOption(CodegenConstants.LICENSE_URL,
CodegenConstants.LICENSE_URL_DESC,
Expand Down Expand Up @@ -68,21 +90,21 @@ public FsharpFunctionsServerCodegen() {
null);

addOption(CodegenConstants.SOURCE_FOLDER,
CodegenConstants.SOURCE_FOLDER_DESC,
sourceFolder);
CodegenConstants.SOURCE_FOLDER_DESC,
sourceFolder);
}

@Override
public void processOpts() {
super.processOpts();

modelPackage = "Model";
embeddedTemplateDir = templateDir = "fsharp-functions-server";

apiTemplateFiles.put("Handler.mustache", "Handler.fs");
apiTemplateFiles.put("HandlerParams.mustache", "HandlerParams.fs");
apiTemplateFiles.put("ServiceInterface.mustache", "ServiceInterface.fs");
apiTemplateFiles.put("ServiceImpl.mustache", "Service.fs");
apiTemplateFiles.put("Handler.mustache", "Handler.fs");
apiTemplateFiles.put("HandlerParams.mustache", "HandlerParams.fs");
apiTemplateFiles.put("ServiceInterface.mustache", "ServiceInterface.fs");
apiTemplateFiles.put("ServiceImpl.mustache", "Service.fs");
modelTemplateFiles.put("Model.mustache", ".fs");

String implFolder = sourceFolder + File.separator + "impl";
Expand All @@ -99,9 +121,9 @@ public void processOpts() {

@Override
public String modelFileFolder() {
return super.modelFileFolder().replace("Model","model");
return super.modelFileFolder().replace("Model", "model");
}

@Override
public String apiFileFolder() {
return super.apiFileFolder() + File.separator + "api";
Expand All @@ -113,7 +135,7 @@ private String implFileFolder() {

@Override()
public String toModelImport(String name) {
return packageName + "." + modelPackage() + "." + name;
return packageName + "." + modelPackage() + "." + name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.utils.URLPathUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -59,6 +61,10 @@ public class FsharpGiraffeServerCodegen extends AbstractFSharpCodegen {
public FsharpGiraffeServerCodegen() {
super();

generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.BETA)
.build();

modelPackage = "Model";

apiTemplateFiles.put("Handler.mustache", "Handler.fs");
Expand Down

0 comments on commit 7c7fa68

Please sign in to comment.