Skip to content

Commit

Permalink
megaease#7 Using StringIsNullOrWhiltespace method
Browse files Browse the repository at this point in the history
  • Loading branch information
gaufung committed Sep 14, 2023
1 parent d14763e commit eea7d58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Src/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void RegisterFromYaml(String yamlFile)
}
TraceManager.Trace128Bits = spec.Id128bit;
IZipkinSender sender = default(IZipkinSender);
if (spec.OutputServerUrl == null || spec.OutputServerUrl.Trim().Equals(""))
if (string.IsNullOrWhiteSpace(spec.OutputServerUrl))
{
LoggerManager.GetTracingLogger().LogInformation("out put server was empty, use console reporter for trace.");
sender = new ConsoleSender();
Expand Down
27 changes: 11 additions & 16 deletions Src/Spec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,34 @@ public static Spec Load(string yamlFile)
switch (kvp.Key)
{
case "serviceName":
spec.ServiceName = (string)(isEmpty(kvp.Value) ? "" : kvp.Value);
spec.ServiceName = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value;
break;
case "tracing.type":
spec.TracingType = (string)(isEmpty(kvp.Value) ? "" : kvp.Value);
spec.TracingType = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value;
break;
case "tracing.enable":
spec.TracingEnable = isEmpty(kvp.Value) ? true : bool.Parse(kvp.Value);
spec.TracingEnable = string.IsNullOrWhiteSpace(kvp.Value) ? true : bool.Parse(kvp.Value);
break;
case "tracing.sample.rate":
spec.SampleRate = isEmpty(kvp.Value) ? 1.0F : float.Parse(kvp.Value);
spec.SampleRate = string.IsNullOrWhiteSpace(kvp.Value) ? 1.0F : float.Parse(kvp.Value);
break;
case "tracing.shared.spans":
spec.SharedSpans = isEmpty(kvp.Value) ? true : bool.Parse(kvp.Value);
spec.SharedSpans = string.IsNullOrWhiteSpace(kvp.Value) ? true : bool.Parse(kvp.Value);
break;
case "tracing.id128bit":
spec.Id128bit = isEmpty(kvp.Value) ? false : bool.Parse(kvp.Value);
spec.Id128bit = string.IsNullOrWhiteSpace(kvp.Value) ? false : bool.Parse(kvp.Value);
break;
case "reporter.output.server":
spec.OutputServerUrl = isEmpty(kvp.Value) ? "" : kvp.Value;
spec.OutputServerUrl = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value;
break;
case "reporter.output.server.tls.enable":
spec.EnableTls = isEmpty(kvp.Value) ? false : bool.Parse(kvp.Value);
spec.EnableTls = string.IsNullOrWhiteSpace(kvp.Value) ? false : bool.Parse(kvp.Value);
break;
case "reporter.output.server.tls.key":
spec.TlsKey = isEmpty(kvp.Value) ? "" : kvp.Value;
spec.TlsKey = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value;
break;
case "reporter.output.server.tls.cert":
spec.TlsCert = isEmpty(kvp.Value) ? "" : kvp.Value;
spec.TlsCert = string.IsNullOrWhiteSpace(kvp.Value) ? string.Empty : kvp.Value;
break;
default:
break;
Expand All @@ -119,14 +119,9 @@ public static Spec Load(string yamlFile)
return spec;
}

public static bool isEmpty(string v)
{
return v == null || v.Length == 0 || v.Trim().Length == 0;
}

public void Validate()
{
if (EnableTls && (isEmpty(TlsKey) || isEmpty(TlsCert)))
if (EnableTls && (string.IsNullOrWhiteSpace(TlsKey) || string.IsNullOrWhiteSpace(TlsCert)))
{
throw new FormatException("key, cert are not all specified");
}
Expand Down

0 comments on commit eea7d58

Please sign in to comment.