Skip to content

Commit

Permalink
Add Node.HasValue and Node.ClearValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyCushnie committed Apr 12, 2024
1 parent 2f6922e commit 59d076c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
7 changes: 2 additions & 5 deletions SUCC/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ public override string Message

string startOfErrorMessage = $"Invalid file data on line {lineNumber} of {file}. Expected data of type {ExpectedDataType}, but couldn't interpret data as that type";

bool nodeHasChildren = ErroneousNode.ChildNodes.Count > 0;
bool nodeHasValue = !ErroneousNode.Value.IsNullOrEmpty();

if (nodeHasChildren)
if (ErroneousNode.HasChildNodes)
return startOfErrorMessage + $" (node has {ErroneousNode.ChildNodes.Count} children)";

if (nodeHasValue)
if (ErroneousNode.HasValue)
return startOfErrorMessage + $": {ErroneousNode.Value}";

return startOfErrorMessage + ".";
Expand Down
4 changes: 2 additions & 2 deletions SUCC/Parsing Logic/DataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ internal static (List<Line> topLevelLines, Dictionary<string, KeyNode> topLevelN
}
}

if (node.Value == "") // If this is a node with children
if (!node.HasValue) // If this node can have children, but is not the start of a multi-line string
nestingNodeStack.Push(node);

if (node.Value == MultiLineStringNode.Terminator) // if this is the start of a multi line string
if (node.Value == MultiLineStringNode.Terminator) // If this is the start of a multi line string
{
nestingNodeStack.Push(node);
node.ChildNodeType = NodeChildrenType.MultiLineString;
Expand Down
2 changes: 1 addition & 1 deletion SUCC/Parsing Logic/NodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal static object GetNodeData(Node node, Type type)
if (CollectionTypesManager.IsSupportedType(type))
return RetrieveDataWithErrorChecking(() => CollectionTypesManager.RetrieveCollection(node, type));

if (!node.Value.IsNullOrEmpty())
if (node.HasValue)
return RetrieveDataWithErrorChecking(() => ComplexTypeShortcuts.GetFromShortcut(node.Value, type));

return RetrieveDataWithErrorChecking(() => ComplexTypes.RetrieveComplexType(node, type));
Expand Down
12 changes: 8 additions & 4 deletions SUCC/Parsing Logic/Nodes/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private int GetProperChildIndentation()

private void EnsureProperChildType(NodeChildrenType expectedType)
{
if (expectedType != NodeChildrenType.MultiLineString && !this.Value.IsNullOrEmpty())
if (this.HasValue && expectedType != NodeChildrenType.MultiLineString)
throw new Exception($"node has a value ({Value}), which means it can't have children");

if (ChildNodeType != expectedType)
Expand All @@ -127,10 +127,10 @@ private void EnsureProperChildType(NodeChildrenType expectedType)
}


public bool HasValue => !Value.IsNullOrEmpty();
public void ClearValue() => Value = String.Empty;

public bool ContainsChildNode(string key)
=> GetChildKeys().Contains(key);

public bool HasChildNodes => ChildNodes.Count > 0;
public void ClearChildren()
{
_ChildLines.Clear();
Expand All @@ -139,6 +139,10 @@ public void ClearChildren()
ChildNodeType = NodeChildrenType.None;
}


public bool ContainsChildNode(string key)
=> GetChildKeys().Contains(key);

public void AddChild(Line newLine)
{
if (this.ChildNodeType == NodeChildrenType.Key && newLine is KeyNode keyNode && this.ContainsChildNode(keyNode.Key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void SetCollectionNode(Node node, object data, Type collectionType

public static object RetrieveCollection(Node node, Type collectionType)
{
if (!node.Value.IsNullOrEmpty())
if (node.HasValue)
throw new FormatException("Collection nodes cannot have a value");


Expand Down
4 changes: 2 additions & 2 deletions SUCC/Parsing Logic/Types/Complex Types/ComplexTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal static class ComplexTypes
internal static void SetComplexNode(Node node, object item, Type type, FileStyle style)
{
// Clear the shortcut if there is any
if (!node.Value.IsNullOrEmpty())
node.Value = "";
if (node.HasValue)
node.ClearValue();

foreach (var m in type.GetValidMembers())
{
Expand Down

0 comments on commit 59d076c

Please sign in to comment.