public void FillFromDb(int value, string name, string description = null) { string nameCs = EnumNameConverter.MakeValidIdentifier(name); EnumModelValue modelValue = Values.SingleOrDefault(e => e.Value == value); if (modelValue == null) { /* Was found in DB but has not been discovered in code. Must have been added in Db. */ modelValue = new EnumModelValue(); modelValue.Value = value; modelValue.NameCs = nameCs; modelValue.Description = description; modelValue.IsActive = false; Values.Add(modelValue); } else { /* Was found both in DB and in code.*/ //modelValue.Name = name; modelValue.NameCs = EnumNameConverter.MakeValidIdentifier(name); /* Refresh CS name. */ modelValue.Description = description; } modelValue.IsInDB = true; }
public void FillFromCode(int value, string nameCs, bool isActive) { AssertNoDuplicates(value, nameCs); EnumModelValue modelValue = new EnumModelValue(); modelValue.Value = value; modelValue.NameCs = nameCs; modelValue.IsActive = isActive; modelValue.IsInDB = false; /* Assume */ Values.Add(modelValue); }
private void ExtractActionAttributes(SyntaxTree tree, SyntaxNode syntaxRoot) { foreach (EnumDeclarationSyntax enumeration in syntaxRoot.DescendantNodes().OfType <EnumDeclarationSyntax>()) { EnumModel model = new EnumModel(); model.Name = enumeration.Identifier.ToString(); /* get attributes on the enumeration declaration*/ SyntaxList <AttributeListSyntax> attributesList = enumeration.AttributeLists; foreach (AttributeListSyntax attibutes in attributesList) { foreach (AttributeSyntax attribute in attibutes.Attributes) { string attributeName = attribute.Name.ToString(); if (attributeName == "EnumSqlSelect" || attributeName == "EnumSqlSelectAttribute") { /* extract SqlSelect statement from EnumSqlSelectAttribute */ if (attribute.ArgumentList.Arguments.Count != 1) { throw new ApplicationException(string.Format("Invalid number of arguments {0} (expected 1), in EnumSqlSelectAttribute for {1} enumeration.", attribute.ArgumentList.Arguments.Count, model.Name)); } model.SqlSelect = GetAttributeValue(attribute.ArgumentList.Arguments[0]); } ModelSql sql = FindModelSql(enumeration.OpenBraceToken.FullSpan.Start); if (sql == null) { throw new ApplicationException($"EnumSqlCnn attribute was not found for the enumeration {model.Name}."); } model.SqlProvider = sql.SqlProvider; model.SqlDatasource = sql.SqlDatasource; } } if (string.IsNullOrWhiteSpace(model.SqlSelect)) { continue; } EnumModels.Add(model); model.SpanStart = enumeration.OpenBraceToken.SpanStart + 1; model.SpanEnd = enumeration.CloseBraceToken.SpanStart; var lineSpan = tree.GetLineSpan(enumeration.OpenBraceToken.Span); model.OpenBraceCharacterPosition = lineSpan.StartLinePosition.Character; /* loop all enumeration values*/ foreach (EnumMemberDeclarationSyntax syntax in enumeration.Members) { if (syntax.EqualsValue == null) { /* if enumeration option has no value we skip it, as if not exist*/ continue; } EnumModelValue value = new EnumModelValue(); model.Values.Add(value); value.NameCs = syntax.Identifier.ToString(); value.IsActive = true; string svalue = syntax.EqualsValue.Value.ToString(); value.Value = int.Parse(svalue); } } }
private void ParseSourceEnumerations(string source) { SyntaxTree tree = CSharpSyntaxTree.ParseText(source); SyntaxNode syntaxRoot = tree.GetRoot(); foreach (EnumDeclarationSyntax enumeration in syntaxRoot.DescendantNodes().OfType <EnumDeclarationSyntax>()) { EnumModel model = new EnumModel(); model.Name = enumeration.Identifier.ToString(); /* get attributes on the enumeration declaration*/ SyntaxList <AttributeListSyntax> attributesList = enumeration.AttributeLists; foreach (AttributeListSyntax attibutes in attributesList) { foreach (AttributeSyntax attribute in attibutes.Attributes) { string attributeName = attribute.Name.ToString(); if (attributeName == "EnumSqlSelect" || attributeName == "EnumSqlSelectAttribute") { /* extract SqlSelect statement from EnumSqlSelectAttribute */ if (attribute.ArgumentList.Arguments.Count != 1) { throw new ApplicationException(string.Format("Invalid number of arguments {0} (expected 1), in EnumSqlSelectAttribute for {1} enumeration.", attribute.ArgumentList.Arguments.Count, model.Name)); } model.SqlSelect = GetAttributeValue(attribute.ArgumentList.Arguments[0]); } if (attributeName == "EnumSqlCnn" || attributeName == "EnumSqlCnnAttribute") { if (attribute.ArgumentList.Arguments.Count != 2) { throw new ApplicationException(string.Format("Invalid number of arguments {0} (expected 2), in EnumSqlCnnAttribute for {1} enumeration.", attribute.ArgumentList.Arguments.Count, model.Name)); } model.SqlProvider = GetAttributeValue(attribute.ArgumentList.Arguments[0]); model.SqlDatasource = GetAttributeValue(attribute.ArgumentList.Arguments[1]); } } } if (string.IsNullOrWhiteSpace(model.SqlSelect)) { continue; } EnumModels.Add(model); model.SpanStart = enumeration.OpenBraceToken.SpanStart + 1; model.SpanEnd = enumeration.CloseBraceToken.SpanStart; var lineSpan = tree.GetLineSpan(enumeration.OpenBraceToken.Span); model.OpenBraceCharacterPosition = lineSpan.StartLinePosition.Character; /* loop all enumeration values*/ foreach (EnumMemberDeclarationSyntax syntax in enumeration.Members) { if (syntax.EqualsValue == null) { /* if enumeration option has no value we skip it, as if not exist*/ continue; } EnumModelValue value = new EnumModelValue(); model.Values.Add(value); value.NameCs = syntax.Identifier.ToString(); value.IsActive = true; string svalue = syntax.EqualsValue.Value.ToString(); value.Value = int.Parse(svalue); //#if TRIVIA && DEBUG // if (model.LeadingTrivia == null) // { // string triviaS = ""; // var triviaList = syntax.GetLeadingTrivia(); // foreach (SyntaxTrivia trivia in triviaList) // { // triviaS += trivia.ToFullString(); // } // model.LeadingTrivia = triviaS; // } //#endif } } }