示例#1
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(Statement))
            {
                return(false);
            }
            var modelName = bindingContext.ModelName;
            var body      = actionContext.Request.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
            var headers   = actionContext.Request.Content.Headers;

            try
            {
                var       jsonModelReader = new JsonModelReader(headers, body);
                Statement statement       = jsonModelReader.ReadAs <Statement>().GetAwaiter().GetResult();
                if (statement != null)
                {
                    var validator = new StatementValidator();
                    var results   = validator.Validate(statement);
                    if (!results.IsValid)
                    {
                        results.AddToModelState(bindingContext.ModelState, null);
                        return(false);
                    }
                    bindingContext.Model = statement;
                    return(true);
                }
            } catch (JsonModelReaderException ex)
            {
                bindingContext.ModelState.AddModelError(modelName, ex);
                return(false);
            }

            return(false);
        }
示例#2
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            // Specify a default argument name if none is set by ModelBinderAttribute
            //var modelName = bindingContext.BinderModelName;
            //if (string.IsNullOrEmpty(modelName))
            //{
            //    modelName = "statements";
            //}

            var request = bindingContext.ActionContext.HttpContext.Request;

            try
            {
                var       jsonModelReader = new JsonModelReader(request.Headers, request.Body);
                Statement statement       = await jsonModelReader.ReadAs <Statement>();

                if (statement != null)
                {
                    bindingContext.Result = ModelBindingResult.Success(statement);
                }
                else
                {
                    bindingContext.Result = ModelBindingResult.Failed();
                }
            }
            catch (JsonModelReaderException ex)
            {
                throw new BadRequestException(ex.Message);
            }
        }
示例#3
0
        public void Execute(GeneratorExecutionContext context)
        {
            var modelFiles = context.AdditionalFiles.Where(f => f.Path.EndsWith(".model.json"));
            var generator  = new Implementation.TemplateBased.T4.T4Generator();

            context.AnalyzerConfigOptions.GlobalOptions
            .TryGetValue("build_property.RootNamespace", out var rootNamespace);
            context.AnalyzerConfigOptions.GlobalOptions
            .TryGetValue("build_property.MSBuildProjectDirectory", out var projectDirectory);

            foreach (var file in modelFiles)
            {
                var options = context.AnalyzerConfigOptions.GetOptions(file);
                options.TryGetValue("build_metadata.AdditionalFiles.CustomToolNamespace", out var namespaceName);

                var entities = JsonModelReader.Read(file.GetText().ToString());
                var model    = new Common.EntitiesModel.Model
                {
                    Entities      = entities,
                    NamespaceName = GetNamespace(namespaceName, file.Path, projectDirectory, rootNamespace)
                };

                var code = generator.Generate(model);

                context.AddSource(Path.GetFileName(file.Path), code);
            }
        }
示例#4
0
        protected override string Generate(string modelContent, string namspaceName)
        {
            var model     = JsonModelReader.Read(modelContent);
            var fullModel = new Model
            {
                Entities      = model,
                NamespaceName = namspaceName
            };
            var generator = new T4Generator();

            return(generator.Generate(fullModel));
        }
示例#5
0
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            var entities = JsonModelReader.Read(inputFileContent);
            var model    = new Model
            {
                Entities      = entities,
                NamespaceName = FileNamespace
            };

            var generator  = new T4Generator();
            var outContent = generator.Generate(model);

            return(Encoding.UTF8.GetBytes(outContent));
        }
        public void ReadJsonTest()
        {
            var jsonContent =
                "[{" +
                "\"name\": \"AAAA\"," +
                "\"properties\": [" +
                "{ \"name\": \"BBB\", \"dataType\": \"String\" }," +
                "{ \"name\": \"BBB2\", \"dataType\": \"Boolean\" }," +
                "{ \"name\": \"BBB3\", \"dataType\": \"Integer\" }," +
                "{ \"name\": \"BBB4\", \"dataType\": \"Real\" }" +
                "]}," +
                "{" +
                "\"name\": \"AAAA1\"" +
                "}]";

            var model = JsonModelReader.Read(jsonContent);

            var expectedModel = new[]
            {
                new Entity
                {
                    Name       = "AAAA",
                    Properties = new[]
                    {
                        new EntityProperty {
                            Name = "BBB", DataType = DataType.String
                        },
                        new EntityProperty {
                            Name = "BBB2", DataType = DataType.Boolean
                        },
                        new EntityProperty {
                            Name = "BBB3", DataType = DataType.Integer
                        },
                        new EntityProperty {
                            Name = "BBB4", DataType = DataType.Real
                        },
                    },
                },
                new Entity {
                    Name = "AAAA1", Properties = new EntityProperty[0]
                }
            };

            model.Should().BeEquivalentTo(expectedModel);
        }
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(PostStatementContent))
            {
                return;
            }

            var model = new PostStatementContent();

            var request = bindingContext.ActionContext.HttpContext.Request;

            //string strContentType = request.ContentType ?? MediaTypes.Application.Json;

            //try
            //{
            //    var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(strContentType);
            //}
            //catch (FormatException ex)
            //{
            //    throw new BadRequestException(ex.InnerException?.Message ?? ex.Message, ex);
            //}

            try
            {
                var jsonModelReader = new JsonModelReader(request.Headers, request.Body);
                model.Statements = await jsonModelReader.ReadAs <StatementCollection>();
            }
            catch (JsonModelReaderException ex)
            {
                throw new BadRequestException(ex.InnerException?.Message ?? ex.Message, ex);
            }

            if (model.Statements == null)
            {
                bindingContext.Result = ModelBindingResult.Failed();
            }
            else
            {
                bindingContext.Result = ModelBindingResult.Success(model);
            }
        }
示例#8
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(StatementCollection))
            {
                return;
            }

            try
            {
                var request         = bindingContext.ActionContext.HttpContext.Request;
                var jsonModelReader = new JsonModelReader(request.Headers, request.Body);

                var statements = await jsonModelReader.ReadAs <StatementCollection>();

                bindingContext.Result = ModelBindingResult.Success(statements);
                return;
            }
            catch (JsonModelReaderException ex)
            {
                bindingContext.ModelState.TryAddModelException <StatementCollection>(x => x, ex);
                bindingContext.Result = ModelBindingResult.Failed();
            }
        }