コード例 #1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            ActionParameter parameter = value as ActionParameter;

            if (parameter == null)
            {
                writer.WriteNull();
                return;
            }

            if (parameter.Kind != ParameterKind.Custom)
            {
                writer.WriteValue(_camelCase.GetPropertyName(parameter.Kind.ToString(), false));
                return;
            }

            writer.WriteStartObject();

            writer.WritePropertyName(_camelCase.GetPropertyName(nameof(ActionParameter.Name), false));
            writer.WriteValue(parameter.Name);

            writer.WritePropertyName(_camelCase.GetPropertyName(nameof(ActionParameter.Type), false));
            writer.WriteValue(parameter.Type);

            writer.WritePropertyName(_camelCase.GetPropertyName(nameof(ActionParameter.Optional), false));
            writer.WriteValue(parameter.Optional);

            writer.WriteEndObject();
        }
コード例 #2
0
        /// <summary>
        /// Gets the name and the type of a {} Template
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private (string name, string type) ParseTemplatePart(string input)
        {
            CamelCaseNamingStrategy strat = new CamelCaseNamingStrategy();

            if (input.Contains(":"))
            {
                return(strat.GetPropertyName(input.Substring(0, input.IndexOf(":")), false),
                       input.Substring(input.IndexOf(":") + 1));
            }
            return(strat.GetPropertyName(input, false), null);
        }
コード例 #3
0
ファイル: JsonHelper.cs プロジェクト: jwwicks/Encompass.Rest
            protected override JsonObjectContract CreateObjectContract(Type objectType)
            {
                var contract       = base.CreateObjectContract(objectType);
                var objectTypeInfo = objectType.GetTypeInfo();

                if (TypeData <ExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                {
                    contract.ExtensionDataGetter = o => ((DirtyDictionary <string, object>)((ExtensibleObject)o).ExtensionData).GetDirtyItems().Select(p => new KeyValuePair <object, object>(p.Key, p.Value));
                    contract.ExtensionDataSetter = (o, k, v) => ((ExtensibleObject)o).ExtensionData[k] = v;
                    if (TypeData <DirtyExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                    {
                        var idPropertyName       = DirtyExtensibleObject.GetIdPropertyName(objectTypeInfo);
                        var backingFieldTypeInfo = GetBackingFieldInfo(objectType, idPropertyName)?.FieldInfo.FieldType.GetTypeInfo();
                        idPropertyName = CamelCaseNamingStrategy.GetPropertyName(idPropertyName, false);
                        var idProperty = contract.Properties.GetClosestMatchProperty(idPropertyName);
                        if (idProperty != null && (backingFieldTypeInfo == null || !backingFieldTypeInfo.IsGenericType || backingFieldTypeInfo.IsGenericTypeDefinition || backingFieldTypeInfo.GetGenericTypeDefinition() != TypeData.OpenNeverSerializeValueType))
                        {
                            idProperty.ShouldSerialize = o => ((IIdentifiable)o).Id != null;
                        }

                        ReadEntityAttributes(objectTypeInfo, contract);
                    }
                }
                return(contract);
            }
コード例 #4
0
ファイル: JsonHelper.cs プロジェクト: yornstei/EncompassRest
            protected override JsonObjectContract CreateObjectContract(Type objectType)
            {
                var contract       = base.CreateObjectContract(objectType);
                var objectTypeInfo = objectType.GetTypeInfo();

                if (TypeData <ExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                {
                    contract.ExtensionDataGetter = o => ((DirtyDictionary <string, object>)((ExtensibleObject)o).ExtensionData).GetDirtyItems().Select(p => new KeyValuePair <object, object>(p.Key, p.Value));
                    contract.ExtensionDataSetter = (o, k, v) => ((ExtensibleObject)o).ExtensionData[k] = v;
                    if (TypeData <DirtyExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                    {
                        var idPropertyName       = DirtyExtensibleObject.GetIdPropertyName(objectTypeInfo);
                        var backingFieldTypeInfo = GetBackingFieldInfo(objectType, idPropertyName)?.FieldInfo.FieldType.GetTypeInfo();
                        idPropertyName = CamelCaseNamingStrategy.GetPropertyName(idPropertyName, false);
                        var property = contract.Properties.GetClosestMatchProperty(idPropertyName);
                        if (property != null && (backingFieldTypeInfo == null || !backingFieldTypeInfo.IsGenericType || backingFieldTypeInfo.IsGenericTypeDefinition || backingFieldTypeInfo.GetGenericTypeDefinition() != TypeData.OpenNeverSerializeValueType))
                        {
                            property.ShouldSerialize = o => ((IIdentifiable)o).Id != null;
                        }

                        var entityAttribute = objectTypeInfo.GetCustomAttribute <EntityAttribute>(false);
                        if (entityAttribute != null && !string.IsNullOrEmpty(entityAttribute.PropertiesToAlwaysSerialize))
                        {
                            var propertiesToAlwaysSerialize = entityAttribute.PropertiesToAlwaysSerialize.Split(',');
                            foreach (var propertyToAlwaysSerialize in propertiesToAlwaysSerialize)
                            {
                                property = contract.Properties.GetClosestMatchProperty(propertyToAlwaysSerialize);
                                var valueProvider = property.ValueProvider;
                                property.ShouldSerialize = o => valueProvider.GetValue(o) != null;
                            }
                        }
                    }
                }
                return(contract);
            }
コード例 #5
0
        public IEnumerable <FieldResponse> GetAllForForm(string id)
        {
            var result = new List <FieldResponse>();

            foreach (var field in FieldProvider.GetAllFor(id))
            {
                if (!field.AutoGenerate)
                {
                    continue;
                }

                var control        = ControlMatcher.GetFor(field.Type, field.UIHints);
                var embeddedFormId = FormProvider.GetAll().FirstOrDefault(f => f.Type == field.Type);

                if (control == null && embeddedFormId == null)
                {
                    continue;
                }

                result.Add(new FieldResponse
                {
                    Id             = field.Id,
                    Label          = field.Label ?? Humanizer.Humanize(field.Id),
                    CamelCaseId    = CamelCaseNamingStrategy.GetPropertyName(field.Id, false),
                    Control        = control,
                    EmbeddedFormId = embeddedFormId?.Id,
                    IsSortable     = field.IsSortable,
                    Group          = field.Group,
                });
            }

            return(result);
        }
コード例 #6
0
 /// <summary>
 /// 将CaseNaming 转换为 caseNaming 格式
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public static string CamelCaseNaming(this string name)
 {
     if (string.IsNullOrEmpty(name))
     {
         return(string.Empty);
     }
     return(camelCaseNaming.GetPropertyName(name, false));
 }
コード例 #7
0
 public static string ToCamelCase(this string str)
 {
     if (string.IsNullOrWhiteSpace(str))
     {
         return(str);
     }
     return(string.Join('.', str.Split('.').Select(s => camel.GetPropertyName(s, false))));
 }
コード例 #8
0
        public IEnumerable <FieldResponse> GetAllForForm(string id)
        {
            var result = new List <FieldResponse>();

            foreach (var field in FieldProvider.GetAllFor(id))
            {
                if (!field.AutoGenerate)
                {
                    continue;
                }

                var control        = ControlMatcher.GetFor(field.Type, field.UIHints);
                var embeddedFormId = FormProvider.GetAll().FirstOrDefault(f => f.Type == field.Type);

                if (control == null && embeddedFormId == null)
                {
                    Logger.LogInformation($"Could not find control for {id} {field.Id}");
                    continue;
                }

                var label = field.Label;

                if (label == null)
                {
                    label = field.Id;
                    label = Humanizer.Humanize(field.Id);

                    if (label.EndsWith(" ids"))
                    {
                        label = label.Substring(0, label.Length - " ids".Length);
                        label = Pluralizer.Pluralize(label);
                    }
                    else if (label.EndsWith(" id"))
                    {
                        label = label.Substring(0, label.Length - " id".Length);
                    }
                }

                var singularLabel = Singularizer.Singularize(label);

                result.Add(new FieldResponse
                {
                    Id             = field.Id,
                    Label          = label,
                    SingularLabel  = singularLabel,
                    CamelCaseId    = CamelCaseNamingStrategy.GetPropertyName(field.Id, false),
                    Control        = control,
                    EmbeddedFormId = embeddedFormId?.Id,
                    IsSortable     = field.IsSortable,
                    Group          = field.Group,
                });
            }

            return(result);
        }
コード例 #9
0
        /// <inheritdoc/>
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            if (property.DeclaringType == typeof(Instrumentation.ApolloTrace) || property.DeclaringType?.DeclaringType == typeof(Instrumentation.ApolloTrace))
            {
                property.PropertyName = _camelCase.GetPropertyName(member.Name, false);
            }

            return(property);
        }
コード例 #10
0
 public static string CreateCamel(string modelName)
 {
     if (modelName == null)
     {
         return("null");
     }
     if (String.IsNullOrWhiteSpace(modelName))
     {
         return("empty");
     }
     return(EscapeKeyword(camelNaming.GetPropertyName(modelName, false)));
 }
コード例 #11
0
        protected virtual string ResolvePropertyName([NotNull] string name, bool hasSpecifiedName)
        {
            switch (PropertyNameStrategy)
            {
            case NamingStrategyType.CamelCase:
                return(__camelCaseNaming.GetPropertyName(name, hasSpecifiedName));

            case NamingStrategyType.SnakeCase:
                return(__snakeCaseNaming.GetPropertyName(name, hasSpecifiedName));

            default:
                return(__defaultNaming.GetPropertyName(name, hasSpecifiedName));
            }
        }
コード例 #12
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var type       = value.GetType();
            var properties = type.GetRuntimeProperties();

            writer.WriteStartObject();
            writer.WritePropertyName("type");
            writer.WriteValue(_typesToNames[type]);

            foreach (var property in properties.Where(p => p.CanWrite)) // TODO: Technically an IList property is writable as well.
            {
                var propertyName = _namingStrategy.GetPropertyName(property.Name, false);
                writer.WritePropertyName(propertyName);
                serializer.Serialize(writer, property.GetValue(value));
            }

            writer.WriteEndObject();
        }
コード例 #13
0
ファイル: JsonHelper.cs プロジェクト: gashach/EncompassRest
            protected override JsonObjectContract CreateObjectContract(Type objectType)
            {
                var contract       = base.CreateObjectContract(objectType);
                var objectTypeInfo = objectType.GetTypeInfo();

                if (TypeData <ExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                {
                    contract.ExtensionDataGetter = o => ((DirtyDictionary <string, object>)(((ExtensibleObject)o).ExtensionData)).GetDirtyItems().Select(p => new KeyValuePair <object, object>(p.Key, p.Value));
                    contract.ExtensionDataSetter = (o, k, v) => ((ExtensibleObject)o).ExtensionData[k] = v;
                    var idProperty = GetIdProperty(objectTypeInfo);
                    var idPropertyNameAttribute = idProperty.GetCustomAttribute <IdPropertyNameAttribute>(false);
                    var idPropertyName          = idPropertyNameAttribute != null?CamelCaseNamingStrategy.GetPropertyName(idPropertyNameAttribute.IdPropertyName, false) : "id";

                    var property = contract.Properties.GetClosestMatchProperty(idPropertyName);
                    if (property != null)
                    {
                        property.ShouldSerialize = o => ((IIdentifiable)o).Id != null;
                    }
                }
                return(contract);
            }
コード例 #14
0
 /// <summary>
 /// This function can be overwritten to add any additional custom query strings needed
 /// to the query url. The base class version just returns url, so there is no need to
 /// call it from your subclass.
 /// </summary>
 /// <param name="rel">The input rel.</param>
 /// <param name="queryString">The query builder.</param>
 /// <returns>The customized query string.</returns>
 protected override void AddCustomQuery(String rel, RequestDataBuilder queryString)
 {
     //Get all properties except offset and limit
     foreach (var prop in typeof(TQuery).GetTypeInfo().GetProperties().Where(i => i.Name != nameof(IPagedCollectionQuery.Offset) && i.Name != nameof(IPagedCollectionQuery.Limit)))
     {
         var value = prop.GetValue(this.query);
         if (value != null)
         {
             var name       = camelNaming.GetPropertyName(prop.Name, false);
             var collection = value as System.Collections.ICollection;
             if (collection != null)
             {
                 foreach (var item in collection)
                 {
                     queryString.AppendItem(name, item?.ToString());
                 }
             }
             else
             {
                 queryString.AppendItem(name, value.ToString());
             }
         }
     }
 }
コード例 #15
0
        public IEnumerable <string> GetNames(IEnumerable <string> dt)
        {
            CamelCaseNamingStrategy strat = new CamelCaseNamingStrategy();
            var setNames  = new List <string>();
            int nullCount = 0;

            foreach (var item in dt)
            {
                string name = strat.GetPropertyName(item, false);
                if (name == null)
                {
                    name = "column" + (nullCount == 0 ? "" : nullCount.ToString());
                    nullCount++;
                }
                var origName = name;
                int i        = 1;
                while (setNames.Contains(name))
                {
                    name = origName + "_" + i.ToString();
                }
                setNames.Add(name);
                yield return(name);
            }
        }
コード例 #16
0
 /// <summary>
 /// This will add all the properties from the query class to the query for this collection.
 /// </summary>
 /// <param name="rel"></param>
 /// <param name="queryString"></param>
 public virtual void AddQuery(string rel, RequestDataBuilder queryString)
 {
     //Get all properties except offset and limit
     foreach (var prop in typeof(TQuery).GetTypeInfo().GetProperties())
     {
         var value = prop.GetValue(this.query);
         if (value != null)
         {
             var name       = camelNaming.GetPropertyName(prop.Name, false);
             var collection = value as System.Collections.ICollection;
             if (collection != null)
             {
                 foreach (var item in collection)
                 {
                     queryString.AppendItem(name, item?.ToString());
                 }
             }
             else
             {
                 queryString.AppendItem(name, value.ToString());
             }
         }
     }
 }
コード例 #17
0
 public static string ToJsonPropName(this string propName)
 {
     return(CamelCaseNamingStrategy.GetPropertyName(propName, false));
 }
コード例 #18
0
        public IEnumerable <ContentTypeResponseItem> GetContentTypes()
        {
            var result = new List <ContentTypeResponseItem>();

            foreach (var contentType in ContentTypeProvider.GetAll())
            {
                var    name = contentType.Type.GetCustomAttribute <DisplayAttribute>()?.Name ?? contentType.Type.Name;
                string pluralName;

                if (name.Contains(':') && !contentType.Id.Contains(':'))
                {
                    var nameSplit = name.Split(':');

                    name       = nameSplit.First();
                    pluralName = nameSplit.Last();
                }
                else
                {
                    name       = Humanizer.Humanize(name);
                    pluralName = Pluralizer.Pluralize(name);
                }

                var singleton = SingletonProvider.Get(contentType.Id);

                result.Add(new ContentTypeResponseItem
                {
                    Id                       = contentType.Id,
                    Name                     = name,
                    PluralName               = pluralName,
                    IsNameable               = typeof(INameable).IsAssignableFrom(contentType.Type),
                    NameablePropertyName     = typeof(INameable).IsAssignableFrom(contentType.Type) ? CamelCaseNamingStrategy.GetPropertyName(NameExpressionParser.Parse(contentType.Type), false) : null,
                    IsRoutable               = typeof(IRoutable).IsAssignableFrom(contentType.Type),
                    IsSingleton              = singleton != null,
                    Count                    = -1,
                    ContentTypeActionModules = ContentTypeActionModuleProvider.GetContentTypeActionModulesFor(contentType.Id),
                    ListActionModules        = ListActionModuleProvider.GetListActionModulesFor(contentType.Id),
                });
            }

            return(result.AsReadOnly());
        }
コード例 #19
0
        ContentTypeResponseItem GetItem(ContentTypeDescriptor contentType)
        {
            var    name = contentType.Type.GetCustomAttribute <DisplayAttribute>()?.Name ?? contentType.Type.Name;
            string pluralName;

            if (name.Contains(':') && !contentType.Id.Contains(':'))
            {
                var nameSplit = name.Split(':');

                name       = nameSplit.First();
                pluralName = nameSplit.Last();
            }
            else
            {
                name       = Humanizer.Humanize(name);
                pluralName = Pluralizer.Pluralize(name);
            }

            var singleton = SingletonProvider.Get(contentType.Id);

            var item = new ContentTypeResponseItem
            {
                Id                       = contentType.Id,
                Name                     = name,
                LowerCaseName            = name.Substring(0, 1).ToLower() + name.Substring(1),
                PluralName               = pluralName,
                LowerCasePluralName      = pluralName.Substring(0, 1).ToLower() + pluralName.Substring(1),
                IsNameable               = typeof(INameable).IsAssignableFrom(contentType.Type),
                NameablePropertyName     = typeof(INameable).IsAssignableFrom(contentType.Type) ? CamelCaseNamingStrategy.GetPropertyName(NameExpressionParser.Parse(contentType.Type), false) : null,
                IsImageable              = typeof(IImageable).IsAssignableFrom(contentType.Type),
                ImageablePropertyName    = typeof(IImageable).IsAssignableFrom(contentType.Type) ? CamelCaseNamingStrategy.GetPropertyName(ImageExpressionParser.Parse(contentType.Type), false) : null,
                IsRoutable               = typeof(IRoutable).IsAssignableFrom(contentType.Type),
                IsSingleton              = singleton != null,
                Count                    = -1,
                ContentTypeActionModules = ContentTypeActionModuleProvider.GetContentTypeActionModulesFor(contentType.Id),
                ListActionModules        = ListActionModuleProvider.GetListActionModulesFor(contentType.Id),
                ContentTypeGroups        = ContentTypeGroupMatcher.GetContentTypeGroupsFor(contentType.Id).Select(t => t.Id).ToList().AsReadOnly(),
            };

            return(item);
        }
コード例 #20
0
 public static string ToCamelCase(this string str)
 {
     return(CamelCaseNamingStrategy.GetPropertyName(str, false));
 }