Beispiel #1
0
        public virtual AttributeValue ToAttributeValue(IPocoDynamo db, Type fieldType, string dbType, object value)
        {
            var attrVal = ToAttributeValueFn?.Invoke(fieldType, value);

            if (attrVal != null)
            {
                return(attrVal);
            }

            if (value == null)
            {
                return new AttributeValue {
                           NULL = true
                }
            }
            ;

            var valueConverter = GetValueConverter(fieldType);

            if (valueConverter != null)
            {
                return(valueConverter.ToAttributeValue(value, fieldType));
            }

            switch (dbType)
            {
            case DynamoType.String:
                var str = value as string
                          ?? ((value as DateTimeOffset?)?.ToString(CultureInfo.InvariantCulture) ?? value.ToString());
                return(str == ""     //DynamoDB throws on String.Empty
                        ? new AttributeValue {
                    NULL = true
                }
                        : new AttributeValue {
                    S = str
                });

            case DynamoType.Number:
                return(new AttributeValue
                {
                    N = value is string numStr
                            ? numStr
                            : DynamicNumber.GetNumber(value.GetType()).ToString(value)
                });
        public virtual AttributeValue ToAttributeValue(IPocoDynamo db, Type fieldType, string dbType, object value)
        {
            var attrVal = ToAttributeValueFn?.Invoke(fieldType, value);

            if (attrVal != null)
            {
                return(attrVal);
            }

            if (value == null)
            {
                return new AttributeValue {
                           NULL = true
                }
            }
            ;

            var valueConverter = GetValueConverter(fieldType);

            if (valueConverter != null)
            {
                return(valueConverter.ToAttributeValue(value, fieldType));
            }

            switch (dbType)
            {
            case DynamoType.String:
                var str = value as string
                          ?? (value is DateTimeOffset
                            ? ((DateTimeOffset)value).ToString(CultureInfo.InvariantCulture)
                            : value.ToString());
                return(str == ""     //DynamoDB throws on String.Empty
                        ? new AttributeValue {
                    NULL = true
                }
                        : new AttributeValue {
                    S = str
                });

            case DynamoType.Number:
                return(new AttributeValue {
                    N = value.ToString()
                });

            case DynamoType.Bool:
                return(new AttributeValue {
                    BOOL = (bool)value
                });

            case DynamoType.Binary:
                return(value is MemoryStream
                        ? new AttributeValue {
                    B = (MemoryStream)value
                }
                        : value is Stream
                            ? new AttributeValue {
                    B = new MemoryStream(((Stream)value).ReadFully())
                }
                            : new AttributeValue {
                    B = new MemoryStream((byte[])value)
                });

            case DynamoType.NumberSet:
                return(ToNumberSetAttributeValue(value));

            case DynamoType.StringSet:
                return(ToStringSetAttributeValue(value));

            case DynamoType.List:
                return(ToListAttributeValue(db, value));

            case DynamoType.Map:
                return(ToMapAttributeValue(db, value));

            default:
                return(new AttributeValue {
                    S = value.ToJsv()
                });
            }
        }