-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert structures and tables to AbapValue (#195)
convert structures and tables to AbapValues
- Loading branch information
Showing
10 changed files
with
265 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Abap/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dbosoft/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/YaNco.Core/TypeMapping/DictionaryFromAbapStructureValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using LanguageExt; | ||
|
||
namespace Dbosoft.YaNco.TypeMapping | ||
{ | ||
public class DictionaryFromAbapStructureValueConverter : IFromAbapValueConverter<IDictionary<string, AbapValue>> | ||
{ | ||
public Try<IDictionary<string, AbapValue>> ConvertTo(AbapValue abapValue) | ||
{ | ||
return Prelude.Try(() => | ||
{ | ||
if (!(abapValue is AbapStructureValues structure)) | ||
throw new InvalidCastException( | ||
$"cannot convert type of {abapValue.GetType()} to {nameof(AbapStructureValues)}"); | ||
return structure.Values; | ||
} | ||
|
||
); | ||
} | ||
|
||
public bool CanConvertTo(RfcType rfcType) | ||
{ | ||
|
||
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault | ||
switch (rfcType) | ||
{ | ||
case RfcType.STRUCTURE: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/YaNco.Core/TypeMapping/ListFromAbapTableValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using LanguageExt; | ||
|
||
namespace Dbosoft.YaNco.TypeMapping | ||
{ | ||
public class ListFromAbapTableValueConverter : IFromAbapValueConverter<IEnumerable<IDictionary<string, AbapValue>>> | ||
{ | ||
public Try<IEnumerable<IDictionary<string, AbapValue>>> ConvertTo(AbapValue abapValue) | ||
{ | ||
return Prelude.Try(() => | ||
{ | ||
if (!(abapValue is AbapTableValues table)) | ||
throw new InvalidCastException( | ||
$"cannot convert type of {abapValue.GetType()} to {nameof(AbapTableValues)}"); | ||
return table.Values; | ||
} | ||
|
||
); | ||
} | ||
|
||
public bool CanConvertTo(RfcType rfcType) | ||
{ | ||
|
||
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault | ||
switch (rfcType) | ||
{ | ||
case RfcType.TABLE: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Dbosoft.YaNco.TypeMapping | ||
{ | ||
public class AbapStructureValues : AbapValue | ||
{ | ||
public readonly IDictionary<string, AbapValue> Values; | ||
|
||
public AbapStructureValues(RfcFieldInfo fieldInfo, IDictionary<string, AbapValue> values) : | ||
base(fieldInfo) | ||
{ | ||
Values = values; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Dbosoft.YaNco.TypeMapping | ||
{ | ||
public class AbapTableValues : AbapValue | ||
{ | ||
public readonly IEnumerable<IDictionary<string, AbapValue>> Values; | ||
|
||
public AbapTableValues(RfcFieldInfo fieldInfo, IEnumerable<IDictionary<string, AbapValue>> values) : | ||
base(fieldInfo) | ||
{ | ||
Values = values; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Dbosoft.YaNco; | ||
using Dbosoft.YaNco.TypeMapping; | ||
|
||
namespace SAPSystemTests | ||
{ | ||
public class AbapValueJsonConverter : JsonConverter<AbapValue> | ||
{ | ||
public IFieldMapper FieldMapper; | ||
|
||
public AbapValueJsonConverter(IFieldMapper fieldMapper) | ||
{ | ||
FieldMapper = fieldMapper; | ||
} | ||
|
||
public override AbapValue Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options) => | ||
throw new NotImplementedException(); | ||
|
||
|
||
public override void Write( | ||
Utf8JsonWriter writer, | ||
AbapValue value, | ||
JsonSerializerOptions options) | ||
{ | ||
|
||
switch (value.FieldInfo.Type) | ||
{ | ||
case RfcType.NUM: | ||
case RfcType.INT8: | ||
case RfcType._UTCLONG: | ||
case RfcType.UTCSECOND: | ||
case RfcType.UTCMINUTE: | ||
case RfcType.DTDAY: | ||
case RfcType._DTWEEK: | ||
case RfcType.DTMONTH: | ||
case RfcType.TSECOND: | ||
case RfcType.TMINUTE: | ||
case RfcType.CDAY: | ||
case RfcType.INT: | ||
case RfcType.INT2: | ||
case RfcType.INT1: | ||
FieldMapper.FromAbapValue<long>(value) | ||
.Match(writer.WriteNumberValue | ||
, l => throw new Exception(l.Message)); | ||
break; | ||
case RfcType.DECF16: | ||
case RfcType.DECF34: | ||
FieldMapper.FromAbapValue<decimal>(value) | ||
.Match(writer.WriteNumberValue | ||
, l => throw new Exception(l.Message)); | ||
break; | ||
case RfcType.FLOAT: | ||
FieldMapper.FromAbapValue<float>(value) | ||
.Match(writer.WriteNumberValue | ||
, l => throw new Exception(l.Message)); | ||
break; | ||
case RfcType.XSTRING: | ||
case RfcType.BYTE: | ||
FieldMapper.FromAbapValue<byte[]>(value) | ||
.Match(b => writer.WriteBase64StringValue(new ReadOnlySpan<byte>(b)) | ||
, l => throw new Exception(l.Message)); | ||
break; | ||
|
||
case RfcType.TIME: | ||
case RfcType.DATE: | ||
case RfcType.CHAR: | ||
case RfcType.STRING: | ||
FieldMapper.FromAbapValue<string>(value) | ||
.Match(writer.WriteStringValue | ||
, l => throw new Exception(l.Message)); | ||
break; | ||
|
||
case RfcType.BCD: | ||
break; | ||
case RfcType.NULL: | ||
break; | ||
case RfcType.ABAPOBJECT: | ||
break; | ||
case RfcType.STRUCTURE: | ||
FieldMapper.FromAbapValue<IDictionary<string, AbapValue>>(value) | ||
.Match( | ||
d => JsonSerializer.Serialize(writer, d, options), | ||
l => throw new Exception(l.Message)); | ||
break; | ||
case RfcType.TABLE: | ||
FieldMapper.FromAbapValue<IEnumerable<IDictionary<string, AbapValue>>>(value) | ||
.Match( | ||
ed => JsonSerializer.Serialize(writer, ed, options), | ||
l => throw new Exception(l.Message)); | ||
break; | ||
case RfcType.XMLDATA: | ||
break; | ||
case RfcType.BOX: | ||
break; | ||
case RfcType.GENERIC_BOX: | ||
break; | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters