Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IList<object> support #154

Merged
merged 9 commits into from
Apr 27, 2021

Conversation

karljj1
Copy link
Collaborator

@karljj1 karljj1 commented Apr 12, 2021

This changes the internal list from an array to an IList so it's more flexible and can work with Lists etc.
We use a lot of pooled Lists internally to reduce garbage and arrays make it hard to do this.
I added a version for IList to all the main Format methods and swapped the order of the arguments to prevent accidentally using the wrong version.

@axunonb
Copy link
Member

axunonb commented Apr 12, 2021

5 failing CI tests seem to be related to CultureInfo?
This PR has breaking changes, so should rather go to branch version/v3.0 please.

@axunonb axunonb marked this pull request as draft April 12, 2021 21:22
@axunonb axunonb changed the base branch from main to version/v3.0 April 12, 2021 21:26
@karljj1
Copy link
Collaborator Author

karljj1 commented Apr 12, 2021

5 failing CI tests seem to be related to CultureInfo?
This PR has breaking changes, so should rather go to branch version/v3.0 please.

Oh that's strange. I'll see if I can figure out what's going wrong, I wasn't able to get the tests to run on my machine but I think it was because I was using a Mac.

/// <returns>Returns the formatted input with items replaced with their string representation.</returns>
public string FormatWithCache(ref FormatCache? cache, IList<object> args, string format)
{
args = args ?? k_Empty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should read:

args ??= k_Empty;

in case args is nullable - otherwise this line is redundant

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes our version of dotnet does not support that.

/// <param name="format">The format string.</param>
public void FormatWithCacheInto(ref FormatCache cache, IOutput output, IList<object> args, string format)
{
args = args ?? k_Empty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should read:

args ??= k_Empty;

in case args is nullable - otherwise this line is redundant

@@ -23,6 +22,8 @@ public class SmartFormatter
{
#region : EventHandlers :

static readonly object[] k_Empty = { null };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better name

_empty

@@ -149,9 +161,22 @@ public string Format(string format, params object[] args)
/// <returns>Returns the formatted input with items replaced with their string representation.</returns>
public string Format(IFormatProvider? provider, string format, params object[] args)
{
var output = new StringOutput(format.Length + args.Length * 8);
return Format(null, args, format);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Format(null, args, format);
return Format(provider, args, format);

This resolves failing unit tests

/// <param name="format">The format string.</param>
public void FormatInto(IOutput output, IList<object> args, string format)
{
args = args ?? k_Empty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args = args ?? k_Empty;
args ??= k_Empty;

argsis not marked nullable. In this case, the line is redundant.

Copy link
Member

@axunonb axunonb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice - only few things to fix

@axunonb axunonb closed this Apr 16, 2021
@axunonb axunonb deleted the branch axuno:version/v3.0 April 16, 2021 22:48
@axunonb
Copy link
Member

axunonb commented Apr 16, 2021

swapped the order of the arguments to prevent accidentally using the wrong version

If this is the only reason I would rather keep the existing order of arguments.

@axunonb axunonb reopened this Apr 16, 2021
@gyssels
Copy link

gyssels commented Apr 16, 2021

is another source more appropriate?

@codecov
Copy link

codecov bot commented Apr 16, 2021

Codecov Report

Merging #154 (a3d1cd5) into version/v3.0 (aa9e428) will decrease coverage by 0%.
The diff coverage is 56%.

❗ Current head a3d1cd5 differs from pull request most recent head 3a2ebd0. Consider uploading reports for the commit 3a2ebd0 to get more accurate results
Impacted file tree graph

@@             Coverage Diff             @@
##           version/v3.0   #154   +/-   ##
===========================================
- Coverage            94%    94%   -0%     
===========================================
  Files                42     42           
  Lines              1749   1769   +20     
===========================================
+ Hits               1651   1667   +16     
- Misses               98    102    +4     
Impacted Files Coverage Δ
src/SmartFormat/SmartFormatter.cs 84% <50%> (-2%) ⬇️
src/SmartFormat/Core/Formatting/FormatDetails.cs 100% <100%> (ø)
src/SmartFormat/Extensions/DefaultSource.cs 100% <100%> (ø)
src/SmartFormat/Extensions/ReflectionSource.cs 100% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aa9e428...3a2ebd0. Read the comment docs.

@axunonb
Copy link
Member

axunonb commented Apr 16, 2021

FormatInto(..., IList<object> args, ...
args are not nullable, removed args ?? Empty

@axunonb
Copy link
Member

axunonb commented Apr 16, 2021

is another source more appropriate?

@gyssels What is the comment referring to?

@axunonb
Copy link
Member

axunonb commented Apr 20, 2021

Looks good and makes sense, thanks.
Just wondering which benefit is connected with changes order of params?

@karljj1
Copy link
Collaborator Author

karljj1 commented Apr 20, 2021

Looks good and makes sense, thanks.
Just wondering which benefit is connected with changes order of params?

There's cases where it may change the behaviour. E.g if you are passing in a list as an argument at index 0, it would now be treated as a list of arguments instead.

Copy link
Member

@axunonb axunonb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Added support for IList<object> when passing arguments
  • Added unit tests

@axunonb axunonb marked this pull request as ready for review April 27, 2021 19:53
@axunonb axunonb merged commit 0a70e94 into axuno:version/v3.0 Apr 27, 2021
axunonb added a commit that referenced this pull request Apr 27, 2021
axunonb added a commit to axunonb/SmartFormat that referenced this pull request Mar 10, 2022
* Added support for IList<object> when passing arguments.

* Changed order of params

* Revert merge changes

* Fixed some code that should not have been changed.

* Fixed failing tests

* Reverted order of params, added unit tests

Co-authored-by: axunonb <[email protected]>
axunonb added a commit to axunonb/SmartFormat that referenced this pull request Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants