public bool HandlesOption(NsqConfig c, string option) { var typ = c.GetType(); foreach (var field in typ.GetProperties()) { var opt = field.Get <OptAttribute>(); if (opt != null && opt.Name == option) { return(true); } } return(false); }
public void Validate(NsqConfig c) { var typ = c.GetType(); foreach (var field in typ.GetProperties()) { MinAttribute min = field.Get <MinAttribute>(); MaxAttribute max = field.Get <MaxAttribute>(); if (min == null && max == null) { continue; } object value = field.GetValue(c, index: null); var opt = field.Get <OptAttribute>(); if (min != null) { var coercedMinVal = (IComparable)opt.Coerce(min.Value, field.PropertyType); if (coercedMinVal.CompareTo(value) == 1) { throw new Exception(string.Format("invalid {0} ! {1} < {2}", opt.Name, value, coercedMinVal)); } } if (max != null) { var coercedMaxVal = (IComparable)opt.Coerce(max.Value, field.PropertyType); if (coercedMaxVal.CompareTo(value) == -1) { throw new Exception(string.Format("invalid {0} ! {1} > {2}", opt.Name, value, coercedMaxVal)); } } } if (c.HeartbeatInterval > c.ReadTimeout) { throw new Exception(string.Format("HeartbeatInterval {0} must be less than ReadTimeout {1}", c.HeartbeatInterval, c.ReadTimeout)); } // TODO: PR go-nsq: this check was removed, seems still valid since the value can be set through code // https://github.com/nsqio/go-nsq/commit/dd8e5fc4ad80922d884ece51f5574af3fa4f14d3#diff-b4bda758a2aef091432646c354b4dc59L376 if (c.BackoffStrategy == null) { throw new Exception(string.Format("BackoffStrategy cannot be null")); } }
public void Set(NsqConfig c, string option, object value) { var typ = c.GetType(); foreach (var field in typ.GetProperties()) { var opt = field.Get <OptAttribute>(); if (opt == null || opt.Name != option) { continue; } var min = field.Get <MinAttribute>(); var max = field.Get <MaxAttribute>(); var coercedVal = opt.Coerce(value, field.PropertyType); if (min != null) { var coercedMinVal = (IComparable)opt.Coerce(min.Value, field.PropertyType); if (coercedMinVal.CompareTo(coercedVal) == 1) { throw new Exception(string.Format("invalid {0} ! {1} < {2}", opt.Name, coercedVal, coercedMinVal)); } } if (max != null) { var coercedMaxVal = (IComparable)opt.Coerce(max.Value, field.PropertyType); if (coercedMaxVal.CompareTo(coercedVal) == -1) { throw new Exception(string.Format("invalid {0} ! {1} > {2}", opt.Name, coercedVal, coercedMaxVal)); } } field.SetValue(c, coercedVal, index: null); return; } throw new Exception(string.Format("unknown option {0}", option)); }
public void SetDefaults(NsqConfig c) { Type typ = c.GetType(); foreach (var field in typ.GetProperties()) { var opt = field.Get <OptAttribute>(); var defaultValue = field.Get <DefaultAttribute>(); if (opt == null || defaultValue == null) { continue; } c.Set(opt.Name, defaultValue.Value); } string hostname = OS.Hostname(); c.ClientID = hostname.Split(new[] { '.' })[0]; c.Hostname = hostname; c.UserAgent = string.Format("{0}/{1}", ClientInfo.ClientName, ClientInfo.Version); }