Skip to content

Commit

Permalink
- update the models to reduce the import surface in generated classes
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Aug 21, 2020
1 parent ed901f9 commit 6364e11
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 135 deletions.
130 changes: 130 additions & 0 deletions Templates/Java/BaseJavaModel.template.tt
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,136 @@
return typeString;
}

public string CreatePackageDefForBaseEntityRequestBuilder(CustomT4Host host)
{
var sb = new StringBuilder();
sb.Append(CreatePackageDefinition(host));

var importFormat = @"import {0}.{1}.{2};";
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeRequest(host.CurrentType));
sb.Append("\n");
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeRequest(host.CurrentType));
sb.Append("\n");
var c = host.CurrentType;
if (c.AsOdcmClass() != null)
{
foreach(var prop in c.AsOdcmClass().NavigationProperties())
{
if (prop.IsCollection()) {

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeCollectionRequestBuilder(prop));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeCollectionRequestBuilder(prop));
sb.Append("\n");
}
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeRequestBuilder(prop));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeRequestBuilder(prop));
sb.Append("\n");
}

foreach (var prop in c.AsOdcmClass().GetProperties(typeName:"Stream"))
{
var propRequestBuilder = TypeRequestBuilder(prop);
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
"I" + propRequestBuilder);
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
propRequestBuilder);
sb.Append("\n");
}

if (c is OdcmMediaClass)
{
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeStreamRequestBuilder(host.CurrentType));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeStreamRequestBuilder(host.CurrentType));
sb.Append("\n");
}

foreach (var method in c.AsOdcmClass().MethodsAndOverloads()) {
if (method.IsBoundToCollection) {
continue;
}

foreach (var p in method.Parameters)
{
if(!(p.Type is OdcmPrimitiveType) && p.Type.GetTypeString() != "com.google.gson.JsonElement") {
var typeString = p.Type.GetTypeString();
if(p.Type.GetTypeString().StartsWith("EnumSet<")){
typeString = typeString.Substring(typeString.IndexOf("<") + 1);
typeString = typeString.Substring(0, typeString.Length-1);
}
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
getPackagePrefix(p),
typeString);
sb.Append("\n");
}
}

if (method.IsCollection) {
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeCollectionRequestBuilder(method));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeCollectionRequestBuilder(method));
sb.Append("\n");
} else {
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeRequestBuilder(method));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeRequestBuilder(method));
sb.Append("\n");
}
}
}
return sb.ToString();
}

public string ReturnType(OdcmObject c) {
var returnType = c.AsOdcmMethod().ReturnType;
if (returnType != null) {
Expand Down
132 changes: 0 additions & 132 deletions Templates/Java/requests_extensions/BaseEntityRequestBuilder.java.tt
Original file line number Diff line number Diff line change
Expand Up @@ -151,135 +151,3 @@ if (c.AsOdcmClass() != null)
}
#>
}

<#+
public string CreatePackageDefForBaseEntityRequestBuilder(CustomT4Host host)
{
var sb = new StringBuilder();
sb.Append(CreatePackageDefinition(host));

var importFormat = @"import {0}.{1}.{2};";
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeRequest(host.CurrentType));
sb.Append("\n");
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeRequest(host.CurrentType));
sb.Append("\n");
var c = host.CurrentType;
if (c.AsOdcmClass() != null)
{
foreach(var prop in c.AsOdcmClass().NavigationProperties())
{
if (prop.IsCollection()) {

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeCollectionRequestBuilder(prop));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeCollectionRequestBuilder(prop));
sb.Append("\n");
}
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeRequestBuilder(prop));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeRequestBuilder(prop));
sb.Append("\n");
}

foreach (var prop in c.AsOdcmClass().GetProperties(typeName:"Stream"))
{
var propRequestBuilder = TypeRequestBuilder(prop);
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
"I" + propRequestBuilder);
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
propRequestBuilder);
sb.Append("\n");
}

if (c is OdcmMediaClass)
{
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeStreamRequestBuilder(host.CurrentType));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeStreamRequestBuilder(host.CurrentType));
sb.Append("\n");
}

foreach (var method in c.AsOdcmClass().MethodsAndOverloads()) {
if (method.IsBoundToCollection) {
continue;
}

foreach (var p in method.Parameters)
{
if(!(p.Type is OdcmPrimitiveType) && p.Type.GetTypeString() != "com.google.gson.JsonElement") {
var typeString = p.Type.GetTypeString();
if(p.Type.GetTypeString().StartsWith("EnumSet<")){
typeString = typeString.Substring(typeString.IndexOf("<") + 1);
typeString = typeString.Substring(0, typeString.Length-1);
}
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
getPackagePrefix(p),
typeString);
sb.Append("\n");
}
}

if (method.IsCollection) {
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeCollectionRequestBuilder(method));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeCollectionRequestBuilder(method));
sb.Append("\n");
} else {
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
ITypeRequestBuilder(method));
sb.Append("\n");

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"requests.extensions",
TypeRequestBuilder(method));
sb.Append("\n");
}
}
}
return sb.ToString();
}
#>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
<#@ output extension="\\" #>
<#host.TemplateName = TypeStreamRequestBuilder(c);#>
<#=writer.WriteHeader()#>
<#=CreatePackageDef(host)#>
<#=CreatePackageDefForBaseEntityRequestBuilder(host)#>
import com.microsoft.graph.core.IBaseClient;
import com.microsoft.graph.http.BaseRequestBuilder;
import com.microsoft.graph.options.Option;
import java.util.EnumSet;


<#=CreateClassDef(TypeStreamRequestBuilder(c), "BaseRequestBuilder", ITypeStreamRequestBuilder(c))#>

Expand Down
6 changes: 5 additions & 1 deletion Templates/Java/requests_extensions/BaseStreamRequest.java.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<#@ output extension="\\" #>
<#host.TemplateName = TypeStreamRequest(c);#>
<#=writer.WriteHeader()#>
<#=CreatePackageDef(host)#>
<#=CreatePackageDefForBaseEntityRequestBuilder(host)#>
import com.microsoft.graph.core.IBaseClient;
import com.microsoft.graph.http.BaseRequestBuilder;
import com.microsoft.graph.options.Option;
import java.util.EnumSet;

import java.io.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
<#@ output extension="\\" #>
<#host.TemplateName = ITypeStreamRequestBuilder(c);#>
<#=writer.WriteHeader()#>
<#=CreatePackageDef(host)#>
<#=CreatePackageDefForBaseEntityRequestBuilder(host)#>
import com.microsoft.graph.core.IBaseClient;
import com.microsoft.graph.http.BaseRequestBuilder;
import com.microsoft.graph.options.Option;
import com.microsoft.graph.http.IRequestBuilder;
import java.util.EnumSet;

<#=CreateInterfaceDef(ITypeStreamRequestBuilder(c), "IRequestBuilder")#>

Expand Down

0 comments on commit 6364e11

Please sign in to comment.