public ValueOperation(string outKey, string outType, string value, IParameters parameters) : base(string.Empty, outKey) { _parameters = parameters; if (!value.Equals(string.Empty)) { _value = Common.GetObjectConversionMap()[Common.ToSimpleType(outType)](value); } else if (!_parameters.Any()) { throw new TransformalizeException(Logger, EntityName, "The value transform method requires the value attribute to be set, or a parameter."); } Name = string.Format("ValueOperation ({0})", outKey); }
public JavascriptOperation( string outKey, string script, Dictionary <string, Script> scripts, IParameters parameters, ILogger logger) : base(string.Empty, outKey) { _script = script; _parameters = parameters; _addSelf = !parameters.Any(); foreach (var pair in scripts) { logger.Debug("Running script {0}.", pair.Value.File); try { var program = new JavaScriptParser().Parse(pair.Value.Content, new ParserOptions { Tolerant = true }); if (program.Errors != null && program.Errors.Count > 0) { logger.Warn("Javascript Parse Failed. Script: {0}.", pair.Value.Name); foreach (var error in program.Errors) { logger.Warn(error.Description); } } else { _scriptAppender.AppendLine(pair.Value.Content); } } catch (Exception e) { logger.Error("Javascript Parse Failed. Name: {0}. Script: {1}.", pair.Value.Name, pair.Value.Content); logger.Error(e.Message); } } var externalScripts = _scriptAppender.ToString(); if (externalScripts.Equals(string.Empty)) { return; } logger.Debug("Loading scripts into Javascript engine..."); logger.Debug(externalScripts.Replace("{", "{{").Replace("}", "}}")); _jint.Execute(externalScripts); Name = string.Format("Javascript({0}=>{1})", InKey, OutKey); }
public JavascriptOperation( string outKey, string script, Dictionary<string, Script> scripts, IParameters parameters, ILogger logger) : base(string.Empty, outKey) { _script = script; _parameters = parameters; _addSelf = !parameters.Any(); foreach (var pair in scripts) { logger.Debug("Running script {0}.", pair.Value.File); try { var program = new JavaScriptParser().Parse(pair.Value.Content, new ParserOptions { Tolerant = true}); if (program.Errors != null && program.Errors.Count > 0) { logger.Warn("Javascript Parse Failed. Script: {0}.", pair.Value.Name); foreach (var error in program.Errors) { logger.Warn(error.Description); } } else { _scriptAppender.AppendLine(pair.Value.Content); } } catch (Exception e) { logger.Error("Javascript Parse Failed. Name: {0}. Script: {1}.", pair.Value.Name, pair.Value.Content); logger.Error(e.Message); } } var externalScripts = _scriptAppender.ToString(); if (externalScripts.Equals(string.Empty)) return; logger.Debug("Loading scripts into Javascript engine..."); logger.Debug(externalScripts.Replace("{","{{").Replace("}","}}")); _jint.Execute(externalScripts); Name = string.Format("Javascript({0}=>{1})", InKey, OutKey); }
public CSharpOperation(string outKey, string outType, string script, Dictionary<string, Script> scripts, IParameters parameters) : base(string.Empty, outKey) { var csc = new CSharpCodeProvider(); var ca = Assembly.GetExecutingAssembly(); var cp = new CompilerParameters { GenerateInMemory = true }; var testRow = new Row(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); cp.ReferencedAssemblies.Add("mscorlib.dll"); cp.ReferencedAssemblies.Add(ca.Location); var scriptBuilder = new StringBuilder(string.Empty); foreach (var s in scripts) { scriptBuilder.AppendLine($"// {s.Value.Name} script"); scriptBuilder.AppendLine(s.Value.Content); } var castBuilder = new StringBuilder(string.Empty); if (!parameters.Any()) { castBuilder.AppendLine(string.Format("{1} {0} = ({1}) row[\"{0}\"];", OutKey, Common.ToSystemType(outType))); testRow[OutKey] = new DefaultFactory(Logger).Convert(null, outType); } else { var map = Common.GetLiteral(); foreach (var pair in parameters) { if (pair.Value.HasValue()) { castBuilder.AppendLine($"{Common.ToSystemType(pair.Value.SimpleType)} {pair.Value.Name} = {map[pair.Value.SimpleType](pair.Value.Value)};"); } else { castBuilder.AppendLine(string.Format("{1} {0} = ({1}) row[\"{0}\"];", pair.Value.Name, Common.ToSystemType(pair.Value.SimpleType))); } testRow[pair.Value.Name] = new DefaultFactory(Logger).Convert(null, pair.Value.SimpleType); } } var code = $@"using System; using System.Text; using System.Collections.Generic; using System.Linq; using Transformalize.Operations.Transform; using Transformalize.Libs.Rhino.Etl; {scriptBuilder} public class Transformer : ITransformer {{ public object Transform(Row row) {{ {castBuilder} //User's script {script} }} }}"; Logger.EntityDebug(EntityName, "Compiling this code:"); Logger.EntityDebug(EntityName, code); var res = csc.CompileAssemblyFromSource( cp, code ); if (res.Errors.Count == 0) { var type = res.CompiledAssembly.GetType("Transformer"); _transformer = (ITransformer)Activator.CreateInstance(type); try { var test = _transformer.Transform(testRow); Logger.EntityDebug(EntityName, "CSharp transform compiled and passed test. {0}", test); } catch (Exception e) { Logger.EntityDebug(EntityName, "CSharp transform compiled but failed test. {0}", e.Message); Logger.EntityDebug(EntityName, e.StackTrace); } } else { foreach (var error in res.Errors) { Logger.EntityError(EntityName, error.ToString()); } throw new TransformalizeException(Logger, EntityName, "Failed to compile code. {0}", code); } Name = $"CSharpOperation ({outKey})"; }
public CSharpOperation(string outKey, string outType, string script, Dictionary <string, Script> scripts, IParameters parameters) : base(string.Empty, outKey) { var csc = new CSharpCodeProvider(); var ca = Assembly.GetExecutingAssembly(); var cp = new CompilerParameters { GenerateInMemory = true }; var testRow = new Row(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); cp.ReferencedAssemblies.Add("mscorlib.dll"); cp.ReferencedAssemblies.Add(ca.Location); var scriptBuilder = new StringBuilder(string.Empty); foreach (var s in scripts) { scriptBuilder.AppendLine(string.Format("// {0} script", s.Value.Name)); scriptBuilder.AppendLine(s.Value.Content); } var castBuilder = new StringBuilder(string.Empty); if (!parameters.Any()) { castBuilder.AppendLine(String.Format("{1} {0} = ({1}) row[\"{0}\"];", OutKey, Common.ToSystemType(outType))); testRow[OutKey] = new DefaultFactory(Logger).Convert(null, outType); } else { var map = Common.GetLiteral(); foreach (var pair in parameters) { if (pair.Value.HasValue()) { castBuilder.AppendLine(String.Format("{0} {1} = {2};", Common.ToSystemType(pair.Value.SimpleType), pair.Value.Name, map[pair.Value.SimpleType](pair.Value.Value))); } else { castBuilder.AppendLine(String.Format("{1} {0} = ({1}) row[\"{0}\"];", pair.Value.Name, Common.ToSystemType(pair.Value.SimpleType))); } testRow[pair.Value.Name] = new DefaultFactory(Logger).Convert(null, pair.Value.SimpleType); } } var code = string.Format(@"using System; using System.Text; using System.Collections.Generic; using System.Linq; using Transformalize.Operations.Transform; using Transformalize.Libs.Rhino.Etl; {0} public class Transformer : ITransformer {{ public object Transform(Row row) {{ {1} //User's script {2} }} }}", scriptBuilder, castBuilder, script); Logger.EntityDebug(EntityName, "Compiling this code:"); Logger.EntityDebug(EntityName, code); var res = csc.CompileAssemblyFromSource( cp, code ); if (res.Errors.Count == 0) { var type = res.CompiledAssembly.GetType("Transformer"); _transformer = (ITransformer)Activator.CreateInstance(type); try { var test = _transformer.Transform(testRow); Logger.EntityDebug(EntityName, "CSharp transform compiled and passed test. {0}", test); } catch (Exception e) { Logger.EntityDebug(EntityName, "CSharp transform compiled but failed test. {0}", e.Message); Logger.EntityDebug(EntityName, e.StackTrace); } } else { foreach (var error in res.Errors) { Logger.EntityError(EntityName, error.ToString()); } throw new TransformalizeException(Logger, EntityName, "Failed to compile code. {0}", code); } Name = string.Format("CSharpOperation ({0})", outKey); }