示例#1
0
        public Try <DateTime> ConvertTo(AbapValue abapValue)
        {
            return(Prelude.Try(() =>
            {
                if (!(abapValue is AbapStringValue stringValue))
                {
                    throw new ArgumentException($"DateTimeConverter cannot convert type {abapValue.GetType()}",
                                                nameof(abapValue));
                }

                DateTime dateTime;
                // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
                switch (stringValue.FieldInfo.Type)
                {
                case RfcType.DATE:
                    if (stringValue.Value == "00000000" || stringValue.Value == string.Empty)
                    {
                        dateTime = DateTime.MinValue;
                    }
                    else
                    {
                        dateTime = DateTime.ParseExact(stringValue.Value, "yyyyMMdd", CultureInfo.InvariantCulture);
                    }
                    break;

                case RfcType.TIME:
                    if (stringValue.Value == "000000" || stringValue.Value == string.Empty)
                    {
                        dateTime = DateTime.MinValue;
                    }
                    else
                    {
                        dateTime = default(DateTime).Add(
                            DateTime.ParseExact(stringValue.Value, "HHmmss", CultureInfo.InvariantCulture).TimeOfDay);
                    }
                    break;

                default:
                    throw new NotSupportedException(
                        $"It is not supported to convert RfcType {abapValue.FieldInfo.Type} to DateTime");
                }

                return dateTime;
            }));
        }
示例#2
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));
        }