-
Notifications
You must be signed in to change notification settings - Fork 7
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 #9 from jdunkerley/DateTimeParser
Date time parser
- Loading branch information
Showing
20 changed files
with
673 additions
and
38 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
49 changes: 49 additions & 0 deletions
49
AlteryxDateParser/Attributes/InputPropertyNameAttribute.cs
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,49 @@ | ||
namespace JDunkerley.Alteryx.Attributes | ||
{ | ||
using System; | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Specifies associated input field for configs | ||
/// </summary> | ||
public class InputPropertyNameAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="InputPropertyNameAttribute"/> class. | ||
/// </summary> | ||
/// <param name="fieldName">Name of the field.</param> | ||
/// <param name="engineType">Type of the engine</param> | ||
/// <param name="fieldTypes">List of valid types (defaults to all)</param> | ||
public InputPropertyNameAttribute(string fieldName, Type engineType, params AlteryxRecordInfoNet.FieldType[] fieldTypes) | ||
{ | ||
this.FieldName = fieldName; | ||
this.EngineType = engineType; | ||
|
||
this.FieldTypes = fieldTypes; | ||
if (fieldTypes.Length == 0) | ||
{ | ||
this.FieldTypes = (AlteryxRecordInfoNet.FieldType[])Enum.GetValues(typeof(AlteryxRecordInfoNet.FieldType)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the field. | ||
/// </summary> | ||
public string FieldName { get; } | ||
|
||
/// <summary> | ||
/// Gets the type of the engine. | ||
/// </summary> | ||
public Type EngineType { get; } | ||
|
||
/// <summary> | ||
/// Gets the field types list. | ||
/// </summary> | ||
public AlteryxRecordInfoNet.FieldType[] FieldTypes { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the current meta data. | ||
/// </summary> | ||
public static XmlElement[] CurrentMetaData { get; set; } | ||
} | ||
} |
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,165 @@ | ||
namespace JDunkerley.Alteryx | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
|
||
using AlteryxGuiToolkit.Plugins; | ||
|
||
using AlteryxRecordInfoNet; | ||
|
||
using JDunkerley.Alteryx.Attributes; | ||
using JDunkerley.Alteryx.Framework; | ||
|
||
public class DateTimeParserTool : | ||
BaseTool<DateTimeParserTool.Config, DateTimeParserTool.Engine>, IPlugin | ||
{ | ||
public enum OutputType | ||
{ | ||
Date, | ||
DateTime, | ||
String | ||
} | ||
|
||
public class Config | ||
{ | ||
/// <summary> | ||
/// Gets or sets the type of the output. | ||
/// </summary> | ||
public OutputType OutputType { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the output field. | ||
/// </summary> | ||
public string OutputFieldName { get; set; } = "Date"; | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the input field. | ||
/// </summary> | ||
[TypeConverter(typeof(InputFieldTypeConverter))] | ||
[InputPropertyName(nameof(Engine.Input), typeof(Engine))] | ||
public string InputFieldName { get; set; } = "DateInput"; | ||
|
||
/// <summary> | ||
/// Gets or sets the input format. | ||
/// </summary> | ||
public string InputFormat { get; set; } | ||
|
||
/// <summary> | ||
/// ToString used for annotation | ||
/// </summary> | ||
/// <returns></returns> | ||
public override string ToString() => $"{this.InputFieldName}=>{this.OutputFieldName}"; | ||
|
||
/// <summary> | ||
/// Create a FieldDescription Object | ||
/// </summary> | ||
/// <returns></returns> | ||
public FieldDescription OutputDescription() | ||
{ | ||
switch (this.OutputType) | ||
{ | ||
case OutputType.Date: | ||
return new FieldDescription(this.OutputFieldName, FieldType.E_FT_Date); | ||
case OutputType.DateTime: | ||
return new FieldDescription(this.OutputFieldName, FieldType.E_FT_DateTime); | ||
case OutputType.String: | ||
return new FieldDescription(this.OutputFieldName, FieldType.E_FT_String) | ||
{ | ||
Size = 19, | ||
Source = nameof(DateTimeParserTool), | ||
Description = $"{this.InputFieldName} parsed as a DateTime" | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} | ||
|
||
public class Engine : BaseEngine<Config> | ||
{ | ||
private FieldBase _inputFieldBase; | ||
|
||
private RecordCopier _copier; | ||
|
||
private RecordInfo _outputRecordInfo; | ||
|
||
private FieldBase _outputFieldBase; | ||
|
||
public Engine() | ||
{ | ||
this.Input = new InputProperty( | ||
initFunc: this.InitFunc, | ||
progressAction: d => this.Output.UpdateProgress(d), | ||
pushFunc: this.PushFunc); | ||
} | ||
|
||
private bool InitFunc(RecordInfo info) | ||
{ | ||
var config = this.GetConfigObject(); | ||
var fieldDescription = config.OutputDescription(); | ||
if (fieldDescription == null) | ||
{ | ||
return false; | ||
} | ||
|
||
this._inputFieldBase = info.GetFieldByName(config.InputFieldName, false); | ||
if (this._inputFieldBase == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var newRecordInfo = Utilities.CreateRecordInfo(info, fieldDescription); | ||
|
||
this._outputRecordInfo = newRecordInfo; | ||
this._outputFieldBase = newRecordInfo.GetFieldByName(config.OutputFieldName, false); | ||
this.Output?.Init(newRecordInfo, nameof(this.Output), null, this.XmlConfig); | ||
|
||
// Create the Copier | ||
this._copier = Utilities.CreateCopier(info, newRecordInfo, config.OutputFieldName); | ||
|
||
return true; | ||
} | ||
|
||
private bool PushFunc(RecordData r) | ||
{ | ||
var fmt = this.GetConfigObject().InputFormat; | ||
|
||
var record = this._outputRecordInfo.CreateRecord(); | ||
this._copier.Copy(record, r); | ||
|
||
string input = this._inputFieldBase.GetAsString(r); | ||
|
||
DateTime dt; | ||
bool result = string.IsNullOrWhiteSpace(fmt) | ||
? DateTime.TryParse(input, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces, out dt) | ||
: DateTime.TryParseExact(input, fmt, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces, out dt); | ||
|
||
if (result) | ||
{ | ||
this._outputFieldBase.SetFromString(record, dt.ToString("yyyy-MM-dd HH:mm:ss")); | ||
} | ||
else | ||
{ | ||
this._outputFieldBase.SetNull(record); | ||
} | ||
|
||
this.Output?.PushRecord(record.GetRecord()); | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the input stream. | ||
/// </summary> | ||
[CharLabel('I')] | ||
public InputProperty Input { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the output stream. | ||
/// </summary> | ||
[CharLabel('O')] | ||
public PluginOutputConnectionHelper Output { get; set; } | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.