Skip to content

Commit 85ea782

Browse files
fix: remove unreachable return 0 from CountJsonNodes and CountYamlNodes
Agent-Logs-Url: https://github.com/demaconsulting/FileAssert/sessions/4a6f09ef-6fd4-4d9c-b068-b24b93bc5890 Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
1 parent 0b00adf commit 85ea782

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

src/DemaConsulting.FileAssert/Modeling/FileAssertJsonAssert.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ private static int CountJsonNodes(JsonElement root, string query)
122122
var segments = query.Split('.');
123123
var current = root;
124124

125-
for (var i = 0; i < segments.Length; i++)
125+
// Traverse all but the last segment
126+
for (var i = 0; i < segments.Length - 1; i++)
126127
{
127128
if (current.ValueKind != JsonValueKind.Object)
128129
{
@@ -135,18 +136,23 @@ private static int CountJsonNodes(JsonElement root, string query)
135136
}
136137

137138
current = next;
139+
}
138140

139-
if (i == segments.Length - 1)
140-
{
141-
// Return array length for arrays, or 1 for any other element
142-
return current.ValueKind == JsonValueKind.Array
143-
? current.GetArrayLength()
144-
: 1;
145-
}
141+
// Evaluate the final segment
142+
if (current.ValueKind != JsonValueKind.Object)
143+
{
144+
return 0;
145+
}
146+
147+
if (!current.TryGetProperty(segments[^1], out var leaf))
148+
{
149+
return 0;
146150
}
147151

148-
// Path traversal completed without reaching the final segment (empty segments list)
149-
return 0;
152+
// Return array length for arrays, or 1 for any other element
153+
return leaf.ValueKind == JsonValueKind.Array
154+
? leaf.GetArrayLength()
155+
: 1;
150156
}
151157

152158
/// <summary>

src/DemaConsulting.FileAssert/Modeling/FileAssertYamlAssert.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ private static int CountYamlNodes(YamlNode root, string query)
129129
var segments = query.Split('.');
130130
YamlNode? current = root;
131131

132-
for (var i = 0; i < segments.Length; i++)
132+
// Traverse all but the last segment
133+
for (var i = 0; i < segments.Length - 1; i++)
133134
{
134135
if (current is not YamlMappingNode mapping)
135136
{
@@ -140,22 +141,27 @@ private static int CountYamlNodes(YamlNode root, string query)
140141
{
141142
return 0;
142143
}
144+
}
143145

144-
if (i == segments.Length - 1)
145-
{
146-
// Return sequence length, or 1 for scalar/mapping nodes
147-
return current switch
148-
{
149-
YamlSequenceNode seq => seq.Children.Count,
150-
YamlScalarNode => 1,
151-
YamlMappingNode => 1,
152-
_ => 0
153-
};
154-
}
146+
// Evaluate the final segment
147+
if (current is not YamlMappingNode finalMapping)
148+
{
149+
return 0;
155150
}
156151

157-
// Path traversal completed without reaching the final segment (empty segments list)
158-
return 0;
152+
if (!finalMapping.Children.TryGetValue(new YamlScalarNode(segments[^1]), out var leaf))
153+
{
154+
return 0;
155+
}
156+
157+
// Return sequence length, or 1 for scalar/mapping nodes
158+
return leaf switch
159+
{
160+
YamlSequenceNode seq => seq.Children.Count,
161+
YamlScalarNode => 1,
162+
YamlMappingNode => 1,
163+
_ => 0
164+
};
159165
}
160166

161167
/// <summary>

0 commit comments

Comments
 (0)