Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a config to swith between DateOnly TimeOnly and Date and TimeOfDay #417

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Microsoft.OData.Cli/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ public GenerateCommand()

this.AddOption(multipleFiles);

Option useDateTimeOnly = new Option<bool>(new[] { "--date-time-only" })
{
Name = "date-time-only",
Description = "Use C# DateOnly and TimeOnly for Edm.Date and Edm.TimeOfDay."
};

this.AddOption(useDateTimeOnly);

Option excludedOperationImports = new Option<string>(new[] { "--excluded-operation-imports", "-eoi" })
{
Name = "excluded-operation-imports",
Expand Down Expand Up @@ -305,6 +313,7 @@ private TServiceConfig GetServiceConfiguration<TServiceConfig>(GenerateOptions g
UseDataServiceCollection = (generateOptions.EnableTracking == null) ? (configUserSettings?.UseDataServiceCollection ?? false) : generateOptions.EnableTracking.Value,
MakeTypesInternal = (generateOptions.EnableInternal == null) ? (configUserSettings?.MakeTypesInternal ?? false) : generateOptions.EnableInternal.Value,
GenerateMultipleFiles = (generateOptions.MultipleFiles == null) ? (configUserSettings?.GenerateMultipleFiles ?? false) : generateOptions.MultipleFiles.Value,
UseDateTimeOnly = (generateOptions.UseDateTimeOnly == null) ? (configUserSettings?.UseDateTimeOnly ?? false) : generateOptions.UseDateTimeOnly.Value,
ExcludedSchemaTypes = excludedSchemaTypes,
};

Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.OData.Cli/GenerateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public class GenerateOptions
/// </summary>
public bool? MultipleFiles { get; set; }

/// <summary>
/// Gets or sets a value that determines whether to use DateOnly and TimeOnly for Edm.Date and Edm.TimeOfDay.
/// </summary>
public bool? UseDateTimeOnly { get; set; }

/// <summary>
/// Gets or sets the comma-separated list of the names of operation imports to exclude from the generated code. Example: ExcludedOperationImport1,ExcludedOperationImport2.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private async Task AddT4FileAsync(string metadata, string outputDirectory, Langu
text = Regex.Replace(text, "(public const bool IgnoreUnexpectedElementsAndAttributes = )true;", "$1" + serviceConfiguration.IgnoreUnexpectedElementsAndAttributes.ToString(CultureInfo.InvariantCulture).ToLower(CultureInfo.InvariantCulture) + ";");
text = Regex.Replace(text, "(public const bool MakeTypesInternal = )false;", "$1" + serviceConfiguration.MakeTypesInternal.ToString(CultureInfo.InvariantCulture).ToLower(CultureInfo.InvariantCulture) + ";");
text = Regex.Replace(text, "(public const bool GenerateMultipleFiles = )false;", "$1" + serviceConfiguration.GenerateMultipleFiles.ToString(CultureInfo.InvariantCulture).ToLower(CultureInfo.InvariantCulture) + ";");
text = Regex.Replace(text, "(public const bool UseDateTimeOnly = )false;", "$1" + serviceConfiguration.UseDateTimeOnly.ToString(CultureInfo.InvariantCulture).ToLower(CultureInfo.InvariantCulture) + ";");
var customHeaders = serviceConfiguration.CustomHttpHeaders ?? "";
text = Regex.Replace(text, "(public const string CustomHttpHeaders = )\"\";", "$1@\"" + customHeaders + "\";");
text = Regex.Replace(text, "(public const string MetadataFilePath = )\"\";", "$1@\"" + metadataFile + "\";");
Expand Down Expand Up @@ -163,6 +164,7 @@ private async Task AddGeneratedCodeAsync(string metadata, string outputDirectory
t4CodeGenerator.MakeTypesInternal = serviceConfiguration.MakeTypesInternal;
t4CodeGenerator.OmitVersioningInfo = serviceConfiguration.OmitVersioningInfo;
t4CodeGenerator.GenerateMultipleFiles = serviceConfiguration.GenerateMultipleFiles;
t4CodeGenerator.UseDateTimeOnly = serviceConfiguration.UseDateTimeOnly;
t4CodeGenerator.ExcludedOperationImports = serviceConfiguration.ExcludedOperationImports;
t4CodeGenerator.ExcludedBoundOperations = serviceConfiguration.ExcludedBoundOperations;
t4CodeGenerator.ExcludedSchemaTypes = serviceConfiguration.ExcludedSchemaTypes;
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.OData.CodeGen/Models/BaseUserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BaseUserSettings : INotifyPropertyChanged

private bool generateMultipleFiles;

private bool useDateTimeOnly;

private string customHttpHeaders;

private bool storeCustomHttpHeaders;
Expand Down Expand Up @@ -204,6 +206,20 @@ public bool GenerateMultipleFiles
}
}

