public string Run(IConfigSectionNode node, string inputValue, string macroName, IConfigSectionNode macroParams, object context = null) { if (macroName.StartsWith(AS_PREFIX, StringComparison.InvariantCultureIgnoreCase) && macroName.Length > AS_PREFIX.Length) { var type = macroName.Substring(AS_PREFIX.Length); return(GetValueAs(inputValue, type, macroParams.Navigate("$dflt|$default").Value, macroParams.Navigate("$fmt|$format").Value)); } else if (string.Equals(macroName, "now", StringComparison.InvariantCultureIgnoreCase)) { var utc = macroParams.AttrByName("utc").ValueAsBool(false); var fmt = macroParams.Navigate("$fmt|$format").ValueAsString(); var valueAttr = macroParams.AttrByName("value"); DateTime now; if (utc) { now = App.TimeSource.UTCNow; } else { ILocalizedTimeProvider timeProvider = App.Instance; if (context is ILocalizedTimeProvider) { timeProvider = (ILocalizedTimeProvider)context; } now = timeProvider.LocalizedTime; } // We inspect the "value" param that may be provided for testing purposes if (valueAttr.Exists) { now = valueAttr.Value.AsDateTimeFormat(now, fmt, utc ? DateTimeStyles.AssumeUniversal : DateTimeStyles.AssumeLocal); } return(fmt == null?now.ToString() : now.ToString(fmt)); } else if (string.Equals(macroName, "ctx-name", StringComparison.InvariantCultureIgnoreCase)) { if (context is INamed) { return(((INamed)context).Name); } } return(inputValue); }
public virtual string Run(IConfigSectionNode node, string inputValue, string macroName, IConfigSectionNode macroParams, object context = null) { if (macroName.StartsWith(AS_PREFIX, StringComparison.InvariantCultureIgnoreCase) && macroName.Length > AS_PREFIX.Length) { var type = macroName.Substring(AS_PREFIX.Length); return(GetValueAs(inputValue, type, macroParams.Navigate("$dflt|$default").Value, macroParams.Navigate("$fmt|$format").Value)); } else if (string.Equals(macroName, "now", StringComparison.InvariantCultureIgnoreCase)) { var utc = macroParams.AttrByName("utc").ValueAsBool(false); var fmt = macroParams.Navigate("$fmt|$format").ValueAsString(); var valueAttr = macroParams.AttrByName("value"); var now = Ambient.UTCNow; if (!utc) { ILocalizedTimeProvider timeProvider = context as ILocalizedTimeProvider; if (timeProvider == null && context is IApplicationComponent cmp) { timeProvider = cmp.ComponentDirector as ILocalizedTimeProvider; if (timeProvider == null) { timeProvider = cmp.App; } } now = timeProvider != null ? timeProvider.LocalizedTime : now.ToLocalTime(); } // We inspect the "value" param that may be provided for testing purposes if (valueAttr.Exists) { now = valueAttr.Value.AsDateTimeFormat(now, fmt, utc ? DateTimeStyles.AssumeUniversal : DateTimeStyles.AssumeLocal); } return(fmt == null?now.ToString() : now.ToString(fmt)); } else if (string.Equals(macroName, "ctx-name", StringComparison.InvariantCultureIgnoreCase)) { if (context is Collections.INamed) { return(((Collections.INamed)context).Name); } } return(inputValue); }
/// <summary> /// Applies config values to fields/properties as specified by config attributes /// </summary> public static void Apply(object entity, IConfigSectionNode node) { if (entity == null || node == null) { return; } var etp = entity.GetType(); //20130708 DKh - support for [ConfigMacroContext] injection var macroAttr = etp.GetCustomAttributes(typeof(ConfigMacroContextAttribute), true).FirstOrDefault() as ConfigMacroContextAttribute; if (macroAttr != null) { node.Configuration.MacroRunnerContext = entity; } //--- var cattr = etp.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault() as ConfigAttribute; if (cattr != null)//rebase root config node per supplied path { cattr.evalAttributeVars(etp); var path = cattr.Path ?? StringConsts.NULL_STRING; node = node.Navigate(path) as ConfigSectionNode; if (node == null) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_NAVIGATION_REQUIRED_ERROR, cattr.Path)); } } var members = getAllFieldsOrProps(etp); foreach (var mem in members) { var mattr = mem.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault() as ConfigAttribute; if (mattr == null) { continue; } //20130708 DKh - default attribute name taken from member name if path==null if (string.IsNullOrWhiteSpace(mattr.Path)) { mattr.Path = GetConfigPathsForMember(mem); } //--- mattr.evalAttributeVars(etp); var mnode = node.Navigate(mattr.Path); if (mem.MemberType == MemberTypes.Field) { var finf = (FieldInfo)mem; if (typeof(IConfigSectionNode).IsAssignableFrom(finf.FieldType)) { if (finf.IsInitOnly) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, finf.Name)); } var snode = mnode as IConfigSectionNode; if (snode == null) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR, etp.FullName, finf.Name)); } finf.SetValue(entity, mnode); } else if (typeof(IConfigurable).IsAssignableFrom(finf.FieldType)) { var snode = mnode as IConfigSectionNode; if (snode == null) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR, etp.FullName, finf.Name)); } if (finf.GetValue(entity) != null) { ((IConfigurable)finf.GetValue(entity)).Configure(snode); } } else { if (finf.IsInitOnly) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, finf.Name)); } if (mnode.Exists && mnode.VerbatimValue != null) { finf.SetValue(entity, getVal(mnode, finf.FieldType, etp.FullName, finf.Name)); } else if (mattr.Default != null) { finf.SetValue(entity, mattr.Default); } } } else if (mem.MemberType == MemberTypes.Property) { var pinf = (PropertyInfo)mem; if (typeof(IConfigSectionNode).IsAssignableFrom(pinf.PropertyType)) { if (!pinf.CanWrite) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, pinf.Name)); } var snode = mnode as IConfigSectionNode; if (snode == null) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR, etp.FullName, pinf.Name)); } pinf.SetValue(entity, mnode, null); } else if (typeof(IConfigurable).IsAssignableFrom(pinf.PropertyType)) { var snode = mnode as IConfigSectionNode; if (snode == null) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR, etp.FullName, pinf.Name)); } if (pinf.GetValue(entity, null) != null) { ((IConfigurable)pinf.GetValue(entity, null)).Configure(snode); } } else { if (!pinf.CanWrite) { throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, pinf.Name)); } if (mnode.Exists && mnode.VerbatimValue != null) { pinf.SetValue(entity, getVal(mnode, pinf.PropertyType, etp.FullName, pinf.Name), null); } else if (mattr.Default != null) { pinf.SetValue(entity, mattr.Default, null); } } } }//for members }
/// <summary> /// Override to compile a RDBMS Table /// </summary> protected virtual void DoColumn(IConfigSectionNode columnNode, RDBMSEntity table, StringBuilder sb, ref bool firstColumn, Outputs outputs) { var colComment = columnNode.AttrByName(SCRIPT_COMMENT_ATTR).Value; if (colComment.IsNotNullOrWhiteSpace()) sb.AppendLine(" -- {0}".Args( colComment ) ); var columnName = columnNode.Value; if (columnName.IsNullOrWhiteSpace()) { m_CompileErrors.Add(new SchemaCompilationException(columnNode.RootPath, "Table '{0}' missing column name.".Args(table.OriginalName))); return; } var column = new RDBMSEntity(table, columnNode, RDBMSEntityType.Column, columnName, columnNode.AttrByName(SHORT_NAME_ATTR).Value ?? columnName); TransformEntityName(column); var typeNode = columnNode.Navigate(TYPE_ATTR + "|$"+TYPE_ATTR); if (typeNode==null || typeNode.VerbatimValue.IsNullOrWhiteSpace()) { m_CompileErrors.Add(new SchemaCompilationException(columnNode.RootPath, "Column '{0}' missing {1} attribute.".Args(columnName, TYPE_ATTR))); return; } var columnType = typeNode.Value; var type = new RDBMSEntity(column, typeNode, RDBMSEntityType.Domain, columnType); TransformEntityName(type); var domain = CreateDomain("{0}.{1}::{2}".Args(table.OriginalName, column.OriginalName, type.OriginalName), type.OriginalName, typeNode); if (domain==null) { m_CompileErrors.Add(new SchemaCompilationException(columnNode.RootPath, "Domain could not be created: " +type.TransformedName )); return; } domain.TransformColumnName(this, column); if (!firstColumn) sb.AppendLine(","); #region Column Line { var cn = GetQuotedIdentifierName(RDBMSEntityType.Column, column.TransformedName); var tn = GetQuotedIdentifierName(RDBMSEntityType.Domain, domain.GetTypeName(this)); var required = (domain.GetColumnRequirement(this) ?? false) || columnNode.AttrByName(REQUIRED_ATTR).ValueAsBool(); var nn = TransformKeywordCase( GetColumnNullNotNullClause(column, required) ); var auto = domain.GetColumnAutoGeneratedScript(this, column, outputs); var dfltValue = columnNode.AttrByName(DEFAULT_ATTR).Value ?? domain.GetColumnDefaultScript(this, column, outputs); var dflt = dfltValue.IsNotNullOrWhiteSpace()? "{0} {1}".Args(TransformKeywordCase("default"), EscapeString(dfltValue)) : string.Empty; var chk = domain.GetColumnCheckScript(this, column, outputs); var cmntValue = columnNode.AttrByName(COMMENT_ATTR).Value; var cmnt = cmntValue.IsNotNullOrWhiteSpace()? "{0} {1}".Args(TransformKeywordCase("comment"), EscapeString(cmntValue)) : string.Empty; sb.Append( FormatColumnStatement(cn, tn, nn, auto, dflt, chk, cmnt) ); } #endregion firstColumn = false; foreach(var colSubNode in columnNode.Children) { if (colSubNode.IsSameName(PRIMARY_KEY_SECTION) || colSubNode.IsSameName(REFERENCE_SECTION)) { var keyType = colSubNode.IsSameName(PRIMARY_KEY_SECTION) ? RDBMSEntityType.PrimaryKey : RDBMSEntityType.Reference; var keyName = colSubNode.Value; if (keyName.IsNullOrWhiteSpace()) keyName = column.OriginalShortName; var key = new RDBMSEntity(column, colSubNode, keyType, keyName, colSubNode.AttrByName(SHORT_NAME_ATTR).Value); TransformEntityName(key); } else if (colSubNode.IsSameName(TYPE_ATTR)) { } //type may be used as section as well else m_CompileErrors.Add(new SchemaCompilationException(colSubNode.RootPath, "Unrecognized item inside '{0}.{1}' column, section '{2}'" .Args(table.OriginalName, columnName, colSubNode.Name))); } }
/// <summary> /// Applies config values to fields/properties as specified by config attributes /// </summary> public static void Apply(object entity, IConfigSectionNode node) { if (entity==null || node==null) return; var etp = entity.GetType(); //20130708 DKh - support for [ConfigMacroContext] injection var macroAttr = etp.GetCustomAttributes(typeof(ConfigMacroContextAttribute), true).FirstOrDefault() as ConfigMacroContextAttribute; if (macroAttr!=null) node.Configuration.MacroRunnerContext = entity; //--- var cattr = etp.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault() as ConfigAttribute; if (cattr!=null)//rebase root config node per supplied path { cattr.evalAttributeVars(etp); var path = cattr.Path ?? StringConsts.NULL_STRING; node = node.Navigate(path) as ConfigSectionNode; if (node==null) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_NAVIGATION_REQUIRED_ERROR, cattr.Path)); } var members = getAllFieldsOrProps( etp ); foreach(var mem in members) { var mattr = mem.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault() as ConfigAttribute; if (mattr==null) continue; //20130708 DKh - default attribute name taken from member name if path==null if (string.IsNullOrWhiteSpace(mattr.Path)) mattr.Path = GetConfigPathsForMember(mem); //--- mattr.evalAttributeVars(etp); var mnode = node.Navigate(mattr.Path); if (mem.MemberType == MemberTypes.Field) { var finf = (FieldInfo)mem; if (typeof(IConfigSectionNode).IsAssignableFrom(finf.FieldType)) { if (finf.IsInitOnly) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, finf.Name)); var snode = mnode as IConfigSectionNode; if (snode==null) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR, etp.FullName, finf.Name)); finf.SetValue(entity, mnode); } else if (typeof(IConfigurable).IsAssignableFrom(finf.FieldType)) { var snode = mnode as IConfigSectionNode; if (snode==null) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR, etp.FullName, finf.Name)); if (finf.GetValue(entity)!=null) ((IConfigurable)finf.GetValue(entity)).Configure(snode); } else { if (finf.IsInitOnly) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, finf.Name)); if (mnode.Exists && mnode.VerbatimValue!=null) finf.SetValue(entity, getVal(mnode, finf.FieldType, etp.FullName, finf.Name)); else if (mattr.Default!=null) finf.SetValue(entity, mattr.Default); } } else if (mem.MemberType == MemberTypes.Property) { var pinf = (PropertyInfo)mem; if (typeof(IConfigSectionNode).IsAssignableFrom(pinf.PropertyType)) { if (!pinf.CanWrite) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, pinf.Name)); var snode = mnode as IConfigSectionNode; if (snode==null) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR, etp.FullName, pinf.Name)); pinf.SetValue(entity, mnode, null); } else if (typeof(IConfigurable).IsAssignableFrom(pinf.PropertyType)) { var snode = mnode as IConfigSectionNode; if (snode==null) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR, etp.FullName, pinf.Name)); if (pinf.GetValue(entity, null)!=null) ((IConfigurable)pinf.GetValue(entity, null)).Configure(snode); } else { if (!pinf.CanWrite) throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, pinf.Name)); if (mnode.Exists && mnode.VerbatimValue!=null) pinf.SetValue(entity, getVal(mnode, pinf.PropertyType, etp.FullName, pinf.Name), null); else if (mattr.Default!=null) pinf.SetValue(entity, mattr.Default, null); } } }//for members }
public string Run(IConfigSectionNode node, string inputValue, string macroName, IConfigSectionNode macroParams, object context = null) { if (macroName.StartsWith(AS_PREFIX, StringComparison.InvariantCultureIgnoreCase) && macroName.Length > AS_PREFIX.Length) { var type = macroName.Substring(AS_PREFIX.Length); return GetValueAs(inputValue, type, macroParams.Navigate("$dflt|$default").Value, macroParams.Navigate("$fmt|$format").Value); } else if (string.Equals(macroName, "now", StringComparison.InvariantCultureIgnoreCase)) { var utc = macroParams.AttrByName("utc").ValueAsBool(false); var fmt = macroParams.Navigate("$fmt|$format").ValueAsString(); var valueAttr = macroParams.AttrByName("value"); DateTime now; if (utc) now = App.TimeSource.UTCNow; else { ILocalizedTimeProvider timeProvider = App.Instance; if (context is ILocalizedTimeProvider) timeProvider = (ILocalizedTimeProvider) context; now = timeProvider.LocalizedTime; } // We inspect the "value" param that may be provided for testing purposes if (valueAttr.Exists) now = valueAttr.Value.AsDateTimeFormat(now, fmt, utc ? DateTimeStyles.AssumeUniversal : DateTimeStyles.AssumeLocal); return fmt == null ? now.ToString() : now.ToString(fmt); } else if (string.Equals(macroName, "ctx-name", StringComparison.InvariantCultureIgnoreCase)) { if (context is INamed) return ((INamed)context).Name; } return inputValue; }
/// <summary> /// Override to compile a RDBMS Table /// </summary> protected virtual void DoColumn(IConfigSectionNode columnNode, RDBMSEntity table, StringBuilder sb, ref bool firstColumn, Outputs outputs) { var colComment = columnNode.AttrByName(SCRIPT_COMMENT_ATTR).Value; if (colComment.IsNotNullOrWhiteSpace()) { sb.AppendLine(" -- {0}".Args(colComment)); } var columnName = columnNode.Value; if (columnName.IsNullOrWhiteSpace()) { m_CompileErrors.Add(new SchemaCompilationException(columnNode.RootPath, "Table '{0}' missing column name.".Args(table.OriginalName))); return; } var column = new RDBMSEntity(table, columnNode, RDBMSEntityType.Column, columnName, columnNode.AttrByName(SHORT_NAME_ATTR).Value ?? columnName); TransformEntityName(column); var typeNode = columnNode.Navigate(TYPE_ATTR + "|$" + TYPE_ATTR); if (typeNode == null || typeNode.VerbatimValue.IsNullOrWhiteSpace()) { m_CompileErrors.Add(new SchemaCompilationException(columnNode.RootPath, "Column '{0}' missing {1} attribute.".Args(columnName, TYPE_ATTR))); return; } var columnType = typeNode.Value; var type = new RDBMSEntity(column, typeNode, RDBMSEntityType.Domain, columnType); TransformEntityName(type); var domain = CreateDomain("{0}.{1}::{2}".Args(table.OriginalName, column.OriginalName, type.OriginalName), type.OriginalName, typeNode); if (domain == null) { m_CompileErrors.Add(new SchemaCompilationException(columnNode.RootPath, "Domain could not be created: " + type.TransformedName)); return; } domain.TransformColumnName(this, column); if (!firstColumn) { sb.AppendLine(","); } #region Column Line { var cn = GetQuotedIdentifierName(RDBMSEntityType.Column, column.TransformedName); var tn = GetQuotedIdentifierName(RDBMSEntityType.Domain, domain.GetTypeName(this)); var required = (domain.GetColumnRequirement(this) ?? false) || columnNode.AttrByName(REQUIRED_ATTR).ValueAsBool(); var nn = TransformKeywordCase(GetColumnNullNotNullClause(column, required)); var auto = domain.GetColumnAutoGeneratedScript(this, column, outputs); var dfltValue = columnNode.AttrByName(DEFAULT_ATTR).Value ?? domain.GetColumnDefaultScript(this, column, outputs); var dflt = dfltValue.IsNotNullOrWhiteSpace()? "{0} {1}".Args(TransformKeywordCase("default"), EscapeString(dfltValue)) : string.Empty; var chk = domain.GetColumnCheckScript(this, column, outputs); var cmntValue = columnNode.AttrByName(COMMENT_ATTR).Value; var cmnt = cmntValue.IsNotNullOrWhiteSpace()? "{0} {1}".Args(TransformKeywordCase("comment"), EscapeString(cmntValue)) : string.Empty; sb.Append(FormatColumnStatement(cn, tn, nn, auto, dflt, chk, cmnt)); } #endregion firstColumn = false; foreach (var colSubNode in columnNode.Children) { if (colSubNode.IsSameName(PRIMARY_KEY_SECTION) || colSubNode.IsSameName(REFERENCE_SECTION)) { var keyType = colSubNode.IsSameName(PRIMARY_KEY_SECTION) ? RDBMSEntityType.PrimaryKey : RDBMSEntityType.Reference; var keyName = colSubNode.Value; if (keyName.IsNullOrWhiteSpace()) { keyName = column.OriginalShortName; } var key = new RDBMSEntity(column, colSubNode, keyType, keyName, colSubNode.AttrByName(SHORT_NAME_ATTR).Value); TransformEntityName(key); } else if (colSubNode.IsSameName(TYPE_ATTR)) { } //type may be used as section as well else { m_CompileErrors.Add(new SchemaCompilationException(colSubNode.RootPath, "Unrecognized item inside '{0}.{1}' column, section '{2}'" .Args(table.OriginalName, columnName, colSubNode.Name))); } } }
/// <summary> /// Takes standard config path relative to the calling method/story and navigates it. Pass "!" at the beginning to make path required /// </summary> public IConfigNode Nav(string path) => Data.Navigate(path);