Example #1
0
        private TableDecoder FindDecoderFromHeaderText(string lastHeader)
        {
            TableDecoder decoder = (from key in CommonHeaderContentMap.Keys
                                    where lastHeader.ContainsIgnoreCase(key)
                                    select CommonHeaderContentMap[key]).FirstOrDefault();

            return(decoder);
        }
 private static IEnumerable<ParameterDefinition> ParseParameterTable(IMarkdownTable table, ParameterLocation location, TableDecoder decoder, bool navigationProperties = false)
 {
     var records = from r in table.RowValues
                   select new ParameterDefinition
                   {
                       Name = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["name"]),
                       Type = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["type"]).ParseParameterDataType(defaultValue: ParameterDataType.String),
                       Description = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"]),
                       Required = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"]).IsRequired(),
                       Location = location,
                       IsNavigatable = navigationProperties
                   };
     return records;
 }
        /// <summary>
        /// Convert a markdown table into ErrorDefinition objects
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private static IEnumerable<ErrorDefinition> ParseErrorTable(IMarkdownTable table, TableDecoder decoder)
        {
            var records = from r in table.RowValues
                          select new ErrorDefinition
                          {
                              HttpStatusCode = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["httpStatusCode"]),
                              HttpStatusMessage = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["httpStatusMessage"]),
                              ErrorCode = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["errorCode"]),
                              Description = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"])
                          };

            return records;
        }
 private static IEnumerable<EnumerationDefinition> ParseEnumerationTable(IMarkdownTable table, TableDecoder decoder)
 {
     var records = from r in table.RowValues
                   select new EnumerationDefinition
                   {
                       Value = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["value"]),
                       Description = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"])
                   };
     return records;
 }
 private static IEnumerable<AuthScopeDefinition> ParseAuthScopeTable(IMarkdownTable table, TableDecoder decoder)
 {
     var records = from r in table.RowValues
                   select new AuthScopeDefinition
                   {
                       Scope = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["scope"]),
                       Title = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["title"]),
                       Description = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"]),
                       Required = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["required"]).ToBoolean()
                   };
     return records;
 }
        /// <summary>
        /// Convert a tablespec block into one of our internal object model representations
        /// </summary>
        /// <param name="tableSpecBlock"></param>
        /// <param name="lastHeaderBlock"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public TableDefinition ParseTableSpec(Block tableSpecBlock, Block lastHeaderBlock, out ValidationError[] errors)
        {
            List<ValidationError> discoveredErrors = new List<ValidationError>();
            List<ItemDefinition> items = new List<ItemDefinition>();

            var tableShape = tableSpecBlock.Table;

            TableDecoder decoder = new TableDecoder { Type = TableBlockType.Unknown };
            string headerText = null;
            // Try matching based on header
            if (null != lastHeaderBlock && null != lastHeaderBlock.Content)
            {
                headerText = lastHeaderBlock.Content;
                var matchingDecoder = FindDecoderFromHeaderText(headerText);
                if (null != matchingDecoder)
                    decoder = matchingDecoder;
            }

            // Try matching based on shape
            if (decoder.Type == TableBlockType.Unknown && null != tableSpecBlock.Table)
            {
                var matchingDecoder = FindDecoderFromShape(tableShape);
                if (null != matchingDecoder)
                    decoder = matchingDecoder;
            }

            switch (decoder.Type)
            {
                case TableBlockType.ErrorCodes:
                    items.AddRange(ParseErrorTable(tableShape, decoder));
                    break;

                case TableBlockType.PathParameters:
                    items.AddRange(ParseParameterTable(tableShape, ParameterLocation.Path, decoder));
                    break;

                case TableBlockType.ResourcePropertyDescriptions:
                case TableBlockType.RequestObjectProperties:
                case TableBlockType.ResponseObjectProperties:
                    items.AddRange(ParseParameterTable(tableShape, ParameterLocation.JsonObject, decoder));
                    break;
                case TableBlockType.ResourceNavigationPropertyDescriptions:
                    items.AddRange(ParseParameterTable(tableShape, ParameterLocation.JsonObject, decoder, true));
                    break;

                case TableBlockType.HttpHeaders:
                    items.AddRange(ParseParameterTable(tableShape, ParameterLocation.Header, decoder));
                    break;

                case TableBlockType.QueryStringParameters:
                    items.AddRange(ParseParameterTable(tableShape, ParameterLocation.QueryString, decoder));
                    break;

                case TableBlockType.EnumerationValues:
                    items.AddRange(ParseEnumerationTable(tableShape, decoder));
                    break;

                case TableBlockType.AuthScopes:
                    items.AddRange(ParseAuthScopeTable(tableShape, decoder));
                    break;

                case TableBlockType.Unknown:
                    discoveredErrors.Add(new ValidationMessage(null, "Ignored unclassified table: headerText='{0}', tableHeaders='{1}'", headerText, tableShape.ColumnHeaders != null ? tableShape.ColumnHeaders.ComponentsJoinedByString(",") : "null"));
                    break;
                default:
                    discoveredErrors.Add(new ValidationMessage(null, "Ignored table: classification='{2}', headerText='{0}', tableHeaders='{1}'", headerText, tableShape.ColumnHeaders != null ? tableShape.ColumnHeaders.ComponentsJoinedByString(",") : "null", decoder.Type));
                    break;
            }

            errors = discoveredErrors.ToArray();

            return new TableDefinition(decoder.Type, items, headerText);
        }
Example #7
0
        /// <summary>
        /// Convert a tablespec block into one of our internal object model representations
        /// </summary>
        /// <param name="tableSpecBlock"></param>
        /// <param name="lastHeaderBlock"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public TableDefinition ParseTableSpec(Block tableSpecBlock, Block lastHeaderBlock, out ValidationError[] errors)
        {
            List <ValidationError> discoveredErrors = new List <ValidationError>();
            List <ItemDefinition>  items            = new List <ItemDefinition>();

            var tableShape = tableSpecBlock.Table;

            TableDecoder decoder = new TableDecoder {
                Type = TableBlockType.Unknown
            };
            string headerText = null;

            // Try matching based on header
            if (null != lastHeaderBlock && null != lastHeaderBlock.Content)
            {
                headerText = lastHeaderBlock.Content;
                var matchingDecoder = FindDecoderFromHeaderText(headerText);
                if (null != matchingDecoder)
                {
                    decoder = matchingDecoder;
                }
            }

            // Try matching based on shape
            if (decoder.Type == TableBlockType.Unknown && null != tableSpecBlock.Table)
            {
                var matchingDecoder = FindDecoderFromShape(tableShape);
                if (null != matchingDecoder)
                {
                    decoder = matchingDecoder;
                }
            }

            switch (decoder.Type)
            {
            case TableBlockType.ErrorCodes:
                items.AddRange(ParseErrorTable(tableShape, decoder));
                break;

            case TableBlockType.PathParameters:
                items.AddRange(ParseParameterTable(tableShape, ParameterLocation.Path, decoder));
                break;

            case TableBlockType.ResourcePropertyDescriptions:
            case TableBlockType.RequestObjectProperties:
            case TableBlockType.ResponseObjectProperties:
                items.AddRange(ParseParameterTable(tableShape, ParameterLocation.JsonObject, decoder));
                break;

            case TableBlockType.ResourceNavigationPropertyDescriptions:
                items.AddRange(ParseParameterTable(tableShape, ParameterLocation.JsonObject, decoder, true));
                break;

            case TableBlockType.HttpHeaders:
                items.AddRange(ParseParameterTable(tableShape, ParameterLocation.Header, decoder));
                break;

            case TableBlockType.QueryStringParameters:
                items.AddRange(ParseParameterTable(tableShape, ParameterLocation.QueryString, decoder));
                break;

            case TableBlockType.EnumerationValues:
                items.AddRange(ParseEnumerationTable(tableShape, decoder));
                break;

            case TableBlockType.AuthScopes:
                items.AddRange(ParseAuthScopeTable(tableShape, decoder));
                break;

            case TableBlockType.Unknown:
                discoveredErrors.Add(new ValidationMessage(null, "Ignored unclassified table: headerText='{0}', tableHeaders='{1}'", headerText, tableShape.ColumnHeaders != null ? tableShape.ColumnHeaders.ComponentsJoinedByString(",") : "null"));
                break;

            default:
                discoveredErrors.Add(new ValidationMessage(null, "Ignored table: classification='{2}', headerText='{0}', tableHeaders='{1}'", headerText, tableShape.ColumnHeaders != null ? tableShape.ColumnHeaders.ComponentsJoinedByString(",") : "null", decoder.Type));
                break;
            }

            errors = discoveredErrors.ToArray();

            return(new TableDefinition(decoder.Type, items, headerText));
        }
Example #8
0
        private static IEnumerable <AuthScopeDefinition> ParseAuthScopeTable(IMarkdownTable table, TableDecoder decoder)
        {
            var records = from r in table.RowValues
                          select new AuthScopeDefinition
            {
                Scope       = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["scope"]),
                Title       = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["title"]),
                Description = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"]),
                Required    = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["required"]).ToBoolean()
            };

            return(records);
        }
Example #9
0
        private static IEnumerable <EnumerationDefinition> ParseEnumerationTable(IMarkdownTable table, TableDecoder decoder)
        {
            var records = from r in table.RowValues
                          select new EnumerationDefinition
            {
                Value       = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["value"]),
                Description = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"])
            };

            return(records);
        }
Example #10
0
        private static IEnumerable <ParameterDefinition> ParseParameterTable(IMarkdownTable table, ParameterLocation location, TableDecoder decoder, bool navigationProperties = false)
        {
            var records = from r in table.RowValues
                          select new ParameterDefinition
            {
                Name          = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["name"]),
                Type          = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["type"]).ParseParameterDataType(defaultValue: ParameterDataType.String),
                Description   = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"]),
                Required      = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"]).IsRequired(),
                Location      = location,
                IsNavigatable = navigationProperties
            };

            return(records);
        }
Example #11
0
        /// <summary>
        /// Convert a markdown table into ErrorDefinition objects
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private static IEnumerable <ErrorDefinition> ParseErrorTable(IMarkdownTable table, TableDecoder decoder)
        {
            var records = from r in table.RowValues
                          select new ErrorDefinition
            {
                HttpStatusCode    = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["httpStatusCode"]),
                HttpStatusMessage = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["httpStatusMessage"]),
                ErrorCode         = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["errorCode"]),
                Description       = r.ValueForColumn(table, decoder.ParseRule.ColumnNames["description"])
            };

            return(records);
        }