/// <summary>
/// Gets or sets a value that determines whether to use <see cref="System.DateOnly"/> or <see cref="System.TimeOnly"/> for Edm.Date and Edm.TimeOfDay.
/// </summary>
[DataMember]
public bool UseDateTimeOnly
{
get { return useDateTimeOnly; }
set
{
useDateTimeOnly = value;
OnPropertyChanged();
}
}

/// <summary>
/// Gets or sets headers that will get sent along with the request when fetching the metadata document from the service. Format: Header1:HeaderValue, Header2:HeaderValue.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OData.CodeGen/Models/ServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ServiceConfiguration
public bool MakeTypesInternal { get; set; }
public bool OpenGeneratedFilesInIDE { get; set; }
public bool GenerateMultipleFiles { get; set; }
public bool UseDateTimeOnly { get; set; }
public string CustomHttpHeaders { get; set; }
public bool IncludeWebProxy { get; set; }
public string WebProxyHost { get; set; }
Expand Down
36 changes: 31 additions & 5 deletions src/Microsoft.OData.CodeGen/Templates/ODataT4CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,14 @@ public static class Configuration
// This flag indicates whether to omit runtime version and code generation timestamp from the generated files
public const bool OmitVersioningInfo = false;

//This files indicates whether to generate the files into multiple files or single.
//This flag indicates whether to generate the files into multiple files or single.
//If set to true then multiple files will be generated. Otherwise only a single file is generated.
public const bool GenerateMultipleFiles = false;

//This flag indicates whether to use DateOnly or TimeOnly.
//If set to true then DateOnly or TimeOnly is used. Otherwise Date or TimeOfDay is used.
public const bool UseDateTimeOnly = false;

// (Optional) Custom http headers as a multiline string
public const string CustomHttpHeaders = "";

Expand Down Expand Up @@ -206,6 +210,7 @@ public virtual async Task<string> TransformTextAsync()
MultipleFilesManager = new FilesManager(null),
OmitVersioningInfo = this.OmitVersioningInfo,
GenerateMultipleFiles = this.GenerateMultipleFiles,
UseDateTimeOnly = this.UseDateTimeOnly,
ExcludedOperationImports = this.ExcludedOperationImports,
ExcludedBoundOperations = this.ExcludedBoundOperations,
ExcludedSchemaTypes = this.ExcludedSchemaTypes,
Expand Down Expand Up @@ -247,6 +252,7 @@ public virtual async Task<string> TransformTextAsync()
MultipleFilesManager = new FilesManager(null),
OmitVersioningInfo = this.OmitVersioningInfo,
GenerateMultipleFiles = this.GenerateMultipleFiles,
UseDateTimeOnly = this.UseDateTimeOnly,
ExcludedOperationImports = this.ExcludedOperationImports,
ExcludedBoundOperations = this.ExcludedBoundOperations,
ExcludedSchemaTypes = this.ExcludedSchemaTypes,
Expand Down Expand Up @@ -514,6 +520,16 @@ public bool GenerateMultipleFiles
get;
set;
}

/// <summary>
/// true to use DateOnly or TimeOnly, false to use Date or TimeOfDay.
/// </summary>
public bool UseDateTimeOnly
{
get;
set;
}

