コード例 #1
0
        public async Task Create(Type collectionType, FaunaClient client)
        {
            FaunaCollection collection = collectionType.GetCustomAttribute <FaunaCollection>();

            var options = new Dictionary <string, Expr>();

            if (!string.IsNullOrEmpty(this.Name))
            {
                options.Add("name", this.Name);
            }
            else
            {
                options.Add("name", GenerateIndexName(collectionType));
            }
            options.Add("source", Collection(collection?.Name ?? collectionType.Name));

            if (Terms != null && Terms.Length > 0)
            {
                options.Add("terms", GenerateTermsObj(collectionType));
            }

            if (Values != null && Values.Length > 0)
            {
                options.Add("values", GenerateValuesObj(collectionType));
            }

            if (this.Data != null)
            {
                options.Add("data", Obj(JsonConvert.DeserializeObject <Dictionary <string, Expr> >(Data)));
            }
            if (this.Permissions != null)
            {
                options.Add("permissions", Obj(JsonConvert.DeserializeObject <Dictionary <string, Expr> >(Permissions)));
            }
            options.Add("unique", this.Unique);
            options.Add("serialized", this.Serialized);

            await client.Query(CreateIndex(options));
        }
コード例 #2
0
        public string GenerateIndexName(Type collectionType)
        {
            FaunaCollection collection = collectionType.GetCustomAttribute <FaunaCollection>();

            string s = string.Empty;

            if (Values == null ||
                (Values != null && Values.Length == 0) ||
                (Values != null && Values.Length == 1 && Values[0].GetType() == typeof(FaunaValueType) && (FaunaValueType)Enum.Parse(typeof(FaunaValueType), Values[0].ToString()) == FaunaValueType.Ref))
            {
                s += $"{collection?.Name ?? collectionType.Name}";
            }
            else
            {
                int i = 0;
                foreach (object val in Values)
                {
                    if (i > 0)
                    {
                        s += "_";
                    }

                    if (val.GetType() != typeof(FaunaValueType))
                    {
                        if (val.GetType() == typeof(IndexOrdering))
                        {
                            IndexOrdering orderingType = (IndexOrdering)Enum.Parse(typeof(IndexOrdering), val.ToString());
                            if (orderingType == IndexOrdering.Reverse)
                            {
                                s += $"reverse";
                            }
                        }
                        else
                        {
                            FaunaFieldAttribute fieldAttribute = collectionType.GetProperty(val.ToString())?.GetCustomAttribute <FaunaFieldAttribute>();
                            if (fieldAttribute == null)
                            {
                                fieldAttribute = collectionType.GetField(val.ToString())?.GetCustomAttribute <FaunaFieldAttribute>();
                            }

                            s += $"{fieldAttribute?.Name ?? val.ToString()}";
                        }
                    }
                    else
                    {
                        FaunaValueType faunaValueType = (FaunaValueType)Enum.Parse(typeof(FaunaValueType), val.ToString());
                        if (faunaValueType == FaunaValueType.Ref)
                        {
                            s += $"{collection?.Name ?? collectionType.Name}";
                        }
                        else if (faunaValueType == FaunaValueType.Ts)
                        {
                            s += $"ts";
                        }
                    }
                    i++;
                }
            }
            s += "_by";
            if (Terms != null)
            {
                for (int i = 0; i < Terms.Length; i++)
                {
                    object term = Terms[i];
                    s += "_";
                    if (term.GetType() != typeof(FaunaValueType))
                    {
                        FaunaFieldAttribute fieldAttribute = collectionType.GetProperty(term.ToString())?.GetCustomAttribute <FaunaFieldAttribute>();
                        if (fieldAttribute == null)
                        {
                            fieldAttribute = collectionType.GetField(term.ToString())?.GetCustomAttribute <FaunaFieldAttribute>();
                        }
                        s += $"{fieldAttribute?.Name ?? term.ToString()}";
                    }
                    else
                    {
                        FaunaTermType faunaValueType = (FaunaTermType)Enum.Parse(typeof(FaunaTermType), term.ToString());
                        if (faunaValueType == FaunaTermType.Ref)
                        {
                            s += $"{collection?.Name ?? collectionType.Name}";
                        }
                        else if (faunaValueType == FaunaTermType.Ts)
                        {
                            s += $"ts";
                        }
                    }
                }
            }
            else
            {
                //TODO figure out what to call it when there are no terms
            }
            return(s);
        }