-
Notifications
You must be signed in to change notification settings - Fork 257
/
TomlUtil.cs
70 lines (61 loc) · 2.16 KB
/
TomlUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Tomlyn.Model;
namespace DafnyCore.Generic;
public static class TomlUtil {
public static bool TryGetValueFromToml(TextWriter errorWriter, string sourceDir, string tomlPath, Type type, object tomlValue, out object value) {
if (tomlValue == null) {
value = null;
return false;
}
if (type.IsAssignableFrom(typeof(List<string>))) {
return TryGetListValueFromToml<string>(errorWriter, sourceDir, tomlPath, (TomlArray)tomlValue, out value);
}
if (type.IsAssignableFrom(typeof(List<FileInfo>))) {
return TryGetListValueFromToml<FileInfo>(errorWriter, sourceDir, tomlPath, (TomlArray)tomlValue, out value);
}
if (tomlValue is string tomlString) {
if (type == typeof(FileInfo)) {
// Need to make sure relative paths are interpreted relative to the source of the value,
// not the current directory.
var fullPath = sourceDir != null ? Path.GetFullPath(tomlString, sourceDir) : tomlString;
value = new FileInfo(fullPath);
return true;
}
if (typeof(Enum).IsAssignableFrom(type)) {
try {
value = Enum.Parse(type, tomlString);
return true;
} catch (ArgumentException) {
value = null;
return false;
}
}
}
if (!type.IsInstanceOfType(tomlValue)) {
if (type == typeof(string)) {
value = tomlValue.ToString();
return true;
}
errorWriter.WriteLine(
$"Error: property '{tomlPath}' is of type '{tomlValue.GetType()}' but should be of type '{type}'");
value = null;
return false;
}
value = tomlValue;
return true;
}
private static bool TryGetListValueFromToml<T>(TextWriter errorWriter, string sourceDir, string tomlPath, TomlArray tomlValue, out object value) {
var success = true;
value = tomlValue.Select((e, i) => {
if (TryGetValueFromToml(errorWriter, sourceDir, $"{tomlPath}[{i}]", typeof(T), e, out var elementValue)) {
return (T)elementValue;
}
success = false;
return default(T);
}).ToList();
return success;
}
}