Skip to content

Commit 96a95b4

Browse files
author
Caitlin Bales (MSFT)
committed
Add ability to overwrite existing methods
Fixes manual editing of methods: - BaseOnenotePageCollectionRequest - IBaseOnenotePageCollectionRequest - BaseOnenotePageRequest - IBaseOnenotePageRequest
1 parent e45b3e4 commit 96a95b4

File tree

7 files changed

+104
-1
lines changed

7 files changed

+104
-1
lines changed

Templates/Android/BaseModel.template.tt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,4 +816,34 @@ public {1} {2}{3}{4} {{";
816816

817817
return property != null ? property.GetValue(customImports).ToString() : string.Empty;
818818
}
819+
820+
/*
821+
* Find and replace overwrite values for the given class
822+
* @param className The class that you will be applying overwrites to
823+
*
824+
* @return An empty string (does not write anything new to the file)
825+
*/
826+
public string PostProcess(string className)
827+
{
828+
//Load the StringBuilder with the file contents
829+
var strb = this.GenerationEnvironment;
830+
831+
//Get the list of available classes to overwrite
832+
CustomOverwrites customOverwrites = new CustomOverwrites();
833+
var type = customOverwrites.GetType();
834+
835+
//Load the dictionary that corresponds to the current class
836+
var results = type.GetFields().Where(x => x.Name == className).FirstOrDefault();
837+
838+
if (results != null) {
839+
var dictionary = (Dictionary<string, string>)results.GetValue(this);
840+
//Find and replace for each item in the dictionary
841+
foreach (var item in dictionary) {
842+
strb.Replace(item.Key, item.Value);
843+
}
844+
}
845+
//This seems to be a requirement of the template builder
846+
//Specifying a return type of "void" throws an error
847+
return string.Empty;
848+
}
819849
#>

Templates/Android/generated/BaseEntityCollectionRequest.java.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,4 @@
104104
return page;
105105
}
106106
}
107+
<#=PostProcess(BaseTypeCollectionRequest(c))#>

Templates/Android/generated/BaseEntityRequest.java.tt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
}
9494
<# } #>
9595
}
96+
<#=PostProcess(BaseTypeRequest(c))#>
9697
<#+
9798
public String deleteMethods(OdcmObject c)
9899
{
@@ -192,4 +193,4 @@
192193
";
193194
return string.Format(formatString, TypeName(odcmObject));
194195
}
195-
#>
196+
#>

Templates/Android/generated/IBaseEntityCollectionRequest.java.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@
4747

4848
<# } #>
4949
}
50+
<#=PostProcess(IBaseTypeCollectionRequest(c))#>

Templates/Android/generated/IBaseEntityRequest.java.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<#=IBaseTypeRequest(c)#> top(final int value);
7070
<# } #>
7171
}
72+
<#=PostProcess(IBaseTypeRequest(c))#>
7273
<#+
7374
public String deleteMethods(OdcmObject c)
7475
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.Android
8+
{
9+
/**
10+
* This class contains a list of constants that define code snippets that should
11+
* be overwritten at generation time
12+
* The field name should be equal to the class name you wish to edit
13+
* The dictionary value should contain two string, the first is the search value
14+
* and the latter is the replace value
15+
* This will be used in BaseModel.template.tt in the PostProcess() method
16+
*/
17+
public class CustomOverwrites
18+
{
19+
public static Dictionary<string, string> BaseOnenotePageCollectionRequest = new Dictionary<string, string>() {
20+
{
21+
"public void post(final OnenotePage newOnenotePage, final ICallback<OnenotePage> callback) {",
22+
"public void post(final byte[] newOnenotePage, final ICallback<OnenotePage> callback) {"
23+
},
24+
{
25+
"public OnenotePage post(final OnenotePage newOnenotePage) throws ClientException {",
26+
"public OnenotePage post(final byte[] newOnenotePage) throws ClientException {"
27+
}
28+
};
29+
30+
public static Dictionary<string, string> BaseOnenotePageRequest = new Dictionary<string, string>()
31+
{
32+
{
33+
"public void post(final OnenotePage newOnenotePage, final ICallback<OnenotePage> callback) {",
34+
"public void post(final byte[] newOnenotePage, final ICallback<OnenotePage> callback) {"
35+
},
36+
{
37+
"public OnenotePage post(final OnenotePage newOnenotePage) throws ClientException {",
38+
"public OnenotePage post(final byte[] newOnenotePage) throws ClientException {"
39+
}
40+
};
41+
42+
public static Dictionary<string, string> IBaseOnenotePageCollectionRequest = new Dictionary<string, string>()
43+
{
44+
{
45+
"void post(final OnenotePage newOnenotePage, final ICallback<OnenotePage> callback);",
46+
"void post(final byte[] newOnenotePage, final ICallback<OnenotePage> callback);"
47+
},
48+
{
49+
"OnenotePage post(final OnenotePage newOnenotePage) throws ClientException;",
50+
"OnenotePage post(final byte[] newOnenotePage) throws ClientException;"
51+
}
52+
};
53+
54+
public static Dictionary<string, string> IBaseOnenotePageRequest = new Dictionary<string, string>()
55+
{
56+
{
57+
"void post(final OnenotePage newOnenotePage, final ICallback<OnenotePage> callback);",
58+
"void post(final byte[] newOnenotePage, final ICallback<OnenotePage> callback);"
59+
},
60+
{
61+
"OnenotePage post(final OnenotePage newOnenotePage) throws ClientException;",
62+
"OnenotePage post(final byte[] newOnenotePage) throws ClientException;"
63+
}
64+
};
65+
}
66+
}

src/GraphODataTemplateWriter/GraphODataTemplateWriter.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
</Reference>
5252
</ItemGroup>
5353
<ItemGroup>
54+
<Compile Include="CodeHelpers\Android\CustomImports.cs" />
55+
<Compile Include="CodeHelpers\Android\CustomMethods.cs" />
56+
<Compile Include="CodeHelpers\Android\CustomOverwrites.cs" />
5457
<Compile Include="CodeHelpers\CodeWriterBase.cs" />
5558
<Compile Include="CodeHelpers\CSharp\CodeWriterCSharp.cs" />
5659
<Compile Include="CodeHelpers\CSharp\TypeHelperCSharp.cs" />

0 commit comments

Comments
 (0)