Repository navigation
Expand file tree
/
Copy pathModelObject.cs
More file actions
executable file
·100 lines (82 loc) · 3.12 KB
/
Copy pathModelObject.cs
File metadata and controls
executable file
·100 lines (82 loc) · 3.12 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ResourceExtractor;
public class ModelObject(int id) : IDictionary<string, object> {
private readonly IDictionary<string, object> map = new Dictionary<string, object>();
[FieldInfo("id")]
public int Id { get; set; } = id;
[FieldInfo("en")]
public string English { get; set; }
[FieldInfo("ja")]
public string Japanese { get; set; }
[FieldInfo("enl")]
public string EnglishLog { get; set; }
[FieldInfo("jal")]
public string JapaneseLog { get; set; }
public object this[string key] {
get {
var field = FindField(key);
return field is null ? map[key] : field.Value.Property.GetValue(this);
}
set {
var field = FindField(key);
if (field is not null) {
field.Value.Property.SetValue(this, value);
} else {
map[key] = value;
}
}
}
public void Add(string key, object value) =>
this[key] = value;
public void Remove(string key) =>
map.Remove(key);
public void Merge(ModelObject other) {
foreach (var field in Fields) {
var value = field.Property.GetValue(other);
if (value is not null) {
field.Property.SetValue(this, value);
}
}
foreach (var pair in other) {
map[pair.Key] = pair.Value;
}
}
public bool ContainsKey(string key) =>
map.ContainsKey(key);
public bool TryGetValue(string key, out object result) =>
map.TryGetValue(key, out result);
public static readonly Field[] Fields = [.. typeof(ModelObject).GetProperties()
.Select(property => new Field(property, property.GetCustomAttribute<FieldInfoAttribute>()))
.Where(field => field.Info is not null)
.Select(field => field.Info.Name is not null ? field : field with { Info = new(MakeFieldName(field.Property.Name))})];
private static Field? FindField(string key) {
foreach (var field in Fields) {
if (field.Info.Name == key) {
return field;
}
}
return null;
}
public readonly record struct Field(PropertyInfo Property, FieldInfoAttribute Info);
private static string MakeFieldName(string input) =>
new([.. input.SelectMany(c => Char.IsUpper(c) ? new[] { '_', Char.ToLowerInvariant(c) } : [c]).Skip(1)]);
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() =>
map.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
map.GetEnumerator();
public ICollection<string> Keys => map.Keys;
public int Count => map.Count;
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) =>
map.CopyTo(array, arrayIndex);
public ICollection<object> Values => throw new NotSupportedException();
public bool IsReadOnly => throw new NotSupportedException();
public void Add(KeyValuePair<string, object> item) => throw new NotSupportedException();
bool IDictionary<string, object>.Remove(string key) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(KeyValuePair<string, object> item) => throw new NotSupportedException();
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item) => throw new NotSupportedException();
}