public string ConvertToWireType(string source, string clientReference) { if (Location != ParameterLocation.Body && Location != ParameterLocation.FormData && NeedsSpecialSerialization(ClientType)) { var primary = ClientType as PrimaryTypeModel; var sequence = ClientType as SequenceTypeModel; if (primary != null && primary.IsPrimaryType(KnownPrimaryType.ByteArray)) { if (WireType.IsPrimaryType(KnownPrimaryType.String)) { return(string.Format(CultureInfo.InvariantCulture, "{0} {1} = Base64.encodeBase64String({2});", WireType.Name, _wireName, source)); } else { return(string.Format(CultureInfo.InvariantCulture, "{0} {1} = Base64Url.encode({2});", WireType.Name, _wireName, source)); } } else if (sequence != null) { return(string.Format(CultureInfo.InvariantCulture, "{0} {1} = {2}.mapperAdapter().serializeList({3}, CollectionFormat.{4});", WireType.Name, _wireName, clientReference, source, CollectionFormat.ToString().ToUpper(CultureInfo.InvariantCulture))); } } return(convertClientTypeToWireType(WireType, source, _wireName, clientReference)); }
/// <summary> /// Return formatted value string for the parameter. /// </summary> /// <returns></returns> public string ValueForMap() { if (IsAPIVersion) { return(APIVersionName); } var value = IsClientProperty ? "client." + CodeNamerGo.Instance.GetPropertyName(Name.Value) : Name.Value; var format = IsRequired || ModelType.CanBeEmpty() ? "{0}" : "*{0}"; var s = CollectionFormat != CollectionFormat.None ? $"{format},\"{CollectionFormat.GetSeparator()}\"" : $"{format}"; return(string.Format( RequiresUrlEncoding() ? $"autorest.Encode(\"{Location.ToString().ToLower()}\",{s})" : $"{s}", value)); }
public string ShouldApplyFormatOptions(CollectionFormatOptions options) { var format = new CollectionFormat() .AddOptions(options) .SetCollectionPrefixAndSuffix("[", "]") .SetIndexValuePairPrefixAndSuffix("<", ">") .SetIndexPrefixAndSuffix("'", "'") .SetValuePrefixAndSuffix("'", "'") .SetIndexValueSeparator(":") .SetIndexValuePairSeparator(","); return(format.Compile()(new [] { "John", "", null })); }
public void ShouldCreateFormatWithDefaultSetting() { ValueTuple <string, string> defaultPrefixAndSuffix = (null, null); var format = new CollectionFormat(); var toString = format.Compile(); Assert.That(format.IndexValuePairSeparator, Is.Null); Assert.That(format.IndexValueSeparator, Is.Null); Assert.That(format.IndexValuePairPrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix)); Assert.That(format.ValuePrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix)); Assert.That(format.IndexPrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix)); Assert.That(format.CollectionPrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix)); Assert.That(format.Options, Is.EqualTo(None)); Assert.That(toString(new object[] { "John", 12, "Mercedes" }), Is.EqualTo("John12Mercedes")); }
/// <summary> /// Return formatted value string for the parameter. /// </summary> /// <returns></returns> public string ValueForMap(bool useDefault = false) { if (IsAPIVersion) { return(APIVersionName); } if (IsConstant) { return(RequiresUrlEncoding() ? $"autorest.Encode(\"{Location.ToString().ToLower()}\", {DefaultValueString})" : DefaultValueString); } string value = ""; if (useDefault) { value = DefaultValueString; } else if (IsClientProperty) { value = GetClientPropertryName(); } else { value = Name.Value; } var format = IsRequired || ModelType.CanBeEmpty() || useDefault ? "{0}" : "*{0}"; var s = CollectionFormat != CollectionFormat.None ? $"{format},\"{CollectionFormat.GetSeparator()}\"" : $"{format}"; return(string.Format( RequiresUrlEncoding() ? $"autorest.Encode(\"{Location.ToString().ToLower()}\",{s})" : $"{s}", value)); }
public void DefaultCollectionFormatCanBeSpecifiedInSettings(CollectionFormat format, string expectedFormat) { var settingsWithCollectionFormat = new RefitSettings { CollectionFormat = format }; var source = new ObjectWithRepeatedFieldsTestClass { // Members have explicit CollectionFormat A = new List <int> { 1, 2 }, B = new HashSet <string> { "set1", "set2" }, C = new HashSet <int> { 1, 2 }, D = new List <double> { 0.1, 1.0 }, E = new List <bool> { true, false }, // Member has no explicit CollectionFormat F = new[] { 1, 2, 3 } }; var expected = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("A", "01"), new KeyValuePair <string, string>("A", "02"), new KeyValuePair <string, string>("B", "set1,set2"), new KeyValuePair <string, string>("C", "01 02"), new KeyValuePair <string, string>("D", "0.10\t1.00"), new KeyValuePair <string, string>("E", "True|False"), new KeyValuePair <string, string>("F", expectedFormat), }; var actual = new FormValueMultimap(source, settingsWithCollectionFormat); Assert.Equal(expected, actual); }
public void ShouldAlwaysCreateNewInstanceOfDefaultFormat() { var firstFormat = CollectionFormat.CreateDefault() .AddOptions(IncludeIndex) .SetIndexValuePairPrefixAndSuffix("<", ">"); var firstToString = firstFormat.Compile(); var secondFormat = CollectionFormat.CreateDefault(); var secondToString = secondFormat.Compile(); Assert.That(secondFormat, Is.Not.SameAs(firstFormat)); Assert.That(secondToString, Is.Not.SameAs(firstToString)); Assert.That( secondToString(new object[] { "John", "007", null, "Bond" }), Is.EqualTo("['John', '007', '', 'Bond']") ); }
/// <summary> /// Return the separator associated with a given collectionFormat /// </summary> /// <param name="format">The collection format</param> /// <returns>The separator</returns> private static string GetSeparator(this CollectionFormat format) { switch (format) { case CollectionFormat.Csv: return(","); case CollectionFormat.Pipes: return("|"); case CollectionFormat.Ssv: return(" "); case CollectionFormat.Tsv: return("\t"); default: throw new NotSupportedException(string.Format("Collection format {0} is not supported.", format)); } }
public void ShouldCreateDefaultFormat() { ValueTuple <string, string> defaultPrefixAndSuffix = (null, null); var format = CollectionFormat.CreateDefault(); var toString = format.Compile(); Assert.That(toString, Is.Not.Null); Assert.That(format.IndexValueSeparator, Is.Null); Assert.That(format.IndexValuePairSeparator, Is.EqualTo(", ")); Assert.That(format.ValuePrefixAndSuffix, Is.EqualTo(("'", "'"))); Assert.That(format.IndexPrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix)); Assert.That(format.IndexValuePairPrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix)); Assert.That(format.CollectionPrefixAndSuffix, Is.EqualTo(("[", "]"))); Assert.That(format.Options, Is.EqualTo(IncludeNullValues)); Assert.That( toString(new object[] { "John", "007", null, "Bond" }), Is.EqualTo("['John', '007', '', 'Bond']") ); }
/// <summary> /// List in a form all the properties of the ViewData Model applying the appropriate format to the identified property. /// </summary> /// <example> /// html.PropertyListEdit("TestCollectionOne", CustomHtmlHelper.CollectionFormat.Table) /// </example> public static MvcHtmlString PropertyListEdit <TModel>(this HtmlHelper <TModel> html, string propertyId, CollectionFormat format) { if (html.ViewData.Model == null) { throw new ArgumentException("html"); } return(html.PropertyListEdit(html.ViewData.Model, propertyId, format)); }
/// <summary> /// 格式化集合 /// </summary> /// <param name="collection">集合</param> /// <param name="format">格式</param> /// <returns></returns> public static IEnumerable <KeyValuePair <string, string> > FormatAs(this IEnumerable <KeyValuePair <string, string> > collection, CollectionFormat format) { if (format == CollectionFormat.Multi) { return(collection); } switch (format) { case CollectionFormat.Csv: return(collection.FormatAs(@",")); case CollectionFormat.Ssv: return(collection.FormatAs(@" ")); case CollectionFormat.Tsv: return(collection.FormatAs(@"\")); case CollectionFormat.Pipes: return(collection.FormatAs(@"|")); default: throw new NotImplementedException(format.ToString()); } }
public QueryAttribute(CollectionFormat collectionFormat) { CollectionFormat = collectionFormat; }
/// <summary> /// Returns true if the specified CollectionFormat requires a separator. /// </summary> public static bool CollectionFormatRequiresSeparator(this CollectionFormat format) { return(format != CollectionFormat.None && format != CollectionFormat.Multi); }
/// <summary> /// List all the properties of the domain object with only collections /// </summary> /// <example> /// html.PropertyList(obj) /// </example> public static MvcHtmlString PropertyListOnlyCollections(this HtmlHelper html, object domainObject, CollectionFormat format) { INakedObject nakedObject = html.Framework().GetNakedObject(domainObject); IEnumerable <string> collections = ((IObjectSpec)nakedObject.Spec).Properties.OfType <IOneToManyAssociationSpec>().Select(p => p.Id); collections.ForEach(t => html.ViewData[t] = format); return(html.PropertyListWithFilter(domainObject, x => x is IOneToManyAssociationSpec, null)); }
/// <summary> /// 格式化集合 /// </summary> /// <param name="collection">集合</param> /// <param name="format">格式</param> /// <returns></returns> public static IEnumerable <KeyValue> CollectAs(this IEnumerable <KeyValue> collection, CollectionFormat format) { return(format switch { CollectionFormat.Multi => collection, CollectionFormat.Csv => collection.CollectAs(@","), CollectionFormat.Ssv => collection.CollectAs(@" "), CollectionFormat.Tsv => collection.CollectAs(@"\"), CollectionFormat.Pipes => collection.CollectAs(@"|"), _ => throw new NotImplementedException(format.ToString()), });
/// <summary> /// List in a form all the properties of the model parameter applying the appropriate format to the identified property. /// </summary> /// <example> /// html.PropertyListEdit(obj, y => y.TestCollectionOne, CustomHtmlHelper.CollectionFormat.Table) /// </example> public static MvcHtmlString PropertyListEdit <TModel>(this HtmlHelper html, TModel model, Expression <Func <TModel, IEnumerable> > expression, CollectionFormat format) { return(html.PropertyListEdit(model, html.GetProperty(expression).Name, format)); }
/// <summary> /// Use <see cref="CollectionFormat"/> for converting members of <see cref="ICollection"/> type /// to string. Only affects members which declared types are assignable to <see cref="ICollection"/>. /// </summary> /// <param name="setup"> /// Function accepting default <see cref="CollectionFormat"/> and returning modified version or brand new /// instance to be used by <see cref="object.ToString"/> function being built. When it returns null than /// default <see cref="CollectionFormat"/> (the one passed to it) will be used. /// <see cref="CollectionFormat.CreateDefault"/> will be used when function is omitted (or null passed). /// </param> /// <returns>Updated <see cref="ToStringBuilder{TTarget}"/> instance.</returns> public ToStringBuilder <TTarget> UseCollectionFormat( Func <CollectionFormat, CollectionFormat> setup = null ) { return(Use <ICollection>(SetupFormat(CollectionFormat.CreateDefault(), setup).Compile())); }
/// <summary> /// List in a form all the properties of the domain object applying the appropriate format to the identified property. /// </summary> /// <example> /// html.PropertyListEdit(obj, "TestCollectionOne", CustomHtmlHelper.CollectionFormat.Table) /// </example> public static MvcHtmlString PropertyListEdit(this HtmlHelper html, object domainObject, string propertyId, CollectionFormat format) { return(html.PropertyListEdit(domainObject, new Tuple <string, CollectionFormat>(propertyId, format))); }
/// <summary> /// List all the properties of the domain object with only collections /// </summary> /// <example> /// html.PropertyList(obj) /// </example> public static MvcHtmlString PropertyListOnlyCollections(this HtmlHelper html, object domainObject, CollectionFormat format) { var nakedObject = html.Facade().GetObject(domainObject); IEnumerable <string> collections = nakedObject.Specification.Properties.Where(p => p.IsCollection).Select(p => p.Id); collections.ForEach(t => html.ViewData[t] = format); Func <IAssociationFacade, bool> f = x => x.IsCollection; return(html.PropertyListWithFilter(domainObject, f, null)); }