Skip to content

Commit f1a9dee

Browse files
Merge pull request #34 from Stravaig-Projects/#33-render-pattern
#33 Make the short code render in to a pattern
2 parents 3698907 + 964aa75 commit f1a9dee

File tree

25 files changed

+343
-50
lines changed

25 files changed

+343
-50
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ ShortCode.GenerateSequentialShortCode();
5252

5353
// To generate sequential short code of the given length
5454
ShortCode.GenerateSequentialShortCode(int length);
55+
56+
// To generate a patterned short code
57+
// by default in the format XXX-XXX-XXX
58+
ShortCode.GeneratePatternedShortCode();
5559
```
5660

5761
There are some configuration methods you can call during your app's startup:
@@ -63,6 +67,15 @@ There are some configuration methods you can call during your app's startup:
6367
- `GuidCodeGenerator`: The code is a hashed form of a GUID
6468
- `RandomCodeGenerator`: The code is generated from the `Random` class.
6569
- `CrytographicallyRandomCodeGenerator`: The code is generated using a cryptographic strength random number generator.
70+
* `ShortCode.SetPattern(IEnumerable<PatternPart> parts)`: Sets the pattern using the given parts. (See below)
71+
72+
## Pattern Parts
73+
74+
If you are using patterned short codes then you can define the pattern with a collection of `PatternPart`. There are two types of pattern part, a `Fixed` part, which consists of a fixed string; and, a `EncodeIntoCharacterSpace` type, which is part of the encoded short code.
75+
76+
To create the encoding part: `new PatternPart(string characterSpace, int length)`. The character space is a string containing the valid characters that a code can contain. It must be made up of unique characters. The `length` is the length of the encoded sequence.
77+
78+
To create the fixed part: `new PatternPart(string fixedChars)`. Where `fixedChars` is the fixed string that is used to separate parts of the code.
6679

6780
## Setting up Short Codes with Microsoft's Dependency Injection
6881

@@ -143,9 +156,37 @@ In the appsettings.json file:
143156
}
144157
```
145158

159+
If you prefer to use a patterned short code generator the appsettings.json will look something like this:
160+
161+
```json
162+
{
163+
"Stravaig": {
164+
"ShortCode": {
165+
"Pattern":[{
166+
"Type": "EncodeIntoCharacterSpace",
167+
"CharacterSpace": "0123456789QWERTYPLKJHGFDSAZXCVBNM",
168+
"Length": 4
169+
},{
170+
"Type": "Fixed",
171+
"FixedString": "-"
172+
},{
173+
"Type": "EncodeIntoCharacterSpace",
174+
"CharacterSpace": "0123456789QWERTYPLKJHGFDSAZXCVBNM",
175+
"Length": 4
176+
}],
177+
"Generator": {
178+
"Seed": 12345
179+
}
180+
}
181+
}
182+
}
183+
```
184+
185+
Note, that when the configuration contains a pattern it overrides any general specification.
186+
146187
## Using the IShortCodeFactory
147188

148189
To get a short code, the `IShortCodeFactory` interface exposes two methods for to create short codes.
149190

150191
* `GetNextCode()` will simply get the next code.
151-
* `GetCodes(int)` will return the number of codes specified as an `IEnumerable<string>`
192+
* `GetCodes(int number)` will return the `number` of codes specified as an `IEnumerable<string>`

release-notes/wip-release-notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ Date: ???
88

99
### Features
1010

11+
- #33: Have patterned codes.
12+
1113
### Miscellaneous
1214

1315
### Dependabot
1416

17+
- Bump Microsoft.Extensions.DependencyInjection.Abstractions from 3.1.11 to 3.1.12
18+
- Bump Microsoft.NET.Test.Sdk from 16.8.3 to 16.9.1- Bump NUnit from 3.13.0 to 3.13.1
19+
- Bump Stravaig.Extensions.Logging.Diagnostics from 0.3.1 to 0.3.2
1520

src/.idea/.idea.Stravaig.ShortCode/.idea/riderModule.iml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Example/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ static void Main()
1414

1515
ServiceCollection services = new ServiceCollection();
1616
services.AddShortCodeGenerator<SequentialCodeGenerator>(config);
17+
// Alternative - Build the options in code.
18+
// services.AddShortCodeGenerator<SequentialCodeGenerator>(options =>
19+
// {
20+
// options.Pattern = new[]
21+
// {
22+
// new PatternOptions(NamedCharacterSpaces.UpperLatinLetters, 3),
23+
// new PatternOptions("-"),
24+
// new PatternOptions(NamedCharacterSpaces.Digits, 3),
25+
// };
26+
// });
1727

1828
ServiceProvider provider = services.BuildServiceProvider();
1929
var factory = provider.GetRequiredService<IShortCodeFactory>();

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": {

src/Stravaig.ShortCode.Benchmarks/Variants/Encoder1.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ public string Convert(ulong fullCode, int? fixedChars = null, int maxChars = int
2929
return result.ToString();
3030
}
3131

32-
public ulong RangeRequired(int numChars)
33-
{
34-
return (ulong)Math.Pow(_characterSpace.Length, numChars);
35-
}
36-
3732
public int MaxLength()
3833
{
3934
return (int)(Math.Log(ulong.MaxValue) / Math.Log(_characterSpace.Length));

src/Stravaig.ShortCode.Benchmarks/Variants/Encoder2.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public string Convert(ulong fullCode, int? fixedChars = null, int maxChars = 64)
2828
return new String(resultChars, 0, index);
2929
}
3030

31-
public ulong RangeRequired(int numChars)
32-
{
33-
return (ulong)Math.Pow(_characterSpace.Length, numChars);
34-
}
35-
3631
public int MaxLength()
3732
{
3833
return (int)(Math.Log(ulong.MaxValue) / Math.Log(_characterSpace.Length));
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 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using Microsoft.Extensions.Configuration;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Options;
@@ -39,7 +40,10 @@ private static void AddCommonParts<TGenerator>(this IServiceCollection services)
3940
services.AddSingleton<IEncoder>(p =>
4041
{
4142
var options = p.GetRequiredService<IOptions<ShortCodeOptions>>();
42-
return new Encoder(options.Value.CharacterSpace);
43+
if (options.Value.Pattern.Length == 0)
44+
return new Encoder(options.Value.CharacterSpace);
45+
return new PatternedEncoder(options.Value.Pattern
46+
.Select(pat => pat.ToPatternPart()));
4347
});
4448
services.AddSingleton<IShortCodeGenerator, TGenerator>();
4549
services.AddSingleton<IShortCodeFactory, ShortCodeFactory>();

src/Stravaig.ShortCode.Tests/EncoderTests.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@ public void Ctor_WithSingleCharacter()
2525
ex.Message.ShouldStartWith("Value cannot have a single character.");
2626
}
2727

28-
[TestCase(1, NamedCharacterSpaces.Digits, ExpectedResult = (ulong)10)]
29-
[TestCase(2, NamedCharacterSpaces.Digits, ExpectedResult = (ulong)100)]
30-
[TestCase(1, NamedCharacterSpaces.LowerLatinLetters, ExpectedResult = (ulong)26)]
31-
[TestCase(2, NamedCharacterSpaces.LowerLatinLetters, ExpectedResult = (ulong)676)]
32-
[TestCase(3, NamedCharacterSpaces.LatinLetters, ExpectedResult = (ulong)140608)]
33-
[TestCase(4, NamedCharacterSpaces.LettersAndDigits, ExpectedResult = (ulong)14776336)]
34-
[TestCase(5, NamedCharacterSpaces.LettersAndDigits, ExpectedResult = (ulong)916132832)]
35-
public ulong RangeRequiredReturnsGoodValueForCharacterSpace(int numChars, string charSpace)
36-
{
37-
var characterSpace = new Encoder(charSpace);
38-
return characterSpace.RangeRequired(numChars);
39-
}
40-
4128
[TestCase(NamedCharacterSpaces.LettersAndDigits, ExpectedResult = 10)]
4229
[TestCase(NamedCharacterSpaces.Digits, ExpectedResult = 19)]
4330
public int MaxLengthTests(string charSpace)

0 commit comments

Comments
 (0)