public CommonReturnValue AddRule(RuleType ruleType, string txt) { BaseRule rule = null; switch (ruleType) // TODO: Check for duplicated rules? { case RuleType.Schema: rule = new SchemaRule(txt); break; case RuleType.Specific: { rule = SpecificRule.FromFullname(txt); } break; case RuleType.Regex: { try { var regexTest = new Regex(txt); } catch (Exception ex) { return(CommonReturnValue.UserError("Invalid regex pattern: " + ex.ToString())); } rule = new RegexRule(txt); break; } default: throw new Exception($"Unsupported rule type: ${ruleType}"); } rule.Id = ShortId.Generate(useNumbers: true, useSpecial: true, length: 6); this.Rules.Add(rule); return(CommonReturnValue.Success()); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var target = serializer.Deserialize <Newtonsoft.Json.Linq.JObject>(reader); var type = (RuleType)(long)((JValue)(target["Type"])).Value; if (type == RuleType.Schema) { var sr = new SchemaRule(); if (target["Name"] != null) { sr.Name = target["Name"].ToString(); } if (target["Id"] != null) { sr.Id = target["Id"].ToString(); } return(sr); } else if (type == RuleType.Specific) { var sr = new SpecificRule(); if (target["Routine"] != null) { sr.Routine = target["Routine"].ToString(); } if (target["Schema"] != null) { sr.Schema = target["Schema"].ToString(); } if (target["Name"] != null) { sr.Name = target["Name"].ToString(); } if (target["Id"] != null) { sr.Id = target["Id"].ToString(); } return(sr); } else if (type == RuleType.Regex) { var rr = new RegexRule(); if (target["Name"] != null) { rr.Name = target["Name"].ToString(); } if (target["Id"] != null) { rr.Id = target["Id"].ToString(); } if (target["Match"] != null) { rr.Match = target["Match"].ToString(); } return(rr); } throw new NotSupportedException(string.Format("Type {0} unexpected.", type)); }