<commit_before>﻿namespace SharpMongo.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class DynamicDocument : DynamicObject
    {
        public DynamicDocument(params object[] arguments) 
            : base(arguments)
        {
        }

        public object Id
        {
            get
            {
                return this.GetMember("Id");
            }

            set
            {
                this.SetMember("Id", value);
            }
        }

        public DynamicDocument Project(DynamicObject projection)
        {
            IList<string> names = new List<string>();

            names.Add("Id");

            foreach (var name in projection.GetMemberNames().Where(n => IsTrue(projection.GetMember(n))))
                if (!names.Contains(name))
                    names.Add(name);

            if (names.Count == 1)
                names = this.GetMemberNames().ToList();

            foreach (var name in projection.GetMemberNames().Where(n => IsFalse(projection.GetMember(n))))
                if (names.Contains(name))
                    names.Remove(name);

            DynamicDocument document = new DynamicDocument();

            foreach (var name in names)
                document.SetMember(name, this.GetMember(name));

            foreach (var name in  projection.GetMemberNames().Where(n => !IsFalse(projection.GetMember(n)) && !IsTrue(projection.GetMember(n))))
                document.SetMember(name, projection.GetMember(name));

            return document;
        }

        private static bool IsFalse(object value)<commit_msg>Implement evaluate expression in document project new field<commit_after>﻿namespace SharpMongo.Core
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class DynamicDocument : DynamicObject
    {
        public DynamicDocument(params object[] arguments) 
            : base(arguments)
        {
        }

        public object Id
        {
            get
            {
                return this.GetMember("Id");
            }

            set
            {
                this.SetMember("Id", value);
            }
        }

        public DynamicDocument Project(DynamicObject projection)
        {
            IList<string> names = new List<string>();

            names.Add("Id");

            foreach (var name in projection.GetMemberNames().Where(n => IsTrue(projection.GetMember(n))))
                if (!names.Contains(name))
                    names.Add(name);

            if (names.Count == 1)
                names = this.GetMemberNames().ToList();

            foreach (var name in projection.GetMemberNames().Where(n => IsFalse(projection.GetMember(n))))
                if (names.Contains(name))
                    names.Remove(name);

            DynamicDocument document = new DynamicDocument();

            foreach (var name in names)
                document.SetMember(name, this.GetMember(name));

            foreach (var name in  projection.GetMemberNames().Where(n => !IsFalse(projection.GetMember(n)) && !IsTrue(projection.GetMember(n))))
                document.SetMember(name, this.Evaluate(projection.GetMember(name)));

            return document;
        }

        private object Evaluate(object value)
        {
            if (!(value is DynamicObject))
                return value;

            var dynobj = (DynamicObject)value;

            if (dynobj.Exists("$eq"))
            {
                var values = (IEnumerable<object>)dynobj.GetMember("$eq");
                var value1 = this.GetValue(values.First());
                var value2 = this.GetValue(values.Skip(1).First());

                return value1.Equals(value2);
            }

            return null;
        }

        private object GetValue(object value)
        {
            if (value is string)
            {
                string name = (string)value;

                if (name.Length > 0 && name[0] == '$')
                    return this.GetMember(name.Substring(1));
            }

            return value;
        }

        private static bool IsFalse(object value)
{'fraction_non_alphanumeric': 0.10471698113207548, 'fraction_numerical': 0.0023584905660377358, 'mean_word_length': 1.6522826766729206, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6186233', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1020, 'n_words': 229}