Ejemplo n.º 1
0
 /// <summary>
 /// Returns true if values in the parameter required
 /// </summary>
 /// <param name="paramName">Parameter Name</param>
 /// <returns></returns>
 public bool IsRequiredParameter(string paramName)
 {
     if (RequiredFields.Contains(paramName))
     {
         return(true);
     }
     return(false);
 }
        // Adds pre-defined headings from RequiredFields
        public void AddHeadings(string headings)
        {
            var splitHeadings = headings.Split(',');
            var headers       = splitHeadings
                                .Select((name, index) => new { name, index })
                                .Where(h => RequiredFields.Contains(h.name))
                                .Select(h => new Field(h.name, h.index))
                                .ToList();

            Headers = new Head(headers);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Default key deserializer
        /// </summary>
        void IBigtableKeySerializer <object> .DeserializeKey(object instance, byte[] keyBytes, Encoding encoding)
        {
            if (instance == null)
            {
                throw new InvalidOperationException("Can not deserialize keys on a null reference of type " + _type.SimpleName());
            }

            // Locals
            //var bytes = new List<byte[]>();
            var separator = encoding.GetBytes(KeyFieldSeparator);

            var keys = new List <byte[]>();

            // This is not pretty.
            // TODO: Is there a better way?

            // Split key
            var prevIndex = 0;

            for (int index = 0; index < keyBytes.Length; index++)
            {
                var foundKey = true;
                for (int kIndex = 0; kIndex < separator.Length; kIndex++)
                {
                    if (keyBytes[index + kIndex] != separator[kIndex])
                    {
                        foundKey = false;
                        break;
                    }
                }
                if (foundKey || index == keyBytes.Length - 1)
                {
                    // TODO: Account for multibyte separators
                    var size = index == keyBytes.Length - 1 ? keyBytes.Length - prevIndex : index - prevIndex;
                    var key  = new byte[size]; // mmmm, byte sized

                    // A zero byte key is okay, and must be accounted
                    // in order for the key deserialer to find the right slots
                    if (size > 0)
                    {
                        Array.Copy(keyBytes, prevIndex, key, 0, size);
                    }
                    keys.Add(key);
                    index    += separator.Length;
                    prevIndex = index;
                }
            }

            // Ensure we have some key data
            if (!keys.Any())
            {
                throw new SerializationException("No keys found while deserializing " + _type.SimpleName());
            }

            // Populate the key fields on the POCO
            for (int index = 0; index < KeyFields.Length; index++)
            {
                // Localize
                var fieldName = KeyFields[index];
                var required  = RequiredFields.Contains(fieldName);

                // Are there more key fields than we have keys?
                if (index >= keys.Count)
                {
                    // No more key bytes available, was this required?
                    if (required)
                    {
                        throw new KeyNotFoundException(String.Format("Member {0} on type {1} is marked as required but is missing in the deserialization stream.", FieldNameLookup[fieldName], _type));
                    }

                    // Non-required field, continue to check the rest of the fields
                    continue;
                }

                // Localize this key's value
                var valueBytes = keys[index];
                //var value = Getters[field](instance);

                // Missing value?
                if (valueBytes == null)
                {
                    if (required)
                    {
                        throw new SerializationException(String.Format("Member {0} on type {1} is marked as required but was not specified", FieldNameLookup[fieldName], _type));
                    }

                    // Continue to next key field
                    continue;
                }

                // Get destination type
                var valueType = MemberTypes[fieldName];

                // Find a serializer for this type
                var serializer = GetSerializerForType(valueType);

                // Deserialize
                var value = serializer.DeserializeField(valueType, valueBytes, TableEncoding);

                // Store value
                Setters[fieldName](instance, value);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Default key serializer
        /// </summary>
        byte[] IBigtableKeySerializer <object> .SerializeKey(object instance, Encoding encoding)
        {
            // Contract
            if (instance == null)
            {
                throw new InvalidOperationException("Can not serialize keys on a null reference.");
            }

            // Locals
            //bool missingKey = false;
            var bytes     = new List <byte[]>();
            var separator = encoding.GetBytes(KeyFieldSeparator);

            // Iterate keys
            for (int index = 0; index < KeyFields.Length; index++)
            {
                // Localize
                var fieldName = KeyFields[index];
                var required  = RequiredFields.Contains(fieldName);
                var value     = Getters[fieldName](instance);

                //
                if (value == null)
                {
                    if (required)
                    {
                        throw new SerializationException(String.Format("The member {0} on type {1} is marked as required but was not specified", FieldNameLookup[fieldName], _type));
                    }
                    bytes.Add(separator);
                    //missingKey = true;
                    continue;
                }
                //if (missingKey)
                //{
                //    // Programmer exception
                //    var exception = new MissingFieldException(_type.Name, memberName);
                //    // User exception // TODO: I think I designed around this
                //    throw new SerializationException("The default serializer insists that the primary keys are filled in order.  Missing keys are okay as long as all subsequent keys are missing as well.", exception);
                //}
                var valueType  = value.GetType();
                var serializer = GetSerializerForType(valueType);

                if (IsBigBoxed[fieldName])
                {
                    if (!IsSpecified[fieldName](instance))
                    {
                        bytes.Add(new byte[0]);
                        continue;
                    }
                    //var fieldType = MemberTypes[memberName];
                    //var access = GetKeyAccess(fieldType);
                    //value = access.ValueGetter(value);
                }
                var serialized = serializer.SerializeField(valueType, value, TableEncoding);
                bytes.Add(serialized);
            }
            var bytesSize = bytes.Count - 1;
            var sepLength = (bytesSize) * separator.Length;
            var results   = new byte[sepLength + bytes.Sum(b => b.Length)];
            var ptr       = 0;

            for (int index = 0; index < bytes.Count; index++)
            {
                // Get field bytes
                var source = bytes[index];

                // Copy to result array in correct position
                Array.Copy(source, 0, results, ptr, source.Length);

                // Move ptr forward
                ptr += source.Length;
                if (index < bytesSize)
                {
                    // Copy in separator
                    Array.Copy(separator, 0, results, ptr, sepLength);

                    // Move ptr foward
                    ptr += sepLength;
                }
            }

            // Tada!
            return(results);
        }