Exemplo n.º 1
0
 public void ValidateDictionaryEntityGetTypes()
 {
     Assert.That(fullEntity.GetBinary("binary"), Is.InstanceOf(typeof(byte[])));
     Assert.That(fullEntity.GetBoolean("boolean"), Is.InstanceOf(typeof(bool?)));
     Assert.That(fullEntity.GetDateTime("datetime"), Is.InstanceOf(typeof(DateTime?)));
     Assert.That(fullEntity.GetDouble("double"), Is.InstanceOf(typeof(double?)));
     Assert.That(fullEntity.GetGuid("guid"), Is.InstanceOf(typeof(Guid)));
     Assert.That(fullEntity.GetInt32("int32"), Is.InstanceOf(typeof(int?)));
     Assert.That(fullEntity.GetInt64("int64"), Is.InstanceOf(typeof(long?)));
     Assert.That(fullEntity.GetString("string"), Is.InstanceOf(typeof(string)));
 }
Exemplo n.º 2
0
 public void ValidateDictionaryEntityGetTypeForNulledProperties()
 {
     Assert.That(emptyEntity.GetBinary(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetBoolean(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetDateTime(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetDouble(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetGuid(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetInt32(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetInt64(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetString(nulledPropertyKey), Is.Null);
 }
Exemplo n.º 3
0
    private object?GetFieldValue(EntityInfo info, string name, TableEntity entity)
    {
        var ef = info.properties[name].First();

        if (ef.kind == EntityPropertyKind.PartitionKey || ef.kind == EntityPropertyKind.RowKey)
        {
            if (ef.type == typeof(string))
            {
                return(entity.GetString(ef.kind.ToString()));
            }
            else if (ef.type == typeof(Guid))
            {
                return(Guid.Parse(entity.GetString(ef.kind.ToString())));
            }
            else if (ef.type == typeof(int))
            {
                return(int.Parse(entity.GetString(ef.kind.ToString())));
            }
            else
            {
                throw new Exception("invalid ");
            }
        }

        var fieldName = ef.columnName;
        var obj       = entity[fieldName];

        if (obj == null)
        {
            return(null);
        }
        var objType = obj.GetType();

        if (ef.type == typeof(string))
        {
            return(entity.GetString(fieldName));
        }
        else if (ef.type == typeof(bool) || ef.type == typeof(bool?))
        {
            return(entity.GetBoolean(fieldName));
        }
        else if (ef.type == typeof(DateTimeOffset) || ef.type == typeof(DateTimeOffset?))
        {
            return(entity.GetDateTimeOffset(fieldName));
        }
        else if (ef.type == typeof(DateTime) || ef.type == typeof(DateTime?))
        {
            return(entity.GetDateTime(fieldName));
        }
        else if (ef.type == typeof(double) || ef.type == typeof(double?))
        {
            return(entity.GetDouble(fieldName));
        }
        else if (ef.type == typeof(Guid) || ef.type == typeof(Guid?))
        {
            return((object?)Guid.Parse(entity.GetString(fieldName)));
        }
        else if (ef.type == typeof(int) || ef.type == typeof(short) || ef.type == typeof(int?) || ef.type == typeof(short?))
        {
            return(entity.GetInt32(fieldName));
        }
        else if (ef.type == typeof(long) || ef.type == typeof(long?))
        {
            return(entity.GetInt64(fieldName));
        }
        else
        {
            var outputType = ef.type;
            if (ef.discriminator != null)
            {
                var(attr, typeProvider) = ef.discriminator.Value;
                var v = GetFieldValue(info, attr.FieldName, entity) ?? throw new Exception($"No value for {attr.FieldName}");
                outputType = typeProvider.GetTypeInfo(v);
            }


            if (objType == typeof(string))
            {
                var value = entity.GetString(fieldName);
                if (value.StartsWith('[') || value.StartsWith('{') || value == "null")
                {
                    return(JsonSerializer.Deserialize(value, outputType, options: _options));
                }
                else
                {
                    return(JsonSerializer.Deserialize($"\"{value}\"", outputType, options: _options));
                }
            }
            else
            {
                var value = entity.GetString(fieldName);
                return(JsonSerializer.Deserialize(value, outputType, options: _options));
            }
        }
    }