Skip to content

Commit ef7ee5e

Browse files
Add new Encoder to the Dependency Injection extension method
1 parent b0db9d9 commit ef7ee5e

File tree

6 files changed

+73
-10
lines changed

6 files changed

+73
-10
lines changed

src/Example/appsettings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
},
99
"Stravaig": {
1010
"ShortCode": {
11+
"Pattern":[{
12+
"Type": "EncodeIntoCharacterSpace",
13+
"CharacterSpace": "0123456789QWERTYUIOPLKJHGFDSAZXCVBNM",
14+
"Length": 4
15+
},{
16+
"Type": "Fixed",
17+
"FixedString": "-"
18+
},{
19+
"Type": "EncodeIntoCharacterSpace",
20+
"CharacterSpace": "0123456789QWERTYUIOPLKJHGFDSAZXCVBNM",
21+
"Length": 4
22+
}],
1123
"CharacterSpace": "0123456789QWERTYUIOPLKJHGFDSAZXCVBNM",
1224
"FixedLength": 7,
1325
"Generator": {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Stravaig.ShortCode.DependencyInjection
2+
{
3+
internal static class PatternOptionsExtensions
4+
{
5+
public static PatternPart ToPatternPart(this ShortCodeOptions.PatternOptions options)
6+
{
7+
if (options.Type == PatternType.Fixed)
8+
return new PatternPart(options.FixedString);
9+
return new PatternPart(options.CharacterSpace, options.Length);
10+
}
11+
}
12+
}

src/Stravaig.ShortCode.DependencyInjection/ServiceProviderExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using Microsoft.Extensions.Configuration;
35
using Microsoft.Extensions.DependencyInjection;
46
using Microsoft.Extensions.Options;
@@ -39,7 +41,10 @@ private static void AddCommonParts<TGenerator>(this IServiceCollection services)
3941
services.AddSingleton<IEncoder>(p =>
4042
{
4143
var options = p.GetRequiredService<IOptions<ShortCodeOptions>>();
42-
return new Encoder(options.Value.CharacterSpace);
44+
if (options.Value.Pattern.Length == 0)
45+
return new Encoder(options.Value.CharacterSpace);
46+
return new PatternedEncoder(options.Value.Pattern
47+
.Select(pat => pat.ToPatternPart()));
4348
});
4449
services.AddSingleton<IShortCodeGenerator, TGenerator>();
4550
services.AddSingleton<IShortCodeFactory, ShortCodeFactory>();

src/Stravaig.ShortCode/PatternedEncoder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public ulong RangeRequired(int numChars)
6161
public int MaxLength()
6262
{
6363
throw new NotImplementedException();
64+
int result = 0;
65+
ulong remainingRange = ulong.MaxValue;
66+
foreach (var part in _parts)
67+
{
68+
result += (int)(Math.Log(remainingRange) / Math.Log(part.Length));
69+
}
70+
71+
return result;
6472
}
6573

6674
public string NamedCharacterSpace =>

src/Stravaig.ShortCode/ShortCodeFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ShortCodeFactory(IShortCodeGenerator generator, IEncoder encoder, IOption
4040

4141
private void WarnOnInconsistentOptions()
4242
{
43-
if (_options.FixedLength.HasValue && _options.FixedLength.Value > _encoder.MaxLength())
43+
if (_options.Pattern.Length == 0 && _options.FixedLength.HasValue && _options.FixedLength.Value > _encoder.MaxLength())
4444
_logger.LogWarning("The Short Code generator will always produce codes with padding because the Fixed Length ({fixedLength}) option is greater than maximum length the encoder can generate ({encoderMaxLength}).",
4545
_options.FixedLength.Value,
4646
_encoder.MaxLength());

src/Stravaig.ShortCode/ShortCodeOptions.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ namespace Stravaig.ShortCode
55
{
66
public class ShortCodeOptions
77
{
8+
public class PatternOptions
9+
{
10+
private string _characterSpace;
11+
public PatternType Type { get; set; }
12+
13+
public string CharacterSpace
14+
{
15+
get => _characterSpace;
16+
set
17+
{
18+
ValidateCharacterSpace(value);
19+
_characterSpace = value;
20+
}
21+
}
22+
23+
public int Length { get; set; }
24+
25+
public string FixedString { get; set; }
26+
}
827
private const int AbsoluteMinLength = 1;
928
private const int AbsoluteMaxLength = 64;
1029
private const int CharacterSpaceMinLength = 2;
@@ -18,18 +37,23 @@ public string CharacterSpace
1837
get => _characterSpace;
1938
set
2039
{
21-
if (string.IsNullOrWhiteSpace(value))
22-
throw new ArgumentException(
23-
"The value cannot be, null, Empty or contain whitespace.",
24-
nameof(CharacterSpace));
25-
if (value.Length < CharacterSpaceMinLength)
26-
throw new ArgumentException(
27-
$"The value must contain at least {CharacterSpaceMinLength} characters.",
28-
nameof(CharacterSpace));
40+
ValidateCharacterSpace(value);
2941
_characterSpace = value;
3042
}
3143
}
3244

45+
private static void ValidateCharacterSpace(string value)
46+
{
47+
if (string.IsNullOrWhiteSpace(value))
48+
throw new ArgumentException(
49+
"The value cannot be, null, Empty or contain whitespace.",
50+
nameof(CharacterSpace));
51+
if (value.Length < CharacterSpaceMinLength)
52+
throw new ArgumentException(
53+
$"The value must contain at least {CharacterSpaceMinLength} characters.",
54+
nameof(CharacterSpace));
55+
}
56+
3357
public int MaxLength
3458
{
3559
get => _maxLength;
@@ -57,5 +81,7 @@ public int? FixedLength
5781
}
5882

5983
public Dictionary<string, object> Generator { get; set; } = new Dictionary<string, object>();
84+
85+
public PatternOptions[] Pattern { get; set; } = Array.Empty<PatternOptions>();
6086
}
6187
}

0 commit comments

Comments
 (0)