Example #1
0
        private (int, Encoding)? GetLengthAttributeValues(MemberAttributeInfo fieldAttributes, ValueStorage storage)
        {
            var fixedLengthAttribute      = fieldAttributes?.FixedLengthAttribute;
            var variableLengthAttribute   = fieldAttributes?.VariableLengthAttribute;
            var calculatedLengthAttribute = fieldAttributes?.CalculatedLengthAttribute;

            Encoding stringEncoding;
            int      length;

            if (fixedLengthAttribute != null)
            {
                stringEncoding = Tools.RetrieveEncoding(fixedLengthAttribute.StringEncoding);
                length         = fixedLengthAttribute.Length;
            }
            else if (variableLengthAttribute != null)
            {
                stringEncoding = Tools.RetrieveEncoding(variableLengthAttribute.StringEncoding);
                length         = Convert.ToInt32(storage.Get(variableLengthAttribute.FieldName)) + variableLengthAttribute.Offset;
            }
            else if (calculatedLengthAttribute != null)
            {
                stringEncoding = Tools.RetrieveEncoding(calculatedLengthAttribute.StringEncoding);
                length         = calculatedLengthAttribute.CalculationAction(storage);
            }
            else
            {
                return(null);
            }

            return(length, stringEncoding);
        }
Example #2
0
        private bool ResolveCondition(ConditionAttribute condition, ValueStorage storage)
        {
            // If no condition is given, resolve it to true so the field is read
            if (condition == null)
            {
                return(true);
            }

            var value = storage.Get(condition.FieldName);

            switch (condition.Comparer)
            {
            case ConditionComparer.Equal:
                return(Convert.ToUInt64(value) == condition.Value);

            case ConditionComparer.Greater:
                return(Convert.ToUInt64(value) > condition.Value);

            case ConditionComparer.Smaller:
                return(Convert.ToUInt64(value) < condition.Value);

            case ConditionComparer.GEqual:
                return(Convert.ToUInt64(value) >= condition.Value);

            case ConditionComparer.SEqual:
                return(Convert.ToUInt64(value) <= condition.Value);

            default:
                throw new InvalidOperationException($"Unknown comparer {condition.Comparer}.");
            }
        }
Example #3
0
        private Type ChooseType(Type readType, MemberAttributeInfo fieldAttributes, ValueStorage storage)
        {
            var typeChoices = fieldAttributes.TypeChoiceAttributes.ToArray();

            if (readType != typeof(object) && typeChoices.Any(x => !readType.IsAssignableFrom(x.InjectionType)))
            {
                throw new InvalidOperationException($"Not all type choices are injectable to '{readType.Name}'.");
            }

            Type chosenType = null;

            foreach (var typeChoice in typeChoices)
            {
                if (!storage.Exists(typeChoice.FieldName))
                {
                    throw new InvalidOperationException($"Field '{typeChoice.FieldName}' could not be found.");
                }

                var value = storage.Get(typeChoice.FieldName);
                switch (typeChoice.Comparer)
                {
                case TypeChoiceComparer.Equal:
                    if (Convert.ToUInt64(value) == Convert.ToUInt64(typeChoice.Value))
                    {
                        chosenType = typeChoice.InjectionType;
                    }
                    break;

                case TypeChoiceComparer.Greater:
                    if (Convert.ToUInt64(value) > Convert.ToUInt64(typeChoice.Value))
                    {
                        chosenType = typeChoice.InjectionType;
                    }
                    break;

                case TypeChoiceComparer.Smaller:
                    if (Convert.ToUInt64(value) < Convert.ToUInt64(typeChoice.Value))
                    {
                        chosenType = typeChoice.InjectionType;
                    }
                    break;

                case TypeChoiceComparer.GEqual:
                    if (Convert.ToUInt64(value) >= Convert.ToUInt64(typeChoice.Value))
                    {
                        chosenType = typeChoice.InjectionType;
                    }
                    break;

                case TypeChoiceComparer.SEqual:
                    if (Convert.ToUInt64(value) <= Convert.ToUInt64(typeChoice.Value))
                    {
                        chosenType = typeChoice.InjectionType;
                    }
                    break;

                default:
                    throw new InvalidOperationException($"Unknown comparer {typeChoice.Comparer}.");
                }

                if (chosenType != null)
                {
                    break;
                }
            }

            if (chosenType == null)
            {
                throw new InvalidOperationException($"No choice matched the criteria for injection");
            }

            return(chosenType);
        }