Skip to content

Commit

Permalink
Merge pull request #22 from TylerReid/tryGetPropertyTypeErrorMessage
Browse files Browse the repository at this point in the history
Improve error message when PropertyName is not found in properties, but the default conversion of property_name is
  • Loading branch information
xoofx committed Feb 14, 2022
2 parents 6c5a3b7 + afb2ddd commit a278ed4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The mapping comes with a few requirements and is highly customizable:
- Can ignore properties via attributes (`JsonIgnore`, `DataMemberIgnore`). These attributes can be configured in `TomlModelOptions.AttributeListForIgnore`
- By default property names are lowered and split by `_` by PascalCase letters
- For example: `ThisIsAnExample` becomes `this_is_an_example`
- This behavior can be change by passing a `TomlModelOptions` and specifying the `TomlModelOptions.GetPropertyName` delegate.
- This behavior can be changed by passing a `TomlModelOptions` and specifying the `TomlModelOptions.GetPropertyName` delegate.
- If the property contains a special attribute (e.g `JsonPropertyName`, `DataMember`), it will use this name instead. You can control the list of attributes in `TomlModelOptions.AttributeListForGetName`.
- Requires class types to have a parameter-less constructor.
- Supports all C# primitive types, `string`, `DateTime`, `DateTimeOffset` and `TomlDateTime`.
Expand Down
6 changes: 6 additions & 0 deletions src/Tomlyn/Model/Accessors/StandardObjectDynamicAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public override bool TryGetPropertyType(SourceSpan span, string name, out Type?
propertyType = prop.PropertyType;
return true;
}
var snakeCasedName = TomlModelOptions.PascalToSnakeCase(name);
if (_props.ContainsKey(snakeCasedName))
{
Context.Diagnostics.Error(span, $"The property {name} was not found, but {snakeCasedName} was. By default property names are lowered and split by _ by PascalCase letters. This behavior can be changed by passing a TomlModelOptions and specifying the TomlModelOptions.GetPropertyName delegate.");
return false;
}
Context.Diagnostics.Error(span, $"The property {name} was not found on object type {TargetType.FullName}");
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Tomlyn/TomlModelOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ public TomlModelOptions()
}
}

name ??= prop.Name;
return PascalToSnakeCase(name ?? prop.Name);
}

internal static string PascalToSnakeCase(string name)
{
var builder = new StringBuilder();
var pc = (char)0;
foreach (var c in name)
Expand Down

0 comments on commit a278ed4

Please sign in to comment.