/// <summary>
/// Boolean to show if we should include the web proxy
/// </summary>
Expand Down Expand Up @@ -764,6 +780,7 @@ private void ApplyParametersFromConfigurationClass()
this.MetadataFilePath = Configuration.MetadataFilePath;
this.MetadataFileRelativePath = Configuration.MetadataFileRelativePath;
this.GenerateMultipleFiles = Configuration.GenerateMultipleFiles;
this.UseDateTimeOnly = Configuration.UseDateTimeOnly;
this.SetCustomHttpHeadersFromString(Configuration.CustomHttpHeaders);
this.ExcludedOperationImports = Configuration.ExcludedOperationImports.Split(',')
.Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList();
Expand Down Expand Up @@ -1281,6 +1298,15 @@ public bool IgnoreUnexpectedElementsAndAttributes
/// true to generate multiple files, false generate a single file.
/// </summary>
public bool GenerateMultipleFiles
{
get;
set;
}

/// <summary>
/// true to use DateOnly or TimeOnly, false to use Date or TimeOfDay.
/// </summary>
public bool UseDateTimeOnly
{
get;
set;
Expand Down Expand Up @@ -4298,10 +4324,10 @@ public ODataClientCSharpTemplate(CodeGenerationContext context)
internal override string GeometryMultiPolygonTypeName { get { return "global::Microsoft.Spatial.GeometryMultiPolygon"; } }
internal override string GeometryMultiLineStringTypeName { get { return "global::Microsoft.Spatial.GeometryMultiLineString"; } }
internal override string GeometryMultiPointTypeName { get { return "global::Microsoft.Spatial.GeometryMultiPoint"; } }
internal override string DateTypeName { get { return "global::Microsoft.OData.Edm.Date"; } }
internal override string DateTypeName { get { return context.UseDateTimeOnly ? "global::System.DateOnly" : "global::Microsoft.OData.Edm.Date"; } }
internal override string DateTimeOffsetTypeName { get { return "global::System.DateTimeOffset"; } }
internal override string DurationTypeName { get { return "global::System.TimeSpan"; } }
internal override string TimeOfDayTypeName { get { return "global::Microsoft.OData.Edm.TimeOfDay"; } }
internal override string TimeOfDayTypeName { get { return context.UseDateTimeOnly ? "global::System.TimeOnly" : "global::Microsoft.OData.Edm.TimeOfDay"; } }
internal override string XmlConvertClassName { get { return "global::System.Xml.XmlConvert"; } }
internal override string EnumTypeName { get { return "global::System.Enum"; } }
internal override string DictionaryInterfaceName { get { return "global::System.Collections.Generic.IDictionary<{0}, {1}>"; } }
Expand Down Expand Up @@ -6441,10 +6467,10 @@ public ODataClientVBTemplate(CodeGenerationContext context)
internal override string GeometryMultiPolygonTypeName { get { return "Global.Microsoft.Spatial.GeometryMultiPolygon"; } }
internal override string GeometryMultiLineStringTypeName { get { return "Global.Microsoft.Spatial.GeometryMultiLineString"; } }
internal override string GeometryMultiPointTypeName { get { return "Global.Microsoft.Spatial.GeometryMultiPoint"; } }
internal override string DateTypeName { get { return "Global.Microsoft.OData.Edm.Date"; } }
internal override string DateTypeName { get { return context.UseDateTimeOnly ? "global::System.DateOnly" : "global::Microsoft.OData.Edm.Date"; } }
internal override string DateTimeOffsetTypeName { get { return "Global.System.DateTimeOffset"; } }
internal override string DurationTypeName { get { return "Global.System.TimeSpan"; } }
internal override string TimeOfDayTypeName { get { return "Global.Microsoft.OData.Edm.TimeOfDay"; } }
internal override string TimeOfDayTypeName { get { return context.UseDateTimeOnly ? "global::System.TimeOnly" : "global::Microsoft.OData.Edm.TimeOfDay"; } }
internal override string XmlConvertClassName { get { return "Global.System.Xml.XmlConvert"; } }
internal override string EnumTypeName { get { return "Global.System.Enum"; } }
internal override string DictionaryInterfaceName { get { return "Global.System.Collections.Generic.IDictionary(Of {0}, {1})"; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ public static class Configuration
// This flag indicates whether to omit runtime version and code generation timestamp from the generated files
public const bool OmitVersioningInfo = false;

//This files indicates whether to generate the files into multiple files or single.
//This flag indicates whether to generate the files into multiple files or single.
//If set to true then multiple files will be generated. Otherwise only a single file is generated.
public const bool GenerateMultipleFiles = false;

//This flag indicates whether to use DateOnly or TimeOnly.
//If set to true then DateOnly or TimeOnly is used. Otherwise Date or TimeOfDay is used.
public const bool UseDateTimeOnly = false;

// (Optional) Custom http headers as a multiline string
public const string CustomHttpHeaders = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public virtual async Task<string> TransformTextAsync()
MultipleFilesManager = new FilesManager(null),
OmitVersioningInfo = this.OmitVersioningInfo,
GenerateMultipleFiles = this.GenerateMultipleFiles,
UseDateTimeOnly = this.UseDateTimeOnly,
ExcludedOperationImports = this.ExcludedOperationImports,
ExcludedBoundOperations = this.ExcludedBoundOperations,
ExcludedSchemaTypes = this.ExcludedSchemaTypes,
Expand Down Expand Up @@ -104,6 +105,7 @@ public virtual async Task<string> TransformTextAsync()
MultipleFilesManager = new FilesManager(null),
OmitVersioningInfo = this.OmitVersioningInfo,
GenerateMultipleFiles = this.GenerateMultipleFiles,
UseDateTimeOnly = this.UseDateTimeOnly,
ExcludedOperationImports = this.ExcludedOperationImports,
ExcludedBoundOperations = this.ExcludedBoundOperations,
ExcludedSchemaTypes = this.ExcludedSchemaTypes,
Expand Down Expand Up @@ -371,6 +373,16 @@ public bool GenerateMultipleFiles
get;
set;
}

/// <summary>
/// true to use DateOnly or TimeOnly, false to use Date or TimeOfDay.
/// </summary>
public bool UseDateTimeOnly
{
get;
set;
}

/// <summary>
/// Boolean to show if we should include the web proxy
/// </summary>
Expand Down Expand Up @@ -621,6 +633,7 @@ private void ApplyParametersFromConfigurationClass()
this.MetadataFilePath = Configuration.MetadataFilePath;
this.MetadataFileRelativePath = Configuration.MetadataFileRelativePath;
this.GenerateMultipleFiles = Configuration.GenerateMultipleFiles;
this.UseDateTimeOnly = Configuration.UseDateTimeOnly;
this.SetCustomHttpHeadersFromString(Configuration.CustomHttpHeaders);
this.ExcludedOperationImports = Configuration.ExcludedOperationImports.Split(',')
.Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList();
Expand Down Expand Up @@ -1138,6 +1151,15 @@ public class CodeGenerationContext
/// true to generate multiple files, false generate a single file.
/// </summary>
public bool GenerateMultipleFiles
{
get;
set;
}

/// <summary>
/// true to use DateOnly or TimeOnly, false to use Date or TimeOfDay.
/// </summary>
public bool UseDateTimeOnly
{
get;
set;
Expand Down Expand Up @@ -4155,10 +4177,10 @@ public sealed class ODataClientCSharpTemplate : ODataClientTemplate
internal override string GeometryMultiPolygonTypeName { get { return "global::Microsoft.Spatial.GeometryMultiPolygon"; } }
internal override string GeometryMultiLineStringTypeName { get { return "global::Microsoft.Spatial.GeometryMultiLineString"; } }
internal override string GeometryMultiPointTypeName { get { return "global::Microsoft.Spatial.GeometryMultiPoint"; } }
internal override string DateTypeName { get { return "global::Microsoft.OData.Edm.Date"; } }
internal override string DateTypeName { get { return context.UseDateTimeOnly ? "global::System.DateOnly" : "global::Microsoft.OData.Edm.Date"; } }
internal override string DateTimeOffsetTypeName { get { return "global::System.DateTimeOffset"; } }
internal override string DurationTypeName { get { return "global::System.TimeSpan"; } }
internal override string TimeOfDayTypeName { get { return "global::Microsoft.OData.Edm.TimeOfDay"; } }
internal override string TimeOfDayTypeName { get { return context.UseDateTimeOnly ? "global::System.TimeOnly" : "global::Microsoft.OData.Edm.TimeOfDay"; } }
internal override string XmlConvertClassName { get { return "global::System.Xml.XmlConvert"; } }
internal override string EnumTypeName { get { return "global::System.Enum"; } }
internal override string DictionaryInterfaceName { get { return "global::System.Collections.Generic.IDictionary<{0}, {1}>"; } }
Expand Down Expand Up @@ -5355,10 +5377,10 @@ public sealed class ODataClientVBTemplate : ODataClientTemplate
internal override string GeometryMultiPolygonTypeName { get { return "Global.Microsoft.Spatial.GeometryMultiPolygon"; } }
internal override string GeometryMultiLineStringTypeName { get { return "Global.Microsoft.Spatial.GeometryMultiLineString"; } }
internal override string GeometryMultiPointTypeName { get { return "Global.Microsoft.Spatial.GeometryMultiPoint"; } }
internal override string DateTypeName { get { return "Global.Microsoft.OData.Edm.Date"; } }
internal override string DateTypeName { get { return context.UseDateTimeOnly ? "global::System.DateOnly" : "global::Microsoft.OData.Edm.Date"; } }
internal override string DateTimeOffsetTypeName { get { return "Global.System.DateTimeOffset"; } }
internal override string DurationTypeName { get { return "Global.System.TimeSpan"; } }
internal override string TimeOfDayTypeName { get { return "Global.Microsoft.OData.Edm.TimeOfDay"; } }
internal override string TimeOfDayTypeName { get { return context.UseDateTimeOnly ? "global::System.TimeOnly" : "global::Microsoft.OData.Edm.TimeOfDay"; } }
internal override string XmlConvertClassName { get { return "Global.System.Xml.XmlConvert"; } }
internal override string EnumTypeName { get { return "Global.System.Enum"; } }
internal override string DictionaryInterfaceName { get { return "Global.System.Collections.Generic.IDictionary(Of {0}, {1})"; } }
Expand Down
4 changes: 3 additions & 1 deletion src/ODataConnectedService.Shared/Views/AdvancedSettings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@
<TextBlock TextWrapping="Wrap" Margin="0, 0, 40, 0">Omit runtime version and code generation timestamp from the generated files</TextBlock>
</CheckBox>
<CheckBox x:Name="IncludeT4File" Content="Add code templates (Whether to include the T4 files into this project)" IsChecked="{Binding Path=UserSettings.IncludeT4File, Mode=TwoWay}"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 15" FontWeight="Medium"/>
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 15" FontWeight="Medium"/>
<CheckBox x:Name="OpenGeneratedFilesInIDE" Content="Open generated files in the IDE when generation completes" IsChecked="{Binding Path=UserSettings.OpenGeneratedFilesInIDE, Mode=TwoWay}"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 5"/>
<CheckBox x:Name="GenerateMultipleFiles" Content="Generate multiple files" IsChecked="{Binding Path=UserSettings.GenerateMultipleFiles, Mode=TwoWay}"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 5"/>
<CheckBox x:Name="UseDateTimeOnly" Content="Use C# DateOnly and TimeOnly for Edm.Date and Edm.TimeOfDay" IsChecked="{Binding Path=UserSettings.UseDateTimeOnly, Mode=TwoWay}"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 5"/>
</StackPanel>
</StackPanel>
</StackPanel>
Expand Down
Loading