-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Function Result #55
Open
campersau
wants to merge
1
commit into
huysentruitw:develop
Choose a base branch
from
campersau:dynamicrfc
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,97 @@ | ||
using System; | ||
using SapNwRfc.Internal.Fields; | ||
using SapNwRfc.Internal.Interop; | ||
|
||
namespace SapNwRfc.Internal.Dynamic | ||
{ | ||
internal static class DynamicRfc | ||
{ | ||
internal static bool TryGetRfcValue(RfcInterop interop, IntPtr dataHandle, string name, SapRfcType type, Func<ISapTypeMetadata> typeMetadata, Type returnType, out object result) | ||
{ | ||
result = GetRfcValue(interop, dataHandle, name, type, typeMetadata); | ||
|
||
if (returnType == typeof(object)) | ||
{ | ||
return true; | ||
} | ||
|
||
try | ||
{ | ||
result = Convert.ChangeType(result, returnType); | ||
return true; | ||
} | ||
catch { } | ||
|
||
result = null; | ||
return false; | ||
} | ||
|
||
private static object GetRfcValue(RfcInterop interop, IntPtr dataHandle, string name, SapRfcType type, Func<ISapTypeMetadata> typeMetadata) | ||
{ | ||
switch (type) | ||
{ | ||
case SapRfcType.RFCTYPE_CHAR: | ||
case SapRfcType.RFCTYPE_NUM: | ||
case SapRfcType.RFCTYPE_BCD: | ||
case SapRfcType.RFCTYPE_STRING: | ||
return StringField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_INT: | ||
case SapRfcType.RFCTYPE_INT1: | ||
case SapRfcType.RFCTYPE_INT2: | ||
return IntField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_INT8: | ||
return LongField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_FLOAT: | ||
return DoubleField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_DECF16: | ||
case SapRfcType.RFCTYPE_DECF34: | ||
return DecimalField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_DATE: | ||
return DateField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_TIME: | ||
return TimeField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_BYTE: | ||
case SapRfcType.RFCTYPE_XSTRING: | ||
return BytesField.Extract(interop, dataHandle, name, bufferLength: 0).Value; | ||
|
||
case SapRfcType.RFCTYPE_TABLE: | ||
{ | ||
RfcResultCode resultCode = interop.GetTable( | ||
dataHandle: dataHandle, | ||
name: name, | ||
tableHandle: out IntPtr tableHandle, | ||
errorInfo: out RfcErrorInfo errorInfo); | ||
|
||
resultCode.ThrowOnError(errorInfo); | ||
|
||
return new DynamicRfcTable(interop, tableHandle, typeMetadata()); | ||
} | ||
|
||
case SapRfcType.RFCTYPE_STRUCTURE: | ||
{ | ||
RfcResultCode resultCode = interop.GetStructure( | ||
dataHandle: dataHandle, | ||
name: name, | ||
structHandle: out IntPtr structHandle, | ||
errorInfo: out RfcErrorInfo errorInfo); | ||
|
||
resultCode.ThrowOnError(errorInfo); | ||
|
||
return new DynamicRfcStructure(interop, structHandle, typeMetadata()); | ||
} | ||
|
||
case SapRfcType.RFCTYPE_NULL: | ||
return null; | ||
} | ||
|
||
throw new NotSupportedException($"Parameter type {type} is not supported"); | ||
} | ||
} | ||
} |
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,158 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using SapNwRfc.Internal.Interop; | ||
|
||
namespace SapNwRfc.Internal.Dynamic | ||
{ | ||
internal sealed class DynamicRfcFunction : DynamicObject, IReadOnlyDictionary<string, object> | ||
{ | ||
private readonly RfcInterop _interop; | ||
private readonly IntPtr _functionHandle; | ||
private readonly ISapFunctionMetadata _functionMetadata; | ||
|
||
internal DynamicRfcFunction(RfcInterop interop, IntPtr functionHandle, ISapFunctionMetadata functionMetadata) | ||
{ | ||
_interop = interop; | ||
_functionHandle = functionHandle; | ||
_functionMetadata = functionMetadata; | ||
} | ||
|
||
private bool TryGetByName(string name, Type returnType, out object result) | ||
{ | ||
if (_functionMetadata.Parameters.TryGetValue(name, out ISapParameterMetadata parameter)) | ||
{ | ||
if (DynamicRfc.TryGetRfcValue(_interop, _functionHandle, parameter.Name, parameter.Type, parameter.GetTypeMetadata, returnType, out result)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
|
||
private bool TryGetByIndex(int index, Type returnType, out object result) | ||
{ | ||
if (index >= 0 && index < _functionMetadata.Parameters.Count) | ||
{ | ||
ISapParameterMetadata parameter = _functionMetadata.Parameters[index]; | ||
|
||
if (DynamicRfc.TryGetRfcValue(_interop, _functionHandle, parameter.Name, parameter.Type, parameter.GetTypeMetadata, returnType, out result)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
|
||
public override IEnumerable<string> GetDynamicMemberNames() | ||
{ | ||
return Keys; | ||
} | ||
|
||
public override bool TryGetMember(GetMemberBinder binder, out object result) | ||
{ | ||
return TryGetByName(binder.Name, binder.ReturnType, out result); | ||
} | ||
|
||
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) | ||
{ | ||
if (indexes.Length != 1) | ||
throw new ArgumentOutOfRangeException(nameof(indexes)); | ||
|
||
var index = indexes[0]; | ||
|
||
if (index is string name) | ||
return TryGetByName(name, binder.ReturnType, out result); | ||
|
||
if (index is int i) | ||
return TryGetByIndex(i, binder.ReturnType, out result); | ||
|
||
if (index is uint u) | ||
return TryGetByIndex((int)u, binder.ReturnType, out result); | ||
|
||
throw new ArgumentException(nameof(indexes)); | ||
} | ||
|
||
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | ||
{ | ||
if (binder.Name == "GetMetadata" && binder.CallInfo.ArgumentCount == 0) | ||
{ | ||
result = _functionMetadata; | ||
return true; | ||
} | ||
|
||
return base.TryInvokeMember(binder, args, out result); | ||
} | ||
|
||
public IEnumerable<string> Keys | ||
{ | ||
get | ||
{ | ||
foreach (var parameter in _functionMetadata.Parameters) | ||
{ | ||
yield return parameter.Name; | ||
} | ||
} | ||
} | ||
|
||
public IEnumerable<object> Values | ||
{ | ||
get | ||
{ | ||
foreach (var parameter in _functionMetadata.Parameters) | ||
{ | ||
if (TryGetByName(parameter.Name, typeof(object), out object result)) | ||
{ | ||
yield return result; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public int Count => _functionMetadata.Parameters.Count; | ||
|
||
public object this[string key] | ||
{ | ||
get | ||
{ | ||
if (TryGetByName(key, typeof(object), out var result)) | ||
{ | ||
return result; | ||
} | ||
|
||
throw new KeyNotFoundException(); | ||
} | ||
} | ||
|
||
public bool ContainsKey(string key) | ||
{ | ||
return _functionMetadata.Parameters.TryGetValue(key, out _); | ||
} | ||
|
||
public bool TryGetValue(string key, out object value) | ||
{ | ||
return TryGetByName(key, typeof(object), out value); | ||
} | ||
|
||
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() | ||
{ | ||
foreach (var parameter in _functionMetadata.Parameters) | ||
{ | ||
if (TryGetByName(parameter.Name, typeof(object), out object result)) | ||
{ | ||
yield return new KeyValuePair<string, object>(parameter.Name, result); | ||
} | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this code
GetMetadata
can be called on the results to get the current metadata. Not sure if we should expose it like this or just hide it.