public override bool Matches(KtaneModuleInfo module, JsonDict json) { var val = GetValue(module); if (val == null) { return(true); } var str = val.ToString(); var dic = json.GetDict(); return((dic.ContainsKey(str) && dic[str].GetBool()) || dic.All(v => !v.Value.GetBool())); }
public override bool Matches(KtaneModuleInfo module, JsonDict json) { var flags = (int)GetValue(module); foreach (var opt in Options) { bool hasFlag = (flags & opt.Value) != 0; if (json.ContainsKey(opt.Name) && ((json[opt.Name].GetString() == "y" && !hasFlag) || (json[opt.Name].GetString() == "n" && hasFlag))) { return(false); } } return(true); }
public override bool Matches(KtaneModuleInfo module, JsonDict json) { var val = GetValue(module); if (val == null) { return(true); } try { return((int)val >= json["min"].GetInt() && (int)val <= json["max"].GetInt()); } catch (InvalidCastException) { return(false); } }
private HttpResponse generateJson(HttpRequest req) { if (req.Method != HttpMethod.Post) { return(HttpResponse.PlainText("Only POST requests allowed.", HttpStatusCode._405_MethodNotAllowed)); } void populateObject(object obj, Type type) { foreach (var f in type.GetFields()) { var attr = f.GetCustomAttribute <EditableFieldAttribute>(); if (attr == null) { continue; } var fType = f.FieldType; if (f.FieldType.TryGetGenericParameters(typeof(Nullable <>), out var fTypes)) { fType = fTypes[0]; } var val = req.Post[f.Name].Value; try { if (f.GetCustomAttribute <EditableNestedAttribute>() != null) { if (val != "on") { f.SetValue(obj, null); } else { var nestedObj = Activator.CreateInstance(f.FieldType); populateObject(nestedObj, f.FieldType); f.SetValue(obj, nestedObj); } continue; } if (fType == typeof(string)) { f.SetValue(obj, string.IsNullOrWhiteSpace(val) ? null : val.Trim()); } else if (fType.IsEnum) { var enumVal = val == null ? null : Enum.Parse(fType, val); if (enumVal != null) { f.SetValue(obj, enumVal); } } else if (fType == typeof(DateTime)) { f.SetValue(obj, DateTime.ParseExact(val, "yyyy-MM-dd", null)); } else if (fType == typeof(string[])) { f.SetValue(obj, val.Split(';').Select(str => str.Trim()).ToArray()); } else if (fType == typeof(int)) { f.SetValue(obj, string.IsNullOrWhiteSpace(val) ? 0 : int.Parse(val)); } else if (fType == typeof(decimal)) { f.SetValue(obj, string.IsNullOrWhiteSpace(val) ? 0m : decimal.Parse(val)); } else if (fType == typeof(bool)) { f.SetValue(obj, val == "on"); } else { throw new InvalidOperationException($"Unrecognized field type: {fType.FullName}"); } } catch (Exception e) { _logger.Warn($"Generate JSON: unrecognized value. Field: {f.Name}, Type: {fType}, Value: “{val ?? "<null>"}”, Exception: {e.Message} ({e.GetType().FullName})"); } } } var m = new KtaneModuleInfo(); populateObject(m, typeof(KtaneModuleInfo)); var json = ClassifyJson.Serialize(m); // Now deserialize and then re-serialize this to force KtaneModuleInfo to perform some sanity things var m2 = ClassifyJson.Deserialize <KtaneModuleInfo>(json); return(HttpResponse.PlainText(ClassifyJson.Serialize(m2).ToStringIndented())); }
public abstract bool Matches(KtaneModuleInfo module, JsonDict json);
public override bool Matches(KtaneModuleInfo module, JsonDict json) { var str = GetValue(module)?.ToString(); return(str == null || (json.ContainsKey(str) && json[str].GetBool()) || json.All(v => !v.Value.GetBool())); }
private HttpResponse generateJson(HttpRequest req) { if (req.Method != HttpMethod.Post) { return(HttpResponse.PlainText("Only POST requests allowed.", HttpStatusCode._405_MethodNotAllowed)); } void populateObject(object obj, Type type) { foreach (var f in type.GetFields()) { var attr = f.GetCustomAttribute <EditableFieldAttribute>(); if (attr == null || attr.ReadableName == null) { continue; } var fType = f.FieldType; if (f.FieldType.TryGetGenericParameters(typeof(Nullable <>), out var fTypes)) { fType = fTypes[0]; } var val = req.Post[f.Name].Value; try { if (f.GetCustomAttribute <EditableNestedAttribute>() != null) { if (val != "on") { f.SetValue(obj, null); } else { var nestedObj = Activator.CreateInstance(f.FieldType); populateObject(nestedObj, f.FieldType); f.SetValue(obj, nestedObj); } continue; } if (fType == typeof(string)) { f.SetValue(obj, string.IsNullOrWhiteSpace(val) ? null : val.Trim()); } else if (fType.IsEnum && fType.GetCustomAttribute <FlagsAttribute>() != null) { var intVal = 0; foreach (var value in Enum.GetValues(fType)) { if (req.Post[$"{f.Name}-{value}"].Value != null) { intVal |= (int)value; } } f.SetValue(obj, intVal); } else if (fType.IsEnum) { var enumVal = val == null ? null : Enum.Parse(fType, val); if (enumVal != null) { f.SetValue(obj, enumVal); } } else if (fType == typeof(DateTime)) { f.SetValue(obj, DateTime.ParseExact(val, "yyyy-MM-dd", null)); } else if (fType == typeof(string[])) { f.SetValue(obj, val.Split(attr.AllowedSeparators).Select(str => str.Trim()).ToArray().Apply(list => list.Length == 0 || (list.Length == 1 && string.IsNullOrWhiteSpace(list[0])) ? null : list)); } else if (fType == typeof(Dictionary <string, string>)) { if (val.Trim() == "") { continue; } else if (!attr.AllowedDictSeparators.Any(sep => val.Contains(sep))) { f.SetValue(obj, new Dictionary <string, string>() { { attr.DefaultKey, string.IsNullOrWhiteSpace(val) ? null : val.Trim() } }); } else { f.SetValue(obj, val.Split(attr.AllowedSeparators).Select(str => str.Split(attr.AllowedDictSeparators)).ToDictionary(x => x[0].Trim(), x => x[1].Trim())); } } else if (fType == typeof(int)) { f.SetValue(obj, string.IsNullOrWhiteSpace(val) ? 0 : int.Parse(val)); } else if (fType == typeof(decimal)) { f.SetValue(obj, string.IsNullOrWhiteSpace(val) ? 0m : decimal.Parse(val)); } else if (fType == typeof(bool)) { f.SetValue(obj, val == "on"); } else if (fType == typeof(TutorialVideoInfo[])) { f.SetValue(obj, ClassifyJson.Deserialize <TutorialVideoInfo[]>(val)); } else { throw new InvalidOperationException($"Unrecognized field type: {fType.FullName}"); } } catch (Exception e) { Log.Warn($"Generate JSON: unrecognized value. Field: {f.Name}, Type: {fType}, Value: “{val ?? "<null>"}”, Exception: {e.Message} ({e.GetType().FullName})"); } } } var m = new KtaneModuleInfo(); populateObject(m, typeof(KtaneModuleInfo)); if (string.IsNullOrWhiteSpace(m.Name)) { return(HttpResponse.PlainText("You did not specify a module name.")); } var json = ClassifyJson.Serialize(m); // Now deserialize and then re-serialize this to force KtaneModuleInfo to perform some sanity things var m2 = ClassifyJson.Deserialize <KtaneModuleInfo>(json); return(HttpResponse.PlainText(ClassifyJson.Serialize(m2).ToStringIndented())); }