private void BuildDictionaryValue(StringBuilder builder, VarSpec varSpec, IDictionary <string, string> values) { var first = true; CheckCompositeVariables(varSpec); if (op.Named && !varSpec.Exploded) { builder.Append(varSpec.Name); builder.Append('='); } foreach (var item in values) { if (!first) { builder.Append(varSpec.Exploded ? op.Separator : ","); } var key = PctEncoding.Escape(item.Key, CharSpec.ExtendedSafe); builder.Append(key); builder.Append(varSpec.Exploded ? '=' : ','); BuildValue(builder, varSpec, item.Value ?? string.Empty); first = false; } }
private void CheckCompositeVariables(VarSpec varSpec) { if (varSpec.MaxLength != 0) { throw new UriTemplateException("MaxLength modifier are not applicable to composite values"); } }
private void BuildValue(StringBuilder builder, VarSpec varSpec, string value) { var charSpec = op.AllowReserved ? CharSpec.ExtendedSafe : CharSpec.Safe; value = PctEncoding.Escape(value, charSpec); builder.Append(value); }
private bool Resolve(StringBuilder builder, VarSpec varSpec, object value, bool hasPrefix) { string stringValue = null; IDictionary <string, string> dictionaryValue = null; IEnumerable <string> collectionValue = null; stringValue = value as string; if (stringValue == null) { collectionValue = value as IEnumerable <string>; if (collectionValue == null) { dictionaryValue = value as IDictionary <string, string>; if (dictionaryValue == null) { throw new UriTemplateException(string.Format("Invalid value type of variable \"{0}\". Expected: string or IEnumerable<string> or IDictionary<string, string>.", value.GetType())); } else if (dictionaryValue.Count == 0) { return(false); } } else if (collectionValue.Count() == 0) { return(false); } } if (hasPrefix) { builder.Append(op.Separator); } else { builder.Append(op.Prefix); } if (stringValue != null) { BuildStringValue(builder, varSpec, stringValue); } else if (collectionValue != null) { BuildCollectionValue(builder, varSpec, collectionValue); } else { BuildDictionaryValue(builder, varSpec, dictionaryValue); } return(true); }
private void CreateVarSpec() { if (varSpecMaxLength == -1) { ThrowException("Invalid URI template modifier"); } var varSpec = new VarSpec(builder.ToString(), varSpecExloded, varSpecMaxLength); if (varSpecs == null) { varSpecs = new List<VarSpec>(); } varSpecs.Add(varSpec); builder.Length = 0; varSpecExloded = false; varSpecMaxLength = 0; }
private void CreateVarSpec() { if (varSpecMaxLength == -1) { ThrowException("Invalid URI template modifier."); } var name = builder.ToString(); var varSpec = new VarSpec(name, varSpecExloded, varSpecMaxLength, true); if (varSpecs == null) { varSpecs = new List <VarSpec>(); } varSpecs.Add(varSpec); builder.Length = 0; varSpecExloded = false; varSpecMaxLength = 0; }
private void BuildCollectionValue(StringBuilder builder, VarSpec varSpec, IEnumerable <string> values) { var first = true; CheckCompositeVariables(varSpec); if (op.Named && !varSpec.Exploded) { builder.Append(varSpec.Name); } foreach (var value in values) { if (!first) { builder.Append(varSpec.Exploded ? op.Separator : ","); } if (op.Named) { if (varSpec.Exploded) { builder.Append(varSpec.Name); builder.Append('='); } else if (first) { builder.Append('='); } } BuildValue(builder, varSpec, value); first = false; } if (first) { builder.Append(op.Empty); } }
private void BuildStringValue(StringBuilder builder, VarSpec varSpec, string value) { if (op.Named) { builder.Append(varSpec.Name); if (string.IsNullOrEmpty(value)) { builder.Append(op.Empty); } else { builder.Append("="); } } if (varSpec.MaxLength != 0 && varSpec.MaxLength < value.Length) { value = value.Substring(0, varSpec.MaxLength); } BuildValue(builder, varSpec, value); }
private bool Resolve(StringBuilder builder, VarSpec varSpec, object value, bool hasPrefix) { string stringValue = null; IDictionary<string, string> dictionaryValue = null; IEnumerable<string> collectionValue = null; stringValue = value as string; if (stringValue == null) { collectionValue = value as IEnumerable<string>; if (collectionValue == null) { dictionaryValue = value as IDictionary<string, string>; if (dictionaryValue == null) { throw new UriTemplateException(string.Format("Invalid type of variable value \"{0}\". Expected: string or IEnumerable<string> or IDictionary<string, string>", value.GetType())); } else if (dictionaryValue.Count == 0) { return false; } } else if (collectionValue.Count() == 0) { return false; } } if (hasPrefix) { builder.Append(op.Separator); } else { builder.Append(op.Prefix); } if (stringValue != null) { BuildStringValue(builder, varSpec, stringValue); } else if (collectionValue != null) { BuildCollectionValue(builder, varSpec, collectionValue); } else { BuildDictionaryValue(builder, varSpec, dictionaryValue); } return true; }
private void BuildValue(StringBuilder builder, VarSpec varSpec, string value) { builder.Append(PctEncoding.Escape(value, op.AllowReserved)); }
private void BuildDictionaryValue(StringBuilder builder, VarSpec varSpec, IDictionary<string, string> values) { var first = true; CheckCompositeVariables(varSpec); if (op.Named && !varSpec.Exploded) { builder.Append(varSpec.Name); builder.Append('='); } foreach (var item in values) { if (!first) { builder.Append(varSpec.Exploded ? op.Separator : ","); } var key = PctEncoding.Escape(item.Key, true); builder.Append(key); builder.Append(varSpec.Exploded ? '=' : ','); BuildValue(builder, varSpec, item.Value ?? string.Empty); first = false; } }
private void BuildCollectionValue(StringBuilder builder, VarSpec varSpec, IEnumerable<string> values) { var first = true; CheckCompositeVariables(varSpec); if (op.Named && !varSpec.Exploded) { builder.Append(varSpec.Name); } foreach (var value in values) { if (!first) { builder.Append(varSpec.Exploded ? op.Separator : ","); } if (op.Named) { if (varSpec.Exploded) { builder.Append(varSpec.Name); builder.Append('='); } else if (first) { builder.Append('='); } } BuildValue(builder, varSpec, value); first = false; } if (first) { builder.Append(op.Empty); } }