internal HiveId(string value, bool alreadyFormatted) : this(alreadyFormatted && value != null ? new HiveIdValue(value) : HiveIdValue.Empty) { if (alreadyFormatted) { return; } var possibleValue = TryParse(value); if (possibleValue.Success) { this._value = possibleValue.Result._value; this._providerGroupRoot = possibleValue.Result._providerGroupRoot; this._providerId = possibleValue.Result._providerId; } else { _value = new HiveIdValue(value); } }
/// <summary> /// Mandates that the specified parameter is not empty /// </summary> /// <param name="value"></param> /// <param name="paramName"></param> public static void ParameterNotEmpty(HiveIdValue value, string paramName) { That(value != HiveIdValue.Empty, x => new ArgumentNullException(paramName)); }
public HiveId(Uri providerGroupRoot, string providerId, HiveIdValue value) : this(value) { ProviderGroupRoot = providerGroupRoot; ProviderId = providerId; }
public HiveId(string providerGroupSchemeOnly, string providerId, HiveIdValue value) : this(new Uri(providerGroupSchemeOnly + "://"), providerId, value) { }
public HiveId(HiveIdValue value) { _value = value; _providerGroupRoot = null; _providerId = null; }
public static AttemptTuple <HiveId> TryParse(string formattedValue) { if (string.IsNullOrEmpty(formattedValue)) { return(new AttemptTuple <HiveId>(false, HiveId.Empty)); } var style = DetectFormatStyleFromString(formattedValue); //I have removed the exception.. shouldn't a 'Try' method not throw exceptions? SD. 16/11/2011 if (!style.Success) { return(AttemptTuple <HiveId> .False); } //Mandate.That(style.Success, x => new FormatException("Could not determine format of '{0}' in parameter formattedValue. Things to check: does the input string look like a valid Uri, but it contains invalid path characters?".InvariantFormat(formattedValue))); string splitter = " "; switch (style.Result) { case HiveIdFormatStyle.AsUri: splitter = UriDelimiter; break; case HiveIdFormatStyle.UriSafe: splitter = UriSafeDelimiter; break; case HiveIdFormatStyle.AutoSingleValue: if (formattedValue == HiveIdValue.Empty.ToString() || formattedValue == HiveId.Empty.ToString()) { return(new AttemptTuple <HiveId>(true, HiveId.Empty)); } // Try to parse first as a Guid, then an int, then assume it's a string // Then just return from here as we don't need to parse anything else Guid guidValue; if (Guid.TryParse(formattedValue, out guidValue)) { return(new AttemptTuple <HiveId>(true, new HiveId(guidValue))); } Int32 intValue; if (Int32.TryParse(formattedValue, out intValue)) { return(new AttemptTuple <HiveId>(true, new HiveId(intValue))); } return(new AttemptTuple <HiveId>(true, new HiveId(formattedValue, true))); break; default: return(AttemptTuple <HiveId> .False); } try { // Check for just a root / if (style.Result == HiveIdFormatStyle.AsUri && formattedValue == "/") { return(new AttemptTuple <HiveId>(true, new HiveId((Uri)null, null, new HiveIdValue("/")))); } // Check for value-less incoming Uris // If the string does not contain any of the HiveIdValueType enums, and it's parseable as a Uri, then // assume that it is a root value if (style.Result == HiveIdFormatStyle.AsUri && Uri.IsWellFormedUriString(formattedValue, UriKind.Absolute)) { var containsValue = false; foreach (var name in Enum.GetNames(typeof(HiveIdValueTypes))) { if (formattedValue.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) > -1) { containsValue = true; break; } } if (!containsValue) { return(new AttemptTuple <HiveId>(true, new HiveId(new Uri(formattedValue), string.Empty, new HiveIdValue("/")))); } } // Let's say the input is this: // storage://stylesheets/p__provider-name/v__type/value // We need to end up with: // storage://stylesheets/ // provider-name // type // value var splitValue = new List <string>(formattedValue.Split(new[] { splitter }, StringSplitOptions.None)); var stack = new Stack <string>(splitValue); var valueDetect = splitValue.Where(x => x.StartsWith(ValuePrefix)).FirstOrDefault() ?? string.Empty; var providerDetect = splitValue.Where(x => x.StartsWith(ProviderPrefix)).FirstOrDefault() ?? string.Empty; string extractedType = string.Empty; string extractedValue = string.Empty; string extractedProviderId = string.Empty; string extractedRemainder = string.Empty; if (string.IsNullOrEmpty(valueDetect) && string.IsNullOrEmpty(providerDetect) && splitValue.Count > 1) { // Must be a short-form value with no root or provider id, so re-split clearing out blanks var reSplit = formattedValue.Split(new[] { splitter }, StringSplitOptions.RemoveEmptyEntries); extractedType = reSplit[0]; extractedValue = FormatFromUriSafePart(reSplit[1]); } else { var valueStartIndex = splitValue.IndexOf(valueDetect); var wholeValueArray = splitValue.Skip(valueStartIndex + 1).ToArray(); var wholeValue = string.Join(splitter.ToString(), wholeValueArray.Select(x => FormatFromUriSafePart((x)))); extractedValue = wholeValue; extractedType = valueDetect.TrimStart(ValuePrefix); if (!string.IsNullOrEmpty(providerDetect)) { var providerStartIndex = splitValue.IndexOf(providerDetect); extractedProviderId = providerDetect.TrimStart(ProviderPrefix); var extractedRemainderArray = splitValue.Take(providerStartIndex); extractedRemainder = string.Join(splitter.ToString(), extractedRemainderArray); } else { var extractedRemainderArray = splitValue.Take(valueStartIndex); extractedRemainder = string.Join(splitter.ToString(), extractedRemainderArray); } } switch (style.Result) { case HiveIdFormatStyle.UriSafe: extractedRemainder = FormatFromUriSafePart(extractedRemainder); break; } Uri providerGroupRoot = null; extractedRemainder = extractedRemainder.EndsWith(":/") ? extractedRemainder + "/" : extractedRemainder; if (!string.IsNullOrEmpty(extractedRemainder)) { providerGroupRoot = new Uri(extractedRemainder); } HiveIdValueTypes parsedType; var tryParseType = Enum.TryParse <HiveIdValueTypes>(extractedType, true, out parsedType); if (!tryParseType) { return(AttemptTuple <HiveId> .False); } var tryCreateValue = HiveIdValue.TryCreate(extractedValue, parsedType); // Ensure provider id is null if it wasn't detected extractedProviderId = string.IsNullOrEmpty(extractedProviderId) ? null : extractedProviderId; return(new AttemptTuple <HiveId>(true, new HiveId(providerGroupRoot, extractedProviderId, tryCreateValue.Result))); } catch (UriFormatException ex) { //I have removed the exception.. shouldn't a 'Try' method not throw exceptions? SD. 16/11/2011 //this one in particular was causing problems with the RoutingEngine trying to parse a 'user' URL //that was not part of Rebel. return(AttemptTuple <HiveId> .False); //throw new FormatException("Could not parse '{0}' as a HiveId; assumed it was {1} but did not parse correctly" // .InvariantFormat(formattedValue, style.Result.ToString()), ex); } }
public static HiveId ConvertIntToGuid(Uri providerGroupRoot, string providerId, int value) { return(new HiveId(providerGroupRoot, providerId, HiveIdValue.ConvertIntToGuid(value))); }
public static HiveId ConvertIntToGuid(string providerGroupSchemeOnly, string providerId, int value) { return(new HiveId(providerGroupSchemeOnly, providerId, HiveIdValue.ConvertIntToGuid(value))); }
public static HiveId ConvertIntToGuid(int value) { return(new HiveId(HiveIdValue.ConvertIntToGuid(value))); }