/Bundles/Raven.Bundles.UniqueConstraints/Util.cs
C# | 56 lines | 49 code | 6 blank | 1 comment | 9 complexity | 59bce110fac3ee114b84f1a2e02475ff MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
- using System;
- using System.Linq;
- using System.Text;
- using Raven.Imports.Newtonsoft.Json.Linq;
- using Raven.Json.Linq;
-
- namespace Raven.Bundles.UniqueConstraints
- {
- public static class Util
- {
- public static string EscapeUniqueValue(object value, bool caseInsensitive = false)
- {
- var stringToEscape = value.ToString();
- if (caseInsensitive)
- stringToEscape = stringToEscape.ToLowerInvariant();
- var escapeDataString = Uri.EscapeDataString(stringToEscape);
- if (stringToEscape == escapeDataString)
- return stringToEscape;
- // to avoid issues with ids, we encode the entire thing as safe Base64
- return Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToEscape));
- }
-
- public static UniqueConstraint GetConstraint(RavenJToken property)
- {
- switch (property.Type)
- {
- case JTokenType.String: // backward compatability
- return new UniqueConstraint { PropName = property.Value<string>() };
- case JTokenType.Object:
- return new UniqueConstraint { PropName = property.Value<string>("Name"), CaseInsensitive = property.Value<bool>("CaseInsensitive") };
- default:
- throw new ArgumentOutOfRangeException(property.Type.ToString());
- }
- }
-
-
- public static bool TryGetUniqueValues(RavenJToken prop, out string[] uniqueValues)
- {
- if (prop == null || prop.Type == JTokenType.Null)
- {
- uniqueValues = null;
- return false;
- }
-
- var array = prop as RavenJArray;
- uniqueValues = array != null ? array.Select(p => p.Value<string>()).ToArray() : new[] { prop.Value<string>() };
- return true;
- }
- }
-
- public class UniqueConstraint
- {
- public string PropName { get; set; }
- public bool CaseInsensitive { get; set; }
- }
- }