Skip to content

Commit 79f4ec0

Browse files
Format code
1 parent fcc7b2a commit 79f4ec0

File tree

16 files changed

+229
-201
lines changed

16 files changed

+229
-201
lines changed

C Sharp/Object Validator/Runtime/Blank.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ class Blank(bool reversed, string? fieldExpression) : Condition(reversed, fieldE
33
{
44
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
55
{
6-
if (value == null)
7-
throw new Exception("Null values are not supported for 'blank'.");
8-
if (value is string s)
9-
return string.IsNullOrWhiteSpace(s);
10-
throw new Exception("Unsupported type for 'blank': " + value.GetType());
6+
return value switch {
7+
null => throw new Exception("Null values are not supported for 'blank'."),
8+
string s => string.IsNullOrWhiteSpace(s),
9+
_ => throw new Exception("Unsupported type for 'blank': " + value.GetType())
10+
};
1111
}
1212
}

C Sharp/Object Validator/Runtime/Bytes.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class Bytes(bool reversed, string? fieldExpression, string range) : Condition(re
77

88
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (value == null)
11-
throw new Exception("Null values are not supported for 'bytes'.");
12-
if (value is string s)
13-
return Utils.InRange(Encoding.UTF8.GetByteCount(s), range);
14-
throw new Exception("Unsupported type for 'bytes': " + value.GetType());
10+
return value switch {
11+
null => throw new Exception("Null values are not supported for 'bytes'."),
12+
string s => Utils.InRange(Encoding.UTF8.GetByteCount(s), range),
13+
_ => throw new Exception("Unsupported type for 'bytes': " + value.GetType())
14+
};
1515
}
1616
}

C Sharp/Object Validator/Runtime/Condition.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ private static void HandleKey(IDictionary map, string key, List<object?> values)
2121

2222
private static void HandleValues(List<object?> values, string name, List<object?> newValues) {
2323
foreach (object? value in values)
24-
if (value == null)
25-
newValues.Add(null);
26-
else if (value is IDictionary d)
27-
HandleKey(d, name, newValues);
28-
else if (value is IList l && int.TryParse(name, out int index))
29-
HandleIndex(l, index, newValues);
30-
else
31-
HandleField(value, name, newValues);
24+
switch (value) {
25+
case null:
26+
newValues.Add(null);
27+
break;
28+
case IDictionary d:
29+
HandleKey(d, name, newValues);
30+
break;
31+
case IList l when int.TryParse(name, out int index):
32+
HandleIndex(l, index, newValues);
33+
break;
34+
default:
35+
HandleField(value, name, newValues);
36+
break;
37+
}
3238
}
3339

3440
internal bool Check(object? root, string? rootFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields) {
@@ -41,13 +47,17 @@ internal bool Check(object? root, string? rootFieldExpression, HashSet<string> p
4147
fullName += name;
4248
if (fullName == "*")
4349
foreach (object? o in values)
44-
if (o == null)
45-
newValues.Add(null);
46-
else if (o is IEnumerable e)
47-
foreach (object item in e)
48-
newValues.Add(item);
49-
else
50-
throw new Exception("Unsupported type for iteration: " + o.GetType());
50+
switch (o) {
51+
case null:
52+
newValues.Add(null);
53+
break;
54+
case IEnumerable e:
55+
foreach (object item in e)
56+
newValues.Add(item);
57+
break;
58+
default:
59+
throw new Exception("Unsupported type for iteration: " + o.GetType());
60+
}
5161
else if (name.Length >= 3 && name[^3..^1] == "//")
5262
fullName = fullName[0..^3] + fullName[^2..];
5363
else if (name.Length >= 2 && name[^2] == '/') {

C Sharp/Object Validator/Runtime/Contains.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ class Contains(bool reversed, string? fieldExpression, string? arg) : Condition(
77

88
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (value == null)
11-
throw new Exception("Null values are not supported for 'contains'.");
12-
if (value is string s)
13-
return s.Contains(arg!);
14-
if (value is IEnumerable e) {
15-
foreach (object o in e)
16-
if (o == null) {
17-
if (arg == null)
10+
switch (value) {
11+
case null:
12+
throw new Exception("Null values are not supported for 'contains'.");
13+
case string s:
14+
return s.Contains(arg!);
15+
case IEnumerable e:
16+
foreach (object o in e)
17+
if (o == null) {
18+
if (arg == null)
19+
return true;
20+
} else if (o.ToString() == arg)
1821
return true;
19-
} else if (o.ToString() == arg)
20-
return true;
21-
return false;
22+
return false;
23+
default:
24+
throw new Exception("Unsupported type for 'contains': " + value.GetType());
2225
}
23-
throw new Exception("Unsupported type for 'contains': " + value.GetType());
2426
}
2527
}

C Sharp/Object Validator/Runtime/Length.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ class Length(bool reversed, string? fieldExpression, string range) : Condition(r
77

88
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (value == null)
11-
throw new Exception("Null values are not supported for 'length'.");
12-
if (value is string s)
13-
return Utils.InRange(s.Length, range);
14-
if (value is ICollection c)
15-
return Utils.InRange(c.Count, range);
16-
if (value is IReadOnlyCollection<object> roc)
17-
return Utils.InRange(roc.Count, range);
18-
throw new Exception("Unsupported type for 'length': " + value.GetType());
10+
return value switch {
11+
null => throw new Exception("Null values are not supported for 'length'."),
12+
string s => Utils.InRange(s.Length, range),
13+
ICollection c => Utils.InRange(c.Count, range),
14+
IReadOnlyCollection<object> roc => Utils.InRange(roc.Count, range),
15+
_ => throw new Exception("Unsupported type for 'length': " + value.GetType())
16+
};
1917
}
2018
}

C Sharp/Object Validator/Runtime/Range.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ class Range(bool reversed, string? fieldExpression, string range) : Condition(re
77

88
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (value == null)
11-
throw new Exception("Null values are not supported for 'range'.");
12-
if (!(value is sbyte || value is byte || value is short || value is ushort || value is int || value is uint || value is long || value is ulong || value is BigInteger ||
13-
value is float || value is double || value is decimal || value is DateTime))
14-
throw new Exception("Unsupported type for 'range': " + value.GetType());
15-
return Utils.InRange(value, range);
10+
return value switch
11+
{
12+
null => throw new Exception("Null values are not supported for 'range'."),
13+
sbyte or byte or short or ushort or int or uint or long or ulong or BigInteger or float or double or decimal or DateTime => Utils.InRange(value, range),
14+
_ => throw new Exception("Unsupported type for 'range': " + value.GetType()),
15+
};
1616
}
1717
}

C Sharp/Object Validator/Runtime/RegEx.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class RegEx(bool reversed, string? fieldExpression, string pattern) : Condition(
77

88
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (value == null)
11-
throw new Exception("Null values are not supported for 'regex'.");
12-
if (value is string s)
13-
return Regex.IsMatch(s, pattern);
14-
throw new Exception("Unsupported type for 'regex': " + value.GetType());
10+
return value switch {
11+
null => throw new Exception("Null values are not supported for 'regex'."),
12+
string s => Regex.IsMatch(s, pattern),
13+
_ => throw new Exception("Unsupported type for 'regex': " + value.GetType())
14+
};
1515
}
1616
}

C Sharp/Object Validator/Runtime/Utils.cs

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,62 @@ internal static bool InRange(object value, string range) {
4141
targetString = limit;
4242
reversed = true;
4343
}
44-
if (value is sbyte sb) {
45-
if (InLimit(sb, operator_, sbyte.Parse(targetString), reversed))
46-
return false;
47-
} else if (value is byte b) {
48-
if (InLimit(b, operator_, byte.Parse(targetString), reversed))
49-
return false;
50-
} else if (value is short s) {
51-
if (InLimit(s, operator_, short.Parse(targetString), reversed))
52-
return false;
53-
} else if (value is ushort us) {
54-
if (InLimit(us, operator_, ushort.Parse(targetString), reversed))
55-
return false;
56-
} else if (value is int i) {
57-
if (InLimit(i, operator_, int.Parse(targetString), reversed))
58-
return false;
59-
} else if (value is uint ui) {
60-
if (InLimit(ui, operator_, uint.Parse(targetString), reversed))
61-
return false;
62-
} else if (value is long l) {
63-
if (InLimit(l, operator_, long.Parse(targetString), reversed))
64-
return false;
65-
} else if (value is ulong ul) {
66-
if (InLimit(ul, operator_, ulong.Parse(targetString), reversed))
67-
return false;
68-
} else if (value is BigInteger bi) {
69-
if (InLimit(bi, operator_, BigInteger.Parse(targetString), reversed))
70-
return false;
71-
} else if (value is float f) {
72-
if (InLimit(f, operator_, float.Parse(targetString), reversed))
73-
return false;
74-
} else if (value is double d) {
75-
if (InLimit(d, operator_, double.Parse(targetString), reversed))
76-
return false;
77-
} else if (value is decimal dc) {
78-
if (InLimit(dc, operator_, decimal.Parse(targetString), reversed))
79-
return false;
80-
} else if (value is DateTime dt) {
81-
if (InLimit(dt, operator_, DateTime.Parse(targetString, null, System.Globalization.DateTimeStyles.RoundtripKind), reversed))
82-
return false;
83-
} else
84-
throw new Exception("Unsupported type for 'InRange()': " + value.GetType());
44+
switch (value) {
45+
case sbyte sb:
46+
if (InLimit(sb, operator_, sbyte.Parse(targetString), reversed))
47+
return false;
48+
break;
49+
case byte b:
50+
if (InLimit(b, operator_, byte.Parse(targetString), reversed))
51+
return false;
52+
break;
53+
case short s:
54+
if (InLimit(s, operator_, short.Parse(targetString), reversed))
55+
return false;
56+
break;
57+
case ushort us:
58+
if (InLimit(us, operator_, ushort.Parse(targetString), reversed))
59+
return false;
60+
break;
61+
case int i:
62+
if (InLimit(i, operator_, int.Parse(targetString), reversed))
63+
return false;
64+
break;
65+
case uint ui:
66+
if (InLimit(ui, operator_, uint.Parse(targetString), reversed))
67+
return false;
68+
break;
69+
case long l:
70+
if (InLimit(l, operator_, long.Parse(targetString), reversed))
71+
return false;
72+
break;
73+
case ulong ul:
74+
if (InLimit(ul, operator_, ulong.Parse(targetString), reversed))
75+
return false;
76+
break;
77+
case BigInteger bi:
78+
if (InLimit(bi, operator_, BigInteger.Parse(targetString), reversed))
79+
return false;
80+
break;
81+
case float f:
82+
if (InLimit(f, operator_, float.Parse(targetString), reversed))
83+
return false;
84+
break;
85+
case double d:
86+
if (InLimit(d, operator_, double.Parse(targetString), reversed))
87+
return false;
88+
break;
89+
case decimal de:
90+
if (InLimit(de, operator_, decimal.Parse(targetString), reversed))
91+
return false;
92+
break;
93+
case DateTime dt:
94+
if (InLimit(dt, operator_, DateTime.Parse(targetString, null, System.Globalization.DateTimeStyles.RoundtripKind), reversed))
95+
return false;
96+
break;
97+
default:
98+
throw new Exception("Unsupported type for 'inRange()': " + value.GetType());
99+
}
85100
}
86101
return true;
87102
}

C Sharp/Object Validator/TypeConverter.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ public bool Accepts(Type type)
2323
state = 1;
2424
break;
2525
case 1:
26-
if (parser.Current is SequenceEnd) {
27-
parser.MoveNext();
28-
if (parser.Current is not DocumentEnd)
29-
throw new YamlException("Expected DocumentEnd");
30-
return rules.ToArray();
31-
}
32-
if (parser.Current is MappingStart) {
33-
state = 2;
34-
break;
26+
switch (parser.Current) {
27+
case SequenceEnd:
28+
parser.MoveNext();
29+
if (parser.Current is not DocumentEnd)
30+
throw new YamlException("Expected DocumentEnd");
31+
return rules.ToArray();
32+
case MappingStart:
33+
state = 2;
34+
break;
35+
default:
36+
throw new YamlException("Expected MappingStart or SequenceEnd");
3537
}
36-
throw new YamlException("Expected MappingStart or SequenceEnd");
38+
break;
3739
case 2:
3840
Condition? condition = null;
3941
int? id = null;

Java/Object Validator/lib/src/main/java/com/quicksilver/objectvalidator/runtime/Blank.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public Blank(boolean reversed, String fieldExpression) {
99

1010
@Override
1111
protected boolean isFulfilledBy(Object value, String fullFieldExpression, HashSet<String> passedFields, HashSet<String> failedFields) {
12-
if (value == null)
13-
throw new RuntimeException("Null values are not supported for 'blank'.");
14-
if (value instanceof String s)
15-
return s.isBlank();
16-
throw new RuntimeException("Unsupported type for 'blank': " + value.getClass());
12+
return switch (value) {
13+
case null -> throw new RuntimeException("Null values are not supported for 'blank'.");
14+
case String s -> s.isBlank();
15+
default -> throw new RuntimeException("Unsupported type for 'blank': " + value.getClass());
16+
};
1717
}
1818
}

Java/Object Validator/lib/src/main/java/com/quicksilver/objectvalidator/runtime/Bytes.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ else if (Character.isHighSurrogate(c)) {
2929

3030
@Override
3131
protected boolean isFulfilledBy(Object value, String fullFieldExpression, HashSet<String> passedFields, HashSet<String> failedFields) {
32-
if (value == null)
33-
throw new RuntimeException("Null values are not supported for 'bytes'.");
34-
if (value instanceof CharSequence s)
35-
return Utils.inRange(utf8ByteLength(s), range);
36-
throw new RuntimeException("Unsupported type for 'bytes': " + value.getClass());
32+
return switch (value) {
33+
case null -> throw new RuntimeException("Null values are not supported for 'bytes'.");
34+
case CharSequence s -> Utils.inRange(utf8ByteLength(s), range);
35+
default -> throw new RuntimeException("Unsupported type for 'bytes': " + value.getClass());
36+
};
3737
}
3838
}

0 commit comments

Comments
 (0)