Example #1
0
        public Either <RfcErrorInfo, T> FromAbapValue <T>(AbapValue abapValue)
        {
            if (abapValue is T tv)
            {
                return(tv);
            }

            T value = default;

            foreach (var converter in _converterResolver.GetFromRfcConverters <T>(abapValue.FieldInfo.Type, abapValue.GetType()))
            {
                var result = converter.ConvertTo(abapValue)();
                if (result.IsFaulted)
                {
                    continue;
                }
                result.IfSucc(v => value = v);
                break;
            }

            if (value == null)
            {
                return(new RfcErrorInfo(RfcRc.RFC_CONVERSION_FAILURE, RfcErrorGroup.EXTERNAL_APPLICATION_FAILURE, "",
                                        $"Converting from abap type {abapValue.FieldInfo.Type} to type {typeof(T)} is not supported.",
                                        "", "E", "", "", "", "", ""));
            }

            return(Prelude.Right <RfcErrorInfo, T>(value));
        }
        public IEnumerable <IFromAbapValueConverter <T> > GetFromRfcConverters <T>(RfcType rfcType, Type abapValueType)
        {
            var targetType = typeof(T);
            var key        = $"{rfcType}_{targetType}";

            if (!_fromRfcConverters.ContainsKey(key))
            {
                var converters = _decoratedResolver.GetFromRfcConverters <T>(rfcType, abapValueType).ToArray();
                _fromRfcConverters.Add(key, converters.Length == 0 ? null : converters);
            }

            var entry = _fromRfcConverters[key];

            if (entry != null)
            {
                return((IEnumerable <IFromAbapValueConverter <T> >)entry);
            }
            return(new IFromAbapValueConverter <T> [0]);
        }