Skip to content

Commit

Permalink
addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vlada-shubina committed Nov 9, 2022
1 parent 1a7cc02 commit 3d24c4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,13 @@ protected static bool ConvertJTokenToBool(string token, IGeneratedSymbolConfig c
try
{
var jToken = JToken.Parse(token);
if (jToken.Type is not JTokenType.Boolean and not JTokenType.String)
{
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeBoolean, config.VariableName, parameterName), config.VariableName);
}
if (bool.TryParse(jToken.ToString(), out bool result))
if (jToken.TryParseBool(out bool result))
{
return result;
}
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeBoolean, config.VariableName, parameterName), config.VariableName);
}
catch (TemplateAuthoringException)
{
throw;
}
catch (Exception ex)
catch (Exception ex) when (ex is not TemplateAuthoringException)
{
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
}
Expand All @@ -115,21 +107,13 @@ protected static int ConvertJTokenToInt(string token, IGeneratedSymbolConfig con
try
{
var jToken = JToken.Parse(token);
if (jToken.Type is not JTokenType.Integer and not JTokenType.String)
{
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeInteger, config.VariableName, parameterName), config.VariableName);
}
if (int.TryParse(jToken.ToString(), out int result))
if (jToken.TryParseInt(out int result))
{
return result;
}
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_ValueShouldBeInteger, config.VariableName, parameterName), config.VariableName);
}
catch (TemplateAuthoringException)
{
throw;
}
catch (Exception ex)
catch (Exception ex) when (ex is not TemplateAuthoringException)
{
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
}
Expand All @@ -146,11 +130,7 @@ protected static string ConvertJTokenToString(string token, IGeneratedSymbolConf
}
return jToken.ToString();
}
catch (TemplateAuthoringException)
{
throw;
}
catch (Exception ex)
catch (Exception ex) when (ex is not TemplateAuthoringException)
{
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
}
Expand All @@ -167,11 +147,7 @@ protected static JArray ConvertJTokenToJArray(string token, IGeneratedSymbolConf
}
return (JArray)jToken;
}
catch (TemplateAuthoringException)
{
throw;
}
catch (Exception ex)
catch (Exception ex) when (ex is not TemplateAuthoringException)
{
throw new TemplateAuthoringException(string.Format(LocalizableStrings.MacroConfig_Exception_InvalidJSON, config.VariableName, parameterName), config.VariableName, ex);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Shared/JExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ internal static bool ToBool(this JToken? token, string? key = null, bool default
return result;
}

internal static bool TryParseInt(this JToken token, out int result)
{
result = default;
return (token.Type == JTokenType.Integer || token.Type == JTokenType.String)
&&
int.TryParse(token.ToString(), out result);
}

internal static int ToInt32(this JToken? token, string? key = null, int defaultValue = 0)
{
int value;
Expand Down

0 comments on commit 3d24c4a

Please sign in to comment.