Skip to content

Commit

Permalink
Inlined casing conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolestandifer3 committed Apr 8, 2020
1 parent 630a6d3 commit 9d447b4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 84 deletions.
65 changes: 0 additions & 65 deletions src/JsonApiDotNetCore/Extensions/StringExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using str = JsonApiDotNetCore.Extensions.StringExtensions;

namespace JsonApiDotNetCore.Graph
{
/// <summary>
Expand Down Expand Up @@ -34,7 +32,6 @@ namespace JsonApiDotNetCore.Graph
public sealed class CamelCaseFormatter: BaseResourceNameFormatter
{
/// <inheritdoc/>
public override string ApplyCasingConvention(string properName) => str.Camelize(properName);
public override string ApplyCasingConvention(string properName) => char.ToLowerInvariant(properName[0]) + properName.Substring(1);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using str = JsonApiDotNetCore.Extensions.StringExtensions;
using System.Text;

namespace JsonApiDotNetCore.Graph
{
Expand Down Expand Up @@ -34,6 +34,27 @@ namespace JsonApiDotNetCore.Graph
public sealed class KebabCaseFormatter : BaseResourceNameFormatter
{
/// <inheritdoc/>
public override string ApplyCasingConvention(string properName) => str.Dasherize(properName);
public override string ApplyCasingConvention(string properName)
{
var chars = properName.ToCharArray();
if (chars.Length > 0)
{
var builder = new StringBuilder();
for (var i = 0; i < chars.Length; i++)
{
if (char.IsUpper(chars[i]))
{
var hashedString = i > 0 ? $"-{char.ToLower(chars[i])}" : $"{char.ToLower(chars[i])}";
builder.Append(hashedString);
}
else
{
builder.Append(chars[i]);
}
}
return builder.ToString();
}
return properName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Models;

Expand All @@ -28,7 +27,7 @@ public ResourceObject Build(IIdentifiable entity, IEnumerable<AttrAttribute> att
var resourceContext = _provider.GetResourceContext(entity.GetType());

// populating the top-level "type" and "id" members.
var ro = new ResourceObject { Type = resourceContext.ResourceName, Id = entity.StringId.NullIfEmpty() };
var ro = new ResourceObject { Type = resourceContext.ResourceName, Id = entity.StringId == string.Empty ? null : entity.StringId };

// populating the top-level "attribute" member of a resource object. never include "id" as an attribute
if (attributes != null && (attributes = attributes.Where(attr => attr.PropertyInfo.Name != _identifiablePropertyName)).Any())
Expand Down
16 changes: 7 additions & 9 deletions test/UnitTests/Builders/LinkBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using JsonApiDotNetCore.Internal.Contracts;
using JsonApiDotNetCore.Managers.Contracts;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Models.Links;
using JsonApiDotNetCoreExample.Models;
using Moq;
Expand Down Expand Up @@ -50,7 +49,7 @@ public void BuildResourceLinks_GlobalAndResourceConfiguration_ExpectedResult(Lin
{
// Arrange
var config = GetConfiguration(resourceLinks: global);
var primaryResource = GetResourceContext<Article>(resourceLinks: resource);
var primaryResource = GetArticleResourceContext(resourceLinks: resource);
_provider.Setup(m => m.GetResourceContext("articles")).Returns(primaryResource);
var builder = new LinkBuilder(config, GetRequestManager(), null, _provider.Object, _queryStringAccessor);

Expand Down Expand Up @@ -98,7 +97,7 @@ public void BuildResourceLinks_GlobalAndResourceConfiguration_ExpectedResult(Lin
{
// Arrange
var config = GetConfiguration(relationshipLinks: global);
var primaryResource = GetResourceContext<Article>(relationshipLinks: resource);
var primaryResource = GetArticleResourceContext(relationshipLinks: resource);
_provider.Setup(m => m.GetResourceContext(typeof(Article))).Returns(primaryResource);
var builder = new LinkBuilder(config, GetRequestManager(), null, _provider.Object, _queryStringAccessor);
var attr = new HasOneAttribute(links: relationship) { RightType = typeof(Author), PublicRelationshipName = "author" };
Expand Down Expand Up @@ -154,7 +153,7 @@ public void BuildResourceLinks_GlobalAndResourceConfiguration_ExpectedResult(Lin
{
// Arrange
var config = GetConfiguration(topLevelLinks: global);
var primaryResource = GetResourceContext<Article>(topLevelLinks: resource);
var primaryResource = GetArticleResourceContext(topLevelLinks: resource);
_provider.Setup(m => m.GetResourceContext<Article>()).Returns(primaryResource);

bool useBaseId = expectedSelfLink != _topSelf;
Expand Down Expand Up @@ -220,19 +219,18 @@ private IPageService GetPageManager()
mock.Setup(m => m.TotalPages).Returns(3);
mock.Setup(m => m.PageSize).Returns(10);
return mock.Object;

}

private ResourceContext GetResourceContext<TResource>(Link resourceLinks = Link.NotConfigured,
Link topLevelLinks = Link.NotConfigured,
Link relationshipLinks = Link.NotConfigured) where TResource : class, IIdentifiable
private ResourceContext GetArticleResourceContext(Link resourceLinks = Link.NotConfigured,
Link topLevelLinks = Link.NotConfigured,
Link relationshipLinks = Link.NotConfigured)
{
return new ResourceContext
{
ResourceLinks = resourceLinks,
TopLevelLinks = topLevelLinks,
RelationshipLinks = relationshipLinks,
ResourceName = typeof(TResource).Name.Dasherize() + "s"
ResourceName = "articles"
};
}

Expand Down

0 comments on commit 9d447b4

Please sign in to comment.