private IEnumerable <string> GetFilteredDictionary(IDictionary dictionary, bool filterKeys = false) => dictionary .Keys .Cast <object>() .Select(key => key == null ? string.Empty : key.ToStringInvariant()) .Where(stringKey => !IgnorePropertyNames.Contains(stringKey)) .Select(stringKey => filterKeys ? stringKey : dictionary[stringKey] == null ? string.Empty : dictionary[stringKey].ToStringInvariant());
/// <summary> /// Gets the filtered type properties using the IgnoreProperties list. /// </summary> /// <param name="type">The type.</param> /// <returns>Filtered type properties using the IgnoreProperties list.</returns> private PropertyInfo[] GetFilteredTypeProperties(Type type) { lock (_syncLock) { return(TypeCache.Retrieve(type, () => { return type.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.CanRead) .ToArray(); }) .Where(p => IgnorePropertyNames.Contains(p.Name) == false) .ToArray()); } }
/// <summary> /// Gets the filtered dictionary values using the IgnoreProperties list. /// </summary> /// <param name="dictionary">The dictionary.</param> /// <returns>An array containing copies of the elements of the dictionary.</returns> private object[] GetFilteredDictionaryValues(IDictionary dictionary) { var values = new List <object>(); foreach (var key in dictionary.Keys) { var stringKey = key == null ? string.Empty : key.ToStringInvariant(); if (IgnorePropertyNames.Contains(stringKey)) { continue; } values.Add(dictionary[stringKey]); } return(values.ToArray()); }
/// <summary> /// Gets the filtered dictionary keys using the IgnoreProperties list. /// </summary> /// <param name="dictionary">The dictionary.</param> /// <returns>An array containing copies of the elements of the dictionary.</returns> private string[] GetFilteredDictionaryKeys(IDictionary dictionary) { var keys = new List <string>(); foreach (var key in dictionary.Keys) { var stringKey = key == null ? string.Empty : key.ToStringInvariant(); if (IgnorePropertyNames.Contains(stringKey)) { continue; } keys.Add(stringKey); } return(keys.ToArray()); }
/// <summary> /// Gets the filtered dictionary keys using the IgnoreProperties list. /// </summary> /// <param name="dictionary">The dictionary.</param> /// <returns>An array containing copies of the elements of the dictionary</returns> private object[] GetFilteredDictionaryKeys(IDictionary <string, object> dictionary) { var keys = new List <object>(); foreach (var key in dictionary.Keys) { var stringKey = key ?? string.Empty; if (IgnorePropertyNames.Contains(stringKey)) { continue; } keys.Add(stringKey); } return(keys.ToArray()); }
public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool initCollections, bool includeResponseStatus) { var wasAdded = false; var dataMemberIndex = 1; foreach (var prop in type.Properties.Safe()) { if (wasAdded) { sb.AppendLine(); } var propTypeName = Type(prop.Type, prop.GenericArgs); var propType = FindType(prop.Type, prop.TypeNamespace, prop.GenericArgs); var optional = ""; var defaultValue = ""; if (propTypeName.EndsWith("?")) { propTypeName = propTypeName.Substring(0, propTypeName.Length - 1); optional = "?"; } if (Config.MakePropertiesOptional) { optional = "?"; } if (prop.Attributes.Safe().FirstOrDefault(x => x.Name == "Required") != null) { optional = "?"; //always use optional } if (prop.IsArray()) { optional = ""; defaultValue = " = []"; } else if (initCollections && !prop.GenericArgs.IsEmpty()) { if (ArrayTypes.Contains(prop.Type)) { optional = ""; defaultValue = " = []"; } if (DictionaryTypes.Contains(prop.Type)) { optional = ""; defaultValue = " = [:]"; } } if (propType.IsInterface() || IgnorePropertyNames.Contains(prop.Name)) { sb.AppendLine("//{0}:{1} ignored. Swift doesn't support interface properties" .Fmt(prop.Name.SafeToken().PropertyStyle(), propTypeName)); continue; } else if (IgnorePropertyTypeNames.Contains(propTypeName)) { sb.AppendLine("//{0}:{1} ignored. Type could not be extended in Swift" .Fmt(prop.Name.SafeToken().PropertyStyle(), propTypeName)); continue; } wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++); wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded; if (type.IsInterface()) { sb.AppendLine("var {0}:{1}{2} {{ get set }}".Fmt( prop.Name.SafeToken().PropertyStyle(), propTypeName, optional)); } else { sb.AppendLine("public var {0}:{1}{2}{3}".Fmt( prop.Name.SafeToken().PropertyStyle(), propTypeName, optional, defaultValue)); } } if (includeResponseStatus) { if (wasAdded) { sb.AppendLine(); } AppendDataMember(sb, null, dataMemberIndex++); sb.AppendLine("public var {0}:ResponseStatus?".Fmt(typeof(ResponseStatus).Name.PropertyStyle())); } }
private void AddTypeExtension(ref StringBuilderWrapper sbExt, MetadataType type, bool initCollections) { //Swift doesn't support extensions on same protocol used by Base and Sub types if (type.IsAbstract()) { return; } var typeName = Type(type.Name, type.GenericArgs); var typeNameOnly = typeName.SplitOnFirst('<')[0]; sbExt.AppendLine(); sbExt.AppendLine("extension {0} : JsonSerializable".Fmt(typeNameOnly)); sbExt.AppendLine("{"); sbExt = sbExt.Indent(); sbExt.AppendLine("public static var typeName:String {{ return \"{0}\" }}".Fmt(typeName)); //func typeConfig() var isGenericType = !type.GenericArgs.IsEmpty(); if (!isGenericType) { sbExt.AppendLine("public static var metadata = Metadata.create(["); sbExt = sbExt.Indent(); } else { //Swift 2.0 doesn't allow stored static properties on generic types yet sbExt.AppendLine("public static var metadata:Metadata {"); sbExt = sbExt.Indent(); sbExt.AppendLine("return Metadata.create(["); } sbExt = sbExt.Indent(); foreach (var prop in GetPropertes(type)) { var propType = FindType(prop.Type, prop.TypeNamespace, prop.GenericArgs); if (propType.IsInterface() || IgnorePropertyTypeNames.Contains(prop.Type) || IgnorePropertyNames.Contains(prop.Name)) { continue; } var fnName = "property"; if (prop.IsArray() || ArrayTypes.Contains(prop.Type)) { fnName = initCollections ? "arrayProperty" : "optionalArrayProperty"; } else if (DictionaryTypes.Contains(prop.Type)) { fnName = initCollections ? "objectProperty" : "optionalObjectProperty"; } else { if (propType != null && !propType.IsEnum.GetValueOrDefault()) { fnName = "optionalObjectProperty"; } else { fnName = "optionalProperty"; } } sbExt.AppendLine("Type<{0}>.{1}(\"{2}\", get: {{ $0.{2} }}, set: {{ $0.{2} = $1 }}),".Fmt( typeName, fnName, prop.Name.SafeToken().PropertyStyle())); } sbExt = sbExt.UnIndent(); sbExt.AppendLine("])"); sbExt = sbExt.UnIndent(); if (isGenericType) { sbExt.AppendLine("}"); } sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); }
private IEnumerable <PropertyInfo> GetFilteredTypeProperties(Type type) => TypeCache.Retrieve(type, t => t.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.CanRead)) .Where(p => !IgnorePropertyNames.Contains(p.Name));
private void AddTypeExtension(ref StringBuilderWrapper sbExt, MetadataType type, bool initCollections) { //Swift doesn't support extensions on same protocol used by Base and Sub types if (type.IsAbstract()) { return; } var typeName = Type(type.Name, type.GenericArgs); var typeNameOnly = typeName.SplitOnFirst('<')[0]; sbExt.AppendLine(); sbExt.AppendLine("extension {0} : JsonSerializable".Fmt(typeNameOnly)); sbExt.AppendLine("{"); sbExt = sbExt.Indent(); sbExt.AppendLine("public class var typeName:String {{ return \"{0}\" }}".Fmt(typeName)); //func typeConfig() sbExt.AppendLine("public class func reflect() -> Type<{0}> {{".Fmt(typeName)); sbExt = sbExt.Indent(); sbExt.AppendLine( "return TypeConfig.config() ?? TypeConfig.configure(Type<{0}>(".Fmt(typeName)); sbExt = sbExt.Indent(); sbExt.AppendLine("properties: [".Fmt(typeName)); sbExt = sbExt.Indent(); foreach (var prop in GetPropertes(type)) { var propType = FindType(prop.Type, prop.TypeNamespace, prop.GenericArgs); if (propType.IsInterface() || IgnorePropertyTypeNames.Contains(prop.Type) || IgnorePropertyNames.Contains(prop.Name)) { continue; } var fnName = "property"; if (prop.IsArray() || ArrayTypes.Contains(prop.Type)) { fnName = initCollections ? "arrayProperty" : "optionalArrayProperty"; } else if (DictionaryTypes.Contains(prop.Type)) { fnName = initCollections ? "objectProperty" : "optionalObjectProperty"; } else { if (propType != null && !propType.IsEnum.GetValueOrDefault()) { fnName = "optionalObjectProperty"; } else { fnName = "optionalProperty"; } } sbExt.AppendLine("Type<{0}>.{1}(\"{2}\", get: {{ $0.{2} }}, set: {{ $0.{2} = $1 }}),".Fmt( typeName, fnName, prop.Name.SafeToken().PropertyStyle())); } sbExt = sbExt.UnIndent(); sbExt.AppendLine("]))"); sbExt = sbExt.UnIndent(); sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); //toJson() sbExt.AppendLine("public func toJson() -> String {"); sbExt = sbExt.Indent(); sbExt.AppendLine("return {0}.reflect().toJson(self)".Fmt(typeName)); sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); //fromJson() sbExt.AppendLine("public class func fromJson(json:String) -> {0}? {{".Fmt(typeName)); sbExt = sbExt.Indent(); sbExt.AppendLine("return {0}.reflect().fromJson({0}(), json: json)".Fmt(typeName)); sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); //fromObject() sbExt.AppendLine("public class func fromObject(any:AnyObject) -> {0}? {{".Fmt(typeName)); sbExt = sbExt.Indent(); sbExt.AppendLine("return {0}.reflect().fromObject({0}(), any:any)".Fmt(typeName)); sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); //toString() sbExt.AppendLine("public func toString() -> String {"); sbExt = sbExt.Indent(); sbExt.AppendLine("return {0}.reflect().toString(self)".Fmt(typeName)); sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); //fromString() sbExt.AppendLine("public class func fromString(string:String) -> {0}? {{".Fmt(typeName)); sbExt = sbExt.Indent(); sbExt.AppendLine("return {0}.reflect().fromString({0}(), string: string)".Fmt(typeName)); sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); sbExt = sbExt.UnIndent(); sbExt.AppendLine("}"); }