<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Simple.OData.Client.Extensions;

namespace Simple.OData.Client
{
    internal class ValueParser
    {
        private readonly Table _table;

        public ValueParser(Table table)
        {
            _table = table;
        }

        public IDictionary<string, object> Parse(string keyValues)
        {
            if (_table.GetKeyNames().Count == 1)
            {
                var columnName = _table.GetKeyNames()[0];
                return new Dictionary<string, object>() { { columnName, ParseValue(keyValues, _table.FindColumn(columnName).PropertyType) } };
            }
            else
            {
                var dict = new Dictionary<string, object>();
                var kvs = keyValues.Split(',');
                foreach (var kv in kvs)
                {
                    var pair = kv.Split('=');
                    var columnName = pair.First();
                    dict.Add(columnName, ParseValue(pair.Last(), _table.FindColumn(columnName).PropertyType));
                }
                return dict;
            }
        }

        private object ParseValue(string value, EdmPropertyType propertyType)
        {
            return value == "null" ? null
                : propertyType.Name == EdmType.Boolean.Name ? bool.Parse(value)
                : propertyType.Name == EdmType.Byte.Name ? byte.Parse(value)
                : propertyType.Name == EdmType.SByte.Name ? sbyte.Parse(value)
                : propertyType.Name == EdmType.DateTime.Name ? DateTime.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.DateTimeOffset.Name ? DateTimeOffset.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Int16.Name ? short.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Int32.Name ? int.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Int64.Name ? long.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Single.Name ? float.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Float.Name ? float.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Double.Name ? double.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Decimal.Name ? decimal.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Guid.Name ? Guid.Parse(value)<commit_msg>OData numeric literals have suffixes. e.g. 1008L and 10.99M See: http://www.odata.org/documentation/odata-v2-documentation/overview/#6_Primitive_Data_Types<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Simple.OData.Client.Extensions;

namespace Simple.OData.Client
{
    internal class ValueParser
    {
        private readonly Table _table;

        public ValueParser(Table table)
        {
            _table = table;
        }

        public IDictionary<string, object> Parse(string keyValues)
        {
            if (_table.GetKeyNames().Count == 1)
            {
                var columnName = _table.GetKeyNames()[0];
                return new Dictionary<string, object>() { { columnName, ParseValue(keyValues, _table.FindColumn(columnName).PropertyType) } };
            }
            else
            {
                var dict = new Dictionary<string, object>();
                var kvs = keyValues.Split(',');
                foreach (var kv in kvs)
                {
                    var pair = kv.Split('=');
                    var columnName = pair.First();
                    dict.Add(columnName, ParseValue(pair.Last(), _table.FindColumn(columnName).PropertyType));
                }
                return dict;
            }
        }

        private object ParseValue(string value, EdmPropertyType propertyType)
        {
            return value == "null" ? null
                : propertyType.Name == EdmType.Boolean.Name ? bool.Parse(value)
                : propertyType.Name == EdmType.Byte.Name ? byte.Parse(value)
                : propertyType.Name == EdmType.SByte.Name ? sbyte.Parse(value)
                : propertyType.Name == EdmType.DateTime.Name ? DateTime.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.DateTimeOffset.Name ? DateTimeOffset.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Int16.Name ? short.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Int32.Name ? int.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Int64.Name ? long.Parse(value.TrimEnd('L'), CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Single.Name ? float.Parse(value.TrimEnd('f'), CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Float.Name ? float.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Double.Name ? double.Parse(value, CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Decimal.Name ? decimal.Parse(value.TrimEnd('M', 'm'), CultureInfo.InvariantCulture)
                : propertyType.Name == EdmType.Guid.Name ? Guid.Parse(value)
{'fraction_non_alphanumeric': 0.1072341980270369, 'fraction_numerical': 0.0047497259773474606, 'mean_word_length': 2.1775972141613464, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '1068307', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1422, 'n_words': 260}