Skip to content

Commit

Permalink
Support nullable reference types in C# 8 in OData2Poco project
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-hassan committed Mar 30, 2020
1 parent bef052b commit 1575fc5
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 104 deletions.
11 changes: 6 additions & 5 deletions OData2PocoLib/Api/O2P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ public class O2P
public PocoSetting Setting { get; set; }
public List<ClassTemplate> ClassList { get; set; }
internal MetaDataInfo MetaData { get; set; }
public string MetaDataAsString => MetaData?.MetaDataAsString;
public string MetaDataVersion => MetaData?.MetaDataVersion;
public string SchemaNamespace => MetaData?.SchemaNamespace;
public Dictionary<string, string> ServiceHeader => MetaData?.ServiceHeader;
public string MetaDataAsString => MetaData.MetaDataAsString;
public string MetaDataVersion => MetaData.MetaDataVersion;
public string SchemaNamespace => MetaData.SchemaNamespace;
public Dictionary<string, string> ServiceHeader => MetaData.ServiceHeader;

public string CodeText { get; set; }
//warning due to renaming of reserved keywords
public List<string> ModelWarning => ModelManager.ModelWarning;
public O2P(PocoSetting setting = null)
public O2P(PocoSetting? setting = null)
{
Setting = setting ?? new PocoSetting();
ClassList = new List<ClassTemplate>();
MetaData = new MetaDataInfo();
CodeText = "";
}


Expand Down
10 changes: 6 additions & 4 deletions OData2PocoLib/ClassTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using OData2Poco.CustAttributes;
using OData2Poco.Extensions;


namespace OData2Poco
Expand All @@ -13,7 +12,6 @@ public class ClassTemplate
public string Name { get; set; }
public string BaseType { get; set; }
public string Comment { get; set; }
public string ToDebugString { get; set; }
public List<PropertyTemplate> Properties { get; set; }
public List<string> Keys { get; set; }
public List<string> Navigation { get; set; }
Expand All @@ -22,10 +20,10 @@ public class ClassTemplate
public bool IsEnum { get; set; }
public bool IsFlags { get; set; } //v3, Add [FlagsAttribute] to enum
public List<string> EnumElements { get; set; }
public string OriginalName { get; set; }
public string OriginalName { get; set; } = null!;

//v1.4.0
public string EntitySetName { get; set; }
public string? EntitySetName { get; set; }
public string NameSpace { get; set; }
public string FullName =>string.IsNullOrEmpty(NameSpace)
?Name
Expand All @@ -39,6 +37,10 @@ public ClassTemplate()
Keys = new List<string>();
EnumElements = new List<string>();
Navigation = new List<string>();
BaseType = "";
Comment = "";
Name = "UNDEFINED";
NameSpace = "";
}

private readonly AttributeFactory _attributeFactory = AttributeFactory.Default;
Expand Down
2 changes: 1 addition & 1 deletion OData2PocoLib/CustAttributes/AttributeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private AttributeFactory()
/// </summary>
/// <param name="setting"></param>
/// <returns></returns>
public AttributeFactory Init(PocoSetting setting=null)
public AttributeFactory Init(PocoSetting? setting=null)
{
if (setting==null)
setting= new PocoSetting();
Expand Down
5 changes: 1 addition & 4 deletions OData2PocoLib/CustAttributes/PocoAttributesList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ private void FillNamedAttributes()

foreach (var type in types)
{
var item = (INamedAttribute)Activator.CreateInstance(type);

_namedAttributes.Add(item);

if (Activator.CreateInstance(type) is INamedAttribute item) _namedAttributes.Add(item);
}

}
Expand Down
2 changes: 1 addition & 1 deletion OData2PocoLib/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public static T ToEnum<T>(this string value)
// ignored
}

return default;
return default!;
}
public static JObject ToJObject(this string json)
{
Expand Down
6 changes: 3 additions & 3 deletions OData2PocoLib/Http/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public async Task Authenticate(OdataConnectionString odataConnString)
//OAuth2
if (!string.IsNullOrEmpty(odataConnString.TokenUrl))
{
var accessToken = await new TokenEndpoint(odataConnString).GetAccessTokenAsync();
string? accessToken = await new TokenEndpoint(odataConnString).GetAccessTokenAsync();
Authenticate(accessToken);
}
break;
}
}
private AuthenticationHeaderValue Authenticate(string user, string password)
private AuthenticationHeaderValue? Authenticate(string user, string password)
{
//credintial
if (string.IsNullOrEmpty(user)) return null;
Expand All @@ -55,7 +55,7 @@ private AuthenticationHeaderValue Authenticate(string user, string password)
_client.DefaultRequestHeaders.Authorization = headerValue;
return headerValue;
}
private AuthenticationHeaderValue Authenticate(string token)
private AuthenticationHeaderValue? Authenticate(string? token)
{
//credintial
if (string.IsNullOrEmpty(token)) return null;
Expand Down
7 changes: 4 additions & 3 deletions OData2PocoLib/Http/CustomeHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ internal class CustomeHttpClient :IDisposable
{
public static ILog Logger = PocoLogger.Default;
readonly OdataConnectionString _odataConnectionString;
readonly DelegatingHandler _delegatingHandler;
readonly DelegatingHandler? _delegatingHandler;
public Uri ServiceUri { get; set; }
public HttpResponseMessage Response;
public HttpResponseMessage? Response;
private HttpClient _client;
public CustomeHttpClient(OdataConnectionString odataConnectionString)
{
_odataConnectionString = odataConnectionString;
ServiceUri = new Uri(_odataConnectionString.ServiceUrl);
_client = new HttpClient();
}
public CustomeHttpClient(OdataConnectionString odataConnectionString, DelegatingHandler dh)
: this(odataConnectionString)
Expand Down Expand Up @@ -102,7 +103,7 @@ public void Dispose()
{
_delegatingHandler?.Dispose();
Response?.Dispose();
_client?.Dispose();
_client.Dispose();
}
}

Expand Down
8 changes: 4 additions & 4 deletions OData2PocoLib/Http/TokenEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class TokenEndpoint
public string TokenParams { get; set; }
public Dictionary<string, string> TokenParamsCollection { get; set; }
//json string
public string LastToken { get; set; }
public string? LastToken { get; set; }
public TokenEndpoint(OdataConnectionString odataConnectionString)
{
//OConnectionString = odataConnectionString;
Expand Down Expand Up @@ -56,7 +56,7 @@ private string SetTokenParams(OdataConnectionString odataConnectionString)

return tokenParams;
}
public async Task<string> GetAccessTokenAsync()
public async Task<string?> GetAccessTokenAsync()
{
Logger.Normal($"Start connecting to Token endpoint: {TokenUrl}");
var token = await GetToken(new Uri(TokenUrl), TokenParamsCollection);
Expand All @@ -76,7 +76,7 @@ public async Task<string> GetAccessTokenAsync()
access_token : bi05REFMcXdodUhZbkhRNjNHZUNYYyIsImtpZCI6Ik4tbEMwbi05REFMcXdod...
*/
public async Task<string> GetToken(Uri authenticationUrl, Dictionary<string, string> authenticationCredentials)
public async Task<string?> GetToken(Uri authenticationUrl, Dictionary<string, string> authenticationCredentials)
{
var client = new HttpClient();
var content = new FormUrlEncodedContent(authenticationCredentials);
Expand All @@ -103,7 +103,7 @@ internal DateTime ExpiresOn()
var date = ToLocalDateTime(expireOnLong);
return date;
}
internal string ParseTokenResponse(string content, string key)
internal string? ParseTokenResponse(string? content, string key)
{
if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(key))
return null;
Expand Down
15 changes: 10 additions & 5 deletions OData2PocoLib/MetaDataInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ namespace OData2Poco
{
public class MetaDataInfo
{
public string ServiceUrl { get; set; }
public string MetaDataAsString { get; set; }
public string MetaDataVersion { get; set; }
public string ServiceVersion { get; set; } //for http media
public string SchemaNamespace { get; set; }
public string ServiceUrl { get; set; } = null!;
public string MetaDataAsString { get; set; } = null!;
public string MetaDataVersion { get; set; } = null!;
public string ServiceVersion { get; set; } = null!;//for http media
public string SchemaNamespace { get; set; } = null!;
public Dictionary<string, string> ServiceHeader { get; set; }
public Media MediaType { get; set; }

public MetaDataInfo()
{
ServiceHeader = new Dictionary<string, string>();
}

}
}
21 changes: 9 additions & 12 deletions OData2PocoLib/MetaDataReader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using OData2Poco;
using OData2Poco.Http;
using OData2Poco.InfraStructure.Logging;

Expand Down Expand Up @@ -34,14 +30,15 @@ public static async Task<MetaDataInfo> LoadMetaDataHttpAsync(OdataConnectionStri
ServiceUrl = client.ServiceUri.OriginalString,
SchemaNamespace = Helper.GetNameSpace(content),
MediaType = Media.Http,
ServiceHeader = new Dictionary<string, string>()
};
foreach (var entry in client.Response.Headers)
{
string value = entry.Value.FirstOrDefault();
string key = entry.Key;
metaData.ServiceHeader.Add(key, value);
}
if (client.Response != null)
foreach (var entry in client.Response.Headers)
{
string value = entry.Value.FirstOrDefault();
string key = entry.Key;
metaData.ServiceHeader.Add(key, value);
}

metaData.ServiceVersion = Helper.GetServiceVersion(metaData.ServiceHeader);
return metaData;
}
Expand Down
2 changes: 2 additions & 0 deletions OData2PocoLib/OData2Poco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<Product>OData2Poco</Product>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
<Description>
"OData2Poco is a class library supporting netstandard2 and .NET 4.5+ to generate plain-old CLR objects (POCO) from OData feeds that implement both V1-3 and V4 OData protocol,based on the metadata of the service stored on the server. POCO classes can be used in typed RESTful client OData services"
</Description>
Expand Down
28 changes: 18 additions & 10 deletions OData2PocoLib/OdataConnectionString.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
using System.Net;
#nullable disable
using System.Net;
using OData2Poco.Http;

namespace OData2Poco
{
public class OdataConnectionString
{
public string ServiceUrl { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Domain { get; set; }
public string Proxy { get; set; }
public string TokenUrl { get; set; }
public string TokenParams { get; set; }
public string ParamFile { get; set; }
public string ServiceUrl { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Domain { get; set; }
public string Proxy { get; set; }
public string TokenUrl { get; set; }
public string TokenParams { get; set; }
public string ParamFile { get; set; }
public AuthenticationType Authenticate { get; set; }
public SecurityProtocolType TlsProtocol { get; set; }=SecurityProtocolType.Tls12;
public SecurityProtocolType TlsProtocol { get; set; }

public OdataConnectionString()
{
TlsProtocol = SecurityProtocolType.Tls12;
}
}

}

#nullable restore
21 changes: 11 additions & 10 deletions OData2PocoLib/PocoClassGeneratorCs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ private PocoClassGeneratorCs(IPocoGenerator pocoGen, PocoSetting setting)
PocoSetting = setting;
_pocoGen = pocoGen;
ClassList = pocoGen.GeneratePocoList();
CodeText = null;
CodeText = "";
Header = "";
}

public bool BlankSpaceBeforeProperties { get; set; } = true;
public string LangName { get; set; } = "csharp";

private string CodeText { get; set; }
private string? CodeText { get; set; }

//key is fullName: <namespace.className>
public ClassTemplate this[string key] => ClassList.FirstOrDefault(x => x.FullName == key);
Expand All @@ -45,11 +46,11 @@ private PocoClassGeneratorCs(IPocoGenerator pocoGen, PocoSetting setting)
/// <returns></returns>
public string GeneratePoco()
{
var ns = ClassList.Select(x => x.NameSpace).Distinct()
List<string> ns = ClassList.Select(x => x.NameSpace).Distinct()
.OrderBy(x => x).ToList();
var template = new FluentCsTextTemplate {Header = Header};

template.WriteLine(UsingAssemply(ns));
template.WriteLine(UsingAssembly(ns));
foreach (var s in ns)
{
//Use a user supplied namespace prefix combined with the schema namepace or just the schema namespace
Expand All @@ -64,9 +65,9 @@ public string GeneratePoco()
return template.ToString();
}

public static PocoClassGeneratorCs GenerateCsPocoClass(IPocoGenerator pocoGen, PocoSetting setting)
public static PocoClassGeneratorCs GenerateCsPocoClass(IPocoGenerator pocoGen, PocoSetting? setting)
{
setting = setting ?? new PocoSetting();
setting ??= new PocoSetting();
//add jsonproperty to properties/classes that are renamed
setting.Attributes.Add("original"); //v3.2

Expand Down Expand Up @@ -95,7 +96,7 @@ public static PocoClassGeneratorCs GenerateCsPocoClass(IPocoGenerator pocoGen, P
public override string ToString()
{
if (string.IsNullOrEmpty(CodeText)) CodeText = GeneratePoco();
return CodeText;
return CodeText??string.Empty;
}

/// <summary>
Expand Down Expand Up @@ -189,11 +190,11 @@ private string GetHeader()
return h.ToString();
}

private string UsingAssemply(List<string> nameSpaces)
private string UsingAssembly(List<string> nameSpaces)
{
var h = new FluentCsTextTemplate();
var assemplyManager = new AssemplyManager(PocoSetting, ClassList);
var asemplyList = assemplyManager.AssemplyReference;
var assemblyManager = new AssemplyManager(PocoSetting, ClassList);
var asemplyList = assemblyManager.AssemplyReference;
foreach (var entry in asemplyList) h.UsingNamespace(entry);
//add also namespaces of the built-in schema namespaces
if (nameSpaces.Count > 1)
Expand Down
10 changes: 5 additions & 5 deletions OData2PocoLib/PocoSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ public class PocoSetting
/// </summary>
public bool AddJsonAttribute { get; set; } ////obsolete, use Attributes.add("json")

public string Prefix { get; set; }
public string Suffix { get; set; }
public string? Prefix { get; set; }
public string? Suffix { get; set; }
public bool MultiFiles { get; set; }
public string ModuleName { get; set; }
public string ModuleName { get; set; } = null!;
public bool AddReference { get; set; }
public bool GenerateInterface { get; set; }
public List<string> Include { get; set; }
public List<string>? Include { get; set; }
/// <summary>
/// Initialization
/// </summary>
public PocoSetting()
{
Lang = Language.CS;
NamespacePrefix = string.Empty;
Inherit = null;
Inherit = "";
NameCase = CaseEnum.None;
AddJsonAttribute = false;
Attributes = new List<string>();
Expand Down
Loading

0 comments on commit 1575fc5

Please sign in to comment.