/// <summary> /// Generates schema for the provided list of types and store them in the provided dictionary. /// </summary> /// <param name="allListedTypes">The listed types to fetch schema for.</param> /// <param name="crefSchemaMap">The cref to <see cref="InternalSchemaGenerationInfo"/> map.</param> /// <param name="typeFetcher">The type fetcher used to fetch type information using reflection.</param> /// <param name="schemaReferenceRegistry"><see cref="SchemaReferenceRegistry"/>.</param> private void BuildCrefSchemaMap( IList <string> allListedTypes, Dictionary <string, InternalSchemaGenerationInfo> crefSchemaMap, TypeFetcher typeFetcher, SchemaReferenceRegistry schemaReferenceRegistry) { var key = allListedTypes.ToCrefKey(); var schemaInfo = new InternalSchemaGenerationInfo(); try { var type = typeFetcher.LoadTypeFromCrefValues(allListedTypes); var schema = schemaReferenceRegistry.FindOrAddReference(type); schemaInfo.Schema = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); } catch (Exception e) { var error = new GenerationError { ExceptionType = e.GetType().Name, Message = e.Message }; schemaInfo.Error = error; } if (!crefSchemaMap.ContainsKey(key)) { crefSchemaMap.Add(key, schemaInfo); } }
/// <summary> /// Initializes a new instance of <see cref="GenerationError"/> based on the other instance. /// </summary> public GenerationError(GenerationError other) { if (other == null) { return; } Message = other.Message; ExceptionType = other.ExceptionType; }
public ActionResult GenerateMelody(string melody, bool jsEnabled) { if (melody == null || melody == string.Empty) { return(View("GenerationError", GenerationError.Empty)); } //If we can connect to the database, then log user input bool writeLog = ConfigurationManager.AppSettings["writeLog"].AsBool(); if (writeLog) { string csn = ConfigurationManager.AppSettings["logDBConnectionStringName"]; var db = new MelodyDB(csn); Piece mel = new Piece() { Title = null, Text = melody, UserIP = Request.UserHostAddress }; db.Pieces.Add(mel); try { db.SaveChanges(); } catch { } } string dataUrl = null; try { dataUrl = MelodyGeneration.Generate(melody); }catch (ArgumentException ex) { GenerationError error = ex.Data["Error"] == null ? GenerationError.General : (GenerationError)ex.Data["Error"]; return(View("GenerationError", error)); } if (dataUrl == null) { return(View("GenerationError", GenerationError.General)); } if (jsEnabled) { return(View((object)dataUrl)); } else { var t = MelodyGeneration.ReadExamples(Server.MapPath(examplePath)); return(View("NonJSGenerateMelody", new NonJSGenerateMelody() { Melody = melody, DataUrl = dataUrl, Examples = t })); } }
/// <summary> /// Provided a field cref value, fetches the field value using reflection. /// </summary> /// <param name="crefValue">The cref value for the field.</param> /// <param name="crefFieldMap">The cref value to <see cref="FieldValueInfo"/> map.</param> /// <param name="typeFetcher">The type fetcher used to fetch field value from the loaded assemblies.</param> private void BuildCrefFieldValueMap( string crefValue, Dictionary <string, FieldValueInfo> crefFieldMap, TypeFetcher typeFetcher) { if (string.IsNullOrWhiteSpace(crefValue) || crefFieldMap.ContainsKey(crefValue)) { return; } var fieldValueInfo = new FieldValueInfo(); try { var typeName = crefValue.ExtractTypeNameFromFieldCref(); var type = typeFetcher.LoadTypeFromCrefValues(new List <string> { typeName }); var fieldName = crefValue.ExtractFieldNameFromCref(); var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static); var field = fields.FirstOrDefault(f => f.Name == fieldName); if (field == null) { var errorMessage = string.Format( SpecificationGenerationMessages.FieldNotFound, fieldName, typeName); throw new TypeLoadException(errorMessage); } fieldValueInfo.Value = field.GetValue(null).ToString(); } catch (Exception e) { var error = new GenerationError { ExceptionType = e.GetType().Name, Message = e.Message }; fieldValueInfo.Error = error; } crefFieldMap.Add(crefValue, fieldValueInfo); }