-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from microsoft/feature/java-writer
feature/java writer
- Loading branch information
Showing
8 changed files
with
159 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace kiota.core | ||
{ | ||
public class JavaWriter : LanguageWriter | ||
{ | ||
public override string GetFileSuffix() => ".java"; | ||
|
||
public override string GetParameterSignature(CodeParameter parameter) | ||
{ | ||
return $"@javax.annotation.Nonnull final {GetTypeString(parameter.Type)} {parameter.Name}"; | ||
} | ||
|
||
public override string GetTypeString(CodeType code) | ||
{ | ||
var typeName = TranslateType(code.Name, code.Schema); | ||
if (code.ActionOf) | ||
{ | ||
return $"java.util.function.Consumer<{typeName}>"; | ||
} | ||
else | ||
{ | ||
return typeName; | ||
} | ||
} | ||
|
||
public override string TranslateType(string typeName, OpenApiSchema schema) | ||
{ | ||
switch (typeName) | ||
{//TODO we're probably missing a bunch of type mappings | ||
case "integer": return "Integer"; | ||
case "boolean": return "Boolean"; | ||
case "string": return "String"; | ||
case "object": return "Object"; | ||
case "array": return $"{TranslateType(schema.Items.Type, schema.Items)}[]"; | ||
} | ||
|
||
return typeName; | ||
} | ||
|
||
public override void WriteCodeClassDeclaration(CodeClass.Declaration code) | ||
{ | ||
//TODO: missing javadoc | ||
WriteLine($"public class {code.Name} {{"); | ||
IncreaseIndent(); | ||
} | ||
|
||
public override void WriteCodeClassEnd(CodeClass.End code) | ||
{ | ||
DecreaseIndent(); | ||
WriteLine("}"); | ||
} | ||
|
||
public override void WriteIndexer(CodeIndexer code) | ||
{ | ||
WriteMethod(new CodeMethod { | ||
Name = "get", | ||
Parameters = new List<CodeParameter> { | ||
new CodeParameter { | ||
Name = "position", | ||
Type = code.IndexType, | ||
Optional = false, | ||
} | ||
}, | ||
ReturnType = code.IndexType | ||
}); | ||
} | ||
|
||
public override void WriteMethod(CodeMethod code) | ||
{ | ||
//TODO javadoc | ||
WriteLine("@javax.annotation.Nonnull"); | ||
WriteLine($"public java.util.concurrent.Future<{GetTypeString(code.ReturnType)}> {code.Name}({string.Join(',', code.Parameters.Select(p=> GetParameterSignature(p)).ToList())}) {{ return null; }}"); | ||
} | ||
|
||
public override void WriteNamespaceDeclaration(CodeNamespace.Declaration code) | ||
{ | ||
WriteLine($"package {code.Name};"); | ||
WriteLine(); | ||
foreach (var codeUsing in code.Usings) | ||
{ | ||
WriteLine($"import {codeUsing.Name}.*;"); | ||
} | ||
} | ||
|
||
public override void WriteNamespaceEnd(CodeNamespace.End code) => WriteLine(); | ||
|
||
public override void WriteProperty(CodeProperty code) | ||
{ | ||
//TODO: missing javadoc | ||
WriteLine("@javax.annotation.Nullable"); | ||
WriteLine($"public {GetTypeString(code.Type)} {code.Name};"); | ||
} | ||
|
||
public override void WriteType(CodeType code) | ||
{ | ||
Write(GetTypeString(code), includeIndent: false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters