Skip to content

Commit 6b40203

Browse files
committed
feat: Added GetKeyReferences and GetKeys.
1 parent 1d4dd86 commit 6b40203

5 files changed

Lines changed: 477 additions & 42 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/////////////////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// NamingFormatter - String format library with key-valued replacer.
4+
// Copyright (c) 2016-2019 Kouji Matsui (@kekyo2)
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
/////////////////////////////////////////////////////////////////////////////////////////////////
19+
20+
using System;
21+
22+
namespace NamingFormatter
23+
{
24+
/// <summary>
25+
/// Represents a named placeholder reference contained in a format string.
26+
/// </summary>
27+
/// <remarks>
28+
/// The reference preserves the original key path, such as <c>commit.Author.Name</c>,
29+
/// and exposes the root key for callers that need to resolve only the first segment.
30+
/// </remarks>
31+
public readonly struct FormatKeyReference
32+
{
33+
private readonly string keyPath;
34+
private readonly string rootKey;
35+
36+
/// <summary>
37+
/// Initializes a new instance.
38+
/// </summary>
39+
/// <param name="keyPath">The parsed key path.</param>
40+
/// <param name="placeholderStartIndex">The placeholder start index in the original format string.</param>
41+
/// <param name="placeholderLength">The placeholder length in the original format string.</param>
42+
public FormatKeyReference(
43+
string keyPath,
44+
int placeholderStartIndex,
45+
int placeholderLength)
46+
{
47+
if (keyPath == null)
48+
{
49+
throw new ArgumentNullException(nameof(keyPath));
50+
}
51+
if (placeholderStartIndex < 0)
52+
{
53+
throw new ArgumentOutOfRangeException(nameof(placeholderStartIndex));
54+
}
55+
if (placeholderLength < 0)
56+
{
57+
throw new ArgumentOutOfRangeException(nameof(placeholderLength));
58+
}
59+
60+
this.keyPath = keyPath;
61+
this.rootKey =
62+
(keyPath.IndexOf('.') is var dotIndex) && (dotIndex >= 0) ?
63+
keyPath.Substring(0, dotIndex) :
64+
keyPath;
65+
this.PlaceholderStartIndex = placeholderStartIndex;
66+
this.PlaceholderLength = placeholderLength;
67+
}
68+
69+
/// <summary>
70+
/// Gets the full key path.
71+
/// </summary>
72+
public string KeyPath =>
73+
this.keyPath;
74+
75+
/// <summary>
76+
/// Gets the first segment of the key path.
77+
/// </summary>
78+
public string RootKey =>
79+
this.rootKey;
80+
81+
/// <summary>
82+
/// Gets the placeholder start index in the original format string.
83+
/// </summary>
84+
public int PlaceholderStartIndex
85+
{
86+
get;
87+
}
88+
89+
/// <summary>
90+
/// Gets the placeholder length in the original format string.
91+
/// </summary>
92+
public int PlaceholderLength
93+
{
94+
get;
95+
}
96+
97+
/// <summary>
98+
/// Returns the key path.
99+
/// </summary>
100+
/// <returns>The key path.</returns>
101+
public override string ToString() =>
102+
this.KeyPath;
103+
}
104+
}

0 commit comments

Comments
 (0)