Skip to content

Commit 0cfecab

Browse files
committed
Bring back naming convention attribute
1 parent 3358086 commit 0cfecab

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

Element.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Element : AttributeList, IEquatable<Element>
2727
/// <param name="name">An arbitrary string. Does not have to be unique, and can be null.</param>
2828
/// <param name="class_name">An arbitrary string which loosely defines the type of Element this is. Cannot be null.</param>
2929
/// <exception cref="IndexOutOfRangeException">Thrown when the owner already contains the maximum number of Elements allowed in a Datamodel.</exception>
30-
public Element(Datamodel owner, string name, Guid? id = null, string classNameOverride = "")
30+
public Element(Datamodel owner, string name, Guid? id = null, string? classNameOverride = null)
3131
: base(owner)
3232
{
3333
ArgumentNullException.ThrowIfNull(owner);
@@ -174,18 +174,13 @@ internal set
174174
foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
175175
{
176176
// Check if the property is an auto-property and is declared by a subclass of Element
177-
var declaringType = property.DeclaringType;
177+
var declaringType = property.DeclaringType!;
178178

179-
if(declaringType is null)
180-
{
181-
throw new InvalidOperationException("Declaring type is null");
182-
}
183-
184-
if (property.GetIndexParameters().Length == 0 && declaringType.IsSubclassOf(typeof(Element)))
179+
if (declaringType.IsSubclassOf(typeof(Element)))
185180
{
186181
var name = property.Name;
182+
name = declaringType.GetCustomAttribute<Format.AttributeNamingConventionAttribute>()?.GetAttributeName(name, property.PropertyType) ?? name;
187183
name = property.GetCustomAttribute<Format.DMProperty>()?.Name ?? name;
188-
189184
properties.Add((name, property));
190185
}
191186
}

Format/Attribute.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,61 @@
33

44
namespace Datamodel.Format;
55

6+
/// <summary>
7+
/// Subclass this attribute to define a custom attribute name convention.
8+
/// </summary>
9+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
10+
public abstract class AttributeNamingConventionAttribute : System.Attribute
11+
{
12+
public abstract string GetAttributeName(string propertyName, Type propertyType);
13+
}
14+
15+
/// <summary>
16+
/// This class' property names are mostly lowercase.
17+
/// </summary>
18+
public class LowercasePropertiesAttribute : AttributeNamingConventionAttribute
19+
{
20+
public override string GetAttributeName(string propertyName, Type _)
21+
=> propertyName.ToLower();
22+
}
23+
24+
/// <summary>
25+
/// This class' property names are mostly camelCase.
26+
/// </summary>
27+
public class CamelCasePropertiesAttribute : AttributeNamingConventionAttribute
28+
{
29+
public override string GetAttributeName(string propertyName, Type _)
30+
=> char.ToLowerInvariant(propertyName.AsSpan()[0]) + propertyName[1..];
31+
}
32+
33+
/// <summary>
34+
/// This class' property names are mostly m_hungarian.
35+
/// </summary>
36+
public class HungarianPropertiesAttribute : CamelCasePropertiesAttribute
37+
{
38+
public override string GetAttributeName(string propertyName, Type propertyType)
39+
{
40+
var typeAnnotation = propertyType switch
41+
{
42+
_ when propertyType == typeof(int) => "n",
43+
_ when propertyType == typeof(float) => "fl",
44+
_ when propertyType == typeof(bool) => "b",
45+
_ when propertyType == typeof(Vector2) => "v",
46+
_ when propertyType == typeof(Vector3) => "v",
47+
_ when propertyType == typeof(Vector4) => "v",
48+
_ when propertyType == typeof(Matrix4x4) => "mat",
49+
_ => string.Empty,
50+
};
51+
52+
if (typeAnnotation == string.Empty)
53+
{
54+
return "m_" + base.GetAttributeName(propertyName, propertyType);
55+
}
56+
57+
return "m_" + typeAnnotation + propertyName;
58+
}
59+
}
60+
661
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
762
public sealed class DMProperty : System.Attribute
863
{

0 commit comments

Comments
 (0)