// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; namespace Microsoft.Extensions.Configuration { /// /// Utility methods and constants for manipulating Configuration paths /// internal static class ConfigurationPath { /// /// The delimiter ":" used to separate individual keys in a path. /// public static readonly string KeyDelimiter = ":"; /// /// Combines path segments into one path. /// /// The path segments to combine. /// The combined path. public static string Combine(params string[] pathSegments) { if (pathSegments == null) { throw new ArgumentNullException(nameof(pathSegments)); } return string.Join(KeyDelimiter, pathSegments); } /// /// Combines path segments into one path. /// /// The path segments to combine. /// The combined path. public static string Combine(IEnumerable pathSegments) { if (pathSegments == null) { throw new ArgumentNullException(nameof(pathSegments)); } return string.Join(KeyDelimiter, pathSegments); } /// /// Extracts the last path segment from the path. /// /// The path. /// The last path segment of the path. public static string GetSectionKey(string path) { if (string.IsNullOrEmpty(path)) { return path; } var lastDelimiterIndex = path.LastIndexOf(KeyDelimiter, StringComparison.OrdinalIgnoreCase); return lastDelimiterIndex == -1 ? path : path.Substring(lastDelimiterIndex + 1); } /// /// Extracts the path corresponding to the parent node for a given path. /// /// The path. /// The original path minus the last individual segment found in it. Null if the original path corresponds to a top level node. public static string GetParentPath(string path) { if (string.IsNullOrEmpty(path)) { return null; } var lastDelimiterIndex = path.LastIndexOf(KeyDelimiter, StringComparison.OrdinalIgnoreCase); return lastDelimiterIndex == -1 ? null : path.Substring(0, lastDelimiterIndex); } } }