Skip to content

Commit b846091

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Add custom string formatting support to ApiVersion
1 parent 0ad0821 commit b846091

File tree

9 files changed

+1095
-188
lines changed

9 files changed

+1095
-188
lines changed

src/Common/ApiVersion.cs

Lines changed: 11 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Microsoft.AspNetCore.Mvc
99
using System.Diagnostics.Contracts;
1010
using System.Globalization;
1111
using System.Text;
12+
using Versioning;
1213
using static System.DateTime;
1314
using static System.Globalization.CultureInfo;
1415
using static System.String;
@@ -304,94 +305,18 @@ public static bool TryParse( string text, out ApiVersion version )
304305
return true;
305306
}
306307

307-
void AppendGroupVersion( StringBuilder text, IFormatProvider formatProvider )
308-
{
309-
Contract.Requires( text != null );
310-
311-
if ( GroupVersion != null )
312-
{
313-
text.Append( GroupVersion.Value.ToString( GroupVersionFormat, formatProvider ) );
314-
}
315-
}
316-
317-
void AppendMajorAndMinorVersion( StringBuilder text, IFormatProvider formatProvider )
318-
{
319-
Contract.Requires( text != null );
320-
321-
if ( MajorVersion != null )
322-
{
323-
if ( text.Length > 0 )
324-
{
325-
text.Append( '.' );
326-
}
327-
328-
text.Append( MajorVersion.Value.ToString( formatProvider ) );
329-
330-
if ( MinorVersion == null )
331-
{
332-
return;
333-
}
334-
335-
text.Append( '.' );
336-
text.Append( MinorVersion.Value.ToString( formatProvider ) );
337-
}
338-
else if ( MinorVersion != null )
339-
{
340-
text.Append( "0." );
341-
text.Append( MinorVersion.Value.ToString( formatProvider ) );
342-
}
343-
}
344-
345-
void AppendStatus( StringBuilder text )
346-
{
347-
Contract.Requires( text != null );
348-
349-
if ( text.Length > 0 && !IsNullOrEmpty( Status ) )
350-
{
351-
text.Append( '-' );
352-
text.Append( Status );
353-
}
354-
}
355-
356308
/// <summary>
357309
/// Returns the text representation of the version using the specified format and format provider.
358-
/// </summary>
359-
/// <param name="format">The format to return the text representation in.</param>
360-
/// <returns>The <see cref="String">string</see> representation of the version.</returns>
361-
/// <remarks>The supported format codes are:
362-
/// <para>
363-
/// <list type="table">
364-
/// <listheader>
365-
/// <term>Format</term>
366-
/// <description>Description</description>
367-
/// </listheader>
368-
/// <item>
369-
/// <term>G, g</term>
370-
/// <description>Returns only the <see cref="P:GroupVersion">group version</see>, if present.</description>
371-
/// </item>
372-
/// <item>
373-
/// <term>V, v</term>
374-
/// <description>Returns only the <see cref="P:MajorVersion">major</see> and <see cref="P:MinorVersion">minor</see> versions, if present.</description>
375-
/// </item>
376-
/// <item>
377-
/// <term>S, s</term>
378-
/// <description>Returns full API version with the <see cref="P:Status">status</see>, if <see cref="P:Status">status</see> is present.</description>
379-
/// </item>
380-
/// <item>
381-
/// <term>F, f</term>
382-
/// <description>Returns the full API version.</description>
383-
/// </item>
384-
/// </list>
385-
/// </para>
386-
/// </remarks>
387-
/// <exception cref="ArgumentNullException">The specified <paramref name="format"/> is <c>null</c> or any empty string.</exception>
310+
/// <seealso cref="ApiVersionFormatProvider"/></summary>
311+
/// <param name="format">The format to return the text representation in. The value can be <c>null</c> or empty.</param>
312+
/// <returns>The <see cref="string">string</see> representation of the version.</returns>
388313
/// <exception cref="FormatException">The specified <paramref name="format"/> is not one of the supported format values.</exception>
389314
public virtual string ToString( string format ) => ToString( format, InvariantCulture );
390315

391316
/// <summary>
392317
/// Returns the text representation of the version.
393318
/// </summary>
394-
/// <returns>The <see cref="String">string</see> representation of the version.</returns>
319+
/// <returns>The <see cref="string">string</see> representation of the version.</returns>
395320
public override string ToString() => ToString( null, InvariantCulture );
396321

397322
/// <summary>
@@ -548,71 +473,16 @@ public virtual int CompareTo( ApiVersion other )
548473

549474
/// <summary>
550475
/// Returns the text representation of the version using the specified format and format provider.
551-
/// </summary>
552-
/// <param name="format">The format to return the text representation in.</param>
476+
/// <seealso cref="ApiVersionFormatProvider"/></summary>
477+
/// <param name="format">The format to return the text representation in. The value can be <c>null</c> or empty.</param>
553478
/// <param name="formatProvider">The <see cref="IFormatProvider">format provider</see> used to generate text.
554-
/// This implementation should typically use an <see cref="P:CultureInfo.InvariantCulture">invariant culture</see>.</param>
555-
/// <returns>The <see cref="String">string</see> representation of the version.</returns>
556-
/// <remarks>The supported format codes are:
557-
/// <para>
558-
/// <list type="table">
559-
/// <listheader>
560-
/// <term>Format</term>
561-
/// <description>Description</description>
562-
/// </listheader>
563-
/// <item>
564-
/// <term>G, g</term>
565-
/// <description>Returns only the <see cref="P:GroupVersion">group version</see>, if present.</description>
566-
/// </item>
567-
/// <item>
568-
/// <term>V, v</term>
569-
/// <description>Returns only the <see cref="P:MajorVersion">major</see> and <see cref="P:MinorVersion">minor</see> versions, if present.</description>
570-
/// </item>
571-
/// <item>
572-
/// <term>S, s</term>
573-
/// <description>Returns full API version with the <see cref="P:Status">status</see>, if <see cref="P:Status">status</see> is present.</description>
574-
/// </item>
575-
/// <item>
576-
/// <term>F, f</term>
577-
/// <description>Returns the full API version.</description>
578-
/// </item>
579-
/// </list>
580-
/// </para>
581-
/// </remarks>
582-
/// <exception cref="ArgumentNullException">The specified <paramref name="format"/> is <c>null</c> or any empty string.</exception>
479+
/// This implementation should typically use an <see cref="InvariantCulture">invariant culture</see>.</param>
480+
/// <returns>The <see cref="string">string</see> representation of the version.</returns>
583481
/// <exception cref="FormatException">The specified <paramref name="format"/> is not one of the supported format values.</exception>
584482
public virtual string ToString( string format, IFormatProvider formatProvider )
585483
{
586-
// syntax := <group version>[.<major>.<minor>][-status] | [<group version>.]<major>.<minor>[-status]
587-
var text = new StringBuilder();
588-
589-
switch ( format )
590-
{
591-
case "G":
592-
case "g":
593-
AppendGroupVersion( text, formatProvider );
594-
break;
595-
case "V":
596-
case "v":
597-
AppendMajorAndMinorVersion( text, formatProvider );
598-
break;
599-
case "S":
600-
case "s":
601-
AppendGroupVersion( text, formatProvider );
602-
AppendMajorAndMinorVersion( text, formatProvider );
603-
break;
604-
case null:
605-
case "F":
606-
case "f":
607-
AppendGroupVersion( text, formatProvider );
608-
AppendMajorAndMinorVersion( text, formatProvider );
609-
AppendStatus( text );
610-
break;
611-
default:
612-
throw new FormatException( SR.ApiVersionInvalidFormatCode.FormatDefault( format ) );
613-
}
614-
615-
return text.ToString();
484+
var provider = ApiVersionFormatProvider.GetInstance( formatProvider );
485+
return provider.Format( format, this, formatProvider );
616486
}
617487
}
618488
}

src/Common/Common.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Compile Include="$(MSBuildThisFileDirectory)ReportApiVersionsAttribute.cs" />
2020
<Compile Include="$(MSBuildThisFileDirectory)TypeExtensions.cs" />
2121
<Compile Include="$(MSBuildThisFileDirectory)Versioning\AmbiguousApiVersionException.cs" />
22+
<Compile Include="$(MSBuildThisFileDirectory)Versioning\ApiVersionFormatProvider.cs" />
2223
<Compile Include="$(MSBuildThisFileDirectory)Versioning\ApiVersioningOptions.cs" />
2324
<Compile Include="$(MSBuildThisFileDirectory)Versioning\ApiVersionModel.cs" />
2425
<Compile Include="$(MSBuildThisFileDirectory)Versioning\ApiVersionModelDebugView.cs" />

0 commit comments

Comments
 (0)