Skip to content

Commit

Permalink
RavenDB-21956 Address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
djordjedjukic committed May 20, 2024
1 parent 5e4f43c commit b7fc25d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Jint;
using Jint.Native;
using Jint.Native.Object;
using Jint.Runtime.Interop;
using Jint.Runtime.Interop;
using Raven.Client.Documents.Operations.ETL;
using Raven.Client.Documents.Operations.ETL.Queue;
using Raven.Server.Documents.Patch;
Expand All @@ -23,7 +20,7 @@ protected override void LoadToFunction(string queueName, ScriptRunnerResult docu
LoadToFunction(queueName, document, null);
}

private void LoadToFunction(string queueName, ScriptRunnerResult document, CloudEventAttributes attributes)
protected override void LoadToFunction(string queueName, ScriptRunnerResult document, CloudEventAttributes attributes)
{
if (queueName == null)
ThrowLoadParameterIsMandatory(nameof(queueName));
Expand Down Expand Up @@ -51,56 +48,4 @@ public override void Initialize(bool debugMode)
(self, args) => LoadToFunctionTranslatorWithAttributes(queueName, args)));
}
}

private JsValue LoadToFunctionTranslatorWithAttributes(JsValue self, JsValue[] args)
{
var methodSignature = "loadTo(name, obj, attributes)";

if (args.Length != 2 && args.Length != 3)
ThrowInvalidScriptMethodCall($"{methodSignature} must be called with 2 or 3 parameters");

if (args[0].IsString() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} first argument must be a string");

if (args[1].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} second argument must be an object");

if (args.Length == 3 && args[2].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} third argument must be an object");

return LoadToFunctionTranslatorWithAttributesInternal(args[0].AsString(), args[1].AsObject(),
args.Length == 3 ? args[2].AsObject() : null);
}

private JsValue LoadToFunctionTranslatorWithAttributes(string name, JsValue[] args)
{
var methodSignature = $"loadTo{name}(obj, attributes)";

if (args.Length != 1 && args.Length != 2)
ThrowInvalidScriptMethodCall($"{methodSignature} must be called with with 1 or 2 parameters");

if (args[0].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} argument 'obj' must be an object");

if (args.Length == 2 && args[1].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} argument 'attributes' must be an object");

return LoadToFunctionTranslatorWithAttributesInternal(name, args[0].AsObject(),
args.Length == 2 ? args[1].AsObject() : null);
}

private JsValue LoadToFunctionTranslatorWithAttributesInternal(string name, ObjectInstance obj,
ObjectInstance attributes)
{
var result = new ScriptRunnerResult(DocumentScript, obj);

CloudEventAttributes cloudEventAttributes = null;

if (attributes != null)
cloudEventAttributes = GetCloudEventAttributes(attributes);

LoadToFunction(name, result, cloudEventAttributes);

return result.Instance;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Jint;
using Jint.Native;
using Jint.Native.Object;
using Jint.Runtime.Interop;
using Jint.Runtime.Interop;
using Raven.Client.Documents.Operations.ETL;
using Raven.Client.Documents.Operations.ETL.Queue;
using Raven.Server.Documents.Patch;
Expand All @@ -21,7 +18,7 @@ protected override void LoadToFunction(string topicName, ScriptRunnerResult docu
LoadToFunction(topicName, document, null);
}

private void LoadToFunction(string topicName, ScriptRunnerResult document, CloudEventAttributes attributes)
protected override void LoadToFunction(string topicName, ScriptRunnerResult document, CloudEventAttributes attributes)
{
if (topicName == null)
ThrowLoadParameterIsMandatory(nameof(topicName));
Expand All @@ -47,53 +44,4 @@ public override void Initialize(bool debugMode)
(self, args) => LoadToFunctionTranslatorWithAttributes(queueName, args)));
}
}

private JsValue LoadToFunctionTranslatorWithAttributes(JsValue self, JsValue[] args)
{
var methodSignature = "loadTo(name, obj, attributes)";

if (args.Length != 2 && args.Length != 3)
ThrowInvalidScriptMethodCall($"{methodSignature} must be called with 2 or 3 parameters");

if (args[0].IsString() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} first argument must be a string");

if (args[1].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} second argument must be an object");

if (args.Length == 3 && args[2].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} third argument must be an object");

return LoadToFunctionTranslatorWithAttributesInternal(args[0].AsString(), args[1].AsObject(), args.Length == 3 ? args[2].AsObject() : null);
}

private JsValue LoadToFunctionTranslatorWithAttributes(string name, JsValue[] args)
{
var methodSignature = $"loadTo{name}(obj, attributes)";

if (args.Length != 1 && args.Length != 2)
ThrowInvalidScriptMethodCall($"{methodSignature} must be called with with 1 or 2 parameters");

if (args[0].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} argument 'obj' must be an object");

if (args.Length == 2 && args[1].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} argument 'attributes' must be an object");

return LoadToFunctionTranslatorWithAttributesInternal(name, args[0].AsObject(), args.Length == 2 ? args[1].AsObject() : null);
}

private JsValue LoadToFunctionTranslatorWithAttributesInternal(string name, ObjectInstance obj, ObjectInstance attributes)
{
var result = new ScriptRunnerResult(DocumentScript, obj);

CloudEventAttributes cloudEventAttributes = null;

if (attributes != null)
cloudEventAttributes = GetCloudEventAttributes(attributes);

LoadToFunction(name, result, cloudEventAttributes);

return result.Instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,57 @@ bool TryGetDateAttributeValue(PropertyDescriptor attributeValue, out DateTimeOff
return false;
}
}

protected abstract void LoadToFunction(string name, ScriptRunnerResult result, CloudEventAttributes cloudEventAttributes);

protected JsValue LoadToFunctionTranslatorWithAttributesInternal(string name, ObjectInstance obj, ObjectInstance attributes)
{
var result = new ScriptRunnerResult(DocumentScript, obj);

CloudEventAttributes cloudEventAttributes = null;

if (attributes != null)
cloudEventAttributes = GetCloudEventAttributes(attributes);

LoadToFunction(name, result, cloudEventAttributes);

return result.Instance;
}

protected JsValue LoadToFunctionTranslatorWithAttributes(JsValue self, JsValue[] args)
{
var methodSignature = "loadTo(name, obj, attributes)";

if (args.Length != 2 && args.Length != 3)
ThrowInvalidScriptMethodCall($"{methodSignature} must be called with 2 or 3 parameters");

if (args[0].IsString() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} first argument must be a string");

if (args[1].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} second argument must be an object");

if (args.Length == 3 && args[2].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} third argument must be an object");

return LoadToFunctionTranslatorWithAttributesInternal(args[0].AsString(), args[1].AsObject(),
args.Length == 3 ? args[2].AsObject() : null);
}

protected JsValue LoadToFunctionTranslatorWithAttributes(string name, JsValue[] args)
{
var methodSignature = $"loadTo{name}(obj, attributes)";

if (args.Length != 1 && args.Length != 2)
ThrowInvalidScriptMethodCall($"{methodSignature} must be called with with 1 or 2 parameters");

if (args[0].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} argument 'obj' must be an object");

if (args.Length == 2 && args[1].IsObject() == false)
ThrowInvalidScriptMethodCall($"{methodSignature} argument 'attributes' must be an object");

return LoadToFunctionTranslatorWithAttributesInternal(name, args[0].AsObject(),
args.Length == 2 ? args[1].AsObject() : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,10 @@ private JsValue LoadToFunctionTranslatorWithAttributesInternal(string name, Obje

return result.Instance;
}

protected override void LoadToFunction(string name, ScriptRunnerResult result, CloudEventAttributes cloudEventAttributes)
{
// not needed for RabbitMq
throw new System.NotImplementedException();
}
}

0 comments on commit b7fc25d

Please sign in to comment.