Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions src/Nest/Modules/Scripting/IStoredScript.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
using System.Collections.Generic;
using Elasticsearch.Net;
using Elasticsearch.Net;
using Newtonsoft.Json;

namespace Nest
{
/// <summary>
/// A Stored script
/// </summary>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeJsonConverter<StoredScript>))]
public interface IStoredScript
{
/// <summary>
/// The script language
/// </summary>
[JsonProperty("lang")]
string Lang { get; set; }

/// <summary>
/// The script source
/// </summary>
[JsonProperty("source")]
string Source { get; set; }
}

/// <inheritdoc />
public class StoredScript : IStoredScript
{
[JsonProperty("lang")]
string IStoredScript.Lang { get; set; }

[JsonProperty("source")]
string IStoredScript.Source { get; set; }

//used for deserialization
internal StoredScript() { }
internal StoredScript() {}

/// <summary>
/// Instantiates a new instance of <see cref="StoredScript"/>
/// </summary>
/// <param name="lang">Script language</param>
/// <param name="source">Script source</param>
protected StoredScript(string lang, string source)
{
((IStoredScript) this).Lang = lang;
((IStoredScript) this).Source = source;
IStoredScript self = this;
self.Lang = lang;
self.Source = source;
}
}

Expand All @@ -36,39 +53,28 @@ public class PainlessScript : StoredScript
private static readonly string Lang = ScriptLang.Painless.GetStringValue();
public PainlessScript(string source) : base(Lang, source) { }
}
public class GroovyScript : StoredScript
{
private static readonly string Lang = ScriptLang.Groovy.GetStringValue();
public GroovyScript(string source) : base(Lang, source) { }
}
public class JavaScriptScript : StoredScript
{
private static readonly string Lang = ScriptLang.JS.GetStringValue();
public JavaScriptScript(string source) : base(Lang, source) { }
}
public class PythonScript : StoredScript
{
private static readonly string Lang = ScriptLang.Python.GetStringValue();
public PythonScript(string source) : base(Lang, source) { }
}

public class LuceneExpressionScript : StoredScript
{
private static readonly string Lang = ScriptLang.Expression.GetStringValue();
public LuceneExpressionScript(string source) : base(Lang, source) { }
}

public class MustacheScript : StoredScript
{
private static readonly string Lang = ScriptLang.Mustache.GetStringValue();
public MustacheScript(string source) : base(Lang, source) { }
public MustacheScript(string source) : base(Lang, source) { }
}

public class StoredScriptDescriptor : DescriptorBase<StoredScriptDescriptor, IStoredScript>, IStoredScript
{
string IStoredScript.Lang { get; set; }
string IStoredScript.Source { get; set; }
string IStoredScript.Lang { get; set; }

public StoredScriptDescriptor Source(string source) => Assign(a => a.Source = source);

public StoredScriptDescriptor Lang(string lang) => Assign(a => a.Lang = lang);

public StoredScriptDescriptor Source(string source) => Assign(a => a.Source = source);
public StoredScriptDescriptor Lang(ScriptLang lang) => Assign(a => a.Lang = lang.GetStringValue());
}
}
14 changes: 11 additions & 3 deletions src/Nest/Modules/Scripting/PutScript/PutScriptRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ public partial class PutScriptDescriptor
public PutScriptDescriptor Script(Func<StoredScriptDescriptor, IStoredScript> selector) =>
Assign(a => a.Script = selector?.Invoke(new StoredScriptDescriptor()));

/// <summary>
/// A Painless language script
/// </summary>
public PutScriptDescriptor Painless(string source) => Assign(a => a.Script = new PainlessScript(source));
public PutScriptDescriptor Groovy(string source) => Assign(a => a.Script = new GroovyScript(source));
public PutScriptDescriptor JavaScript(string source) => Assign(a => a.Script = new JavaScriptScript(source));
public PutScriptDescriptor Python(string source) => Assign(a => a.Script = new PythonScript(source));

/// <summary>
/// A Lucene expression language script
/// </summary>
public PutScriptDescriptor LuceneExpression(string source) => Assign(a => a.Script = new LuceneExpressionScript(source));

/// <summary>
/// A Mustache template language script
/// </summary>
public PutScriptDescriptor Mustache(string source) => Assign(a => a.Script = new MustacheScript(source));
}
}
9 changes: 0 additions & 9 deletions src/Nest/Modules/Scripting/ScriptLang.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ public enum ScriptLang
[EnumMember(Value = "painless")]
Painless,

[EnumMember(Value = "groovy")]
Groovy,

[EnumMember(Value = "js")]
JS,

[EnumMember(Value = "python")]
Python,

[EnumMember(Value = "expression")]
Expression,

Expand Down
20 changes: 16 additions & 4 deletions src/Tests/Modules/Scripting/PutScript/PutScriptApiTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Framework;
using Tests.Framework.Integration;
Expand All @@ -9,13 +10,12 @@
namespace Tests.Modules.Scripting.PutScript
{
public class PutScriptApiTests
: ApiTestBase<ReadOnlyCluster, IPutScriptResponse, IPutScriptRequest, PutScriptDescriptor, PutScriptRequest>
: ApiIntegrationTestBase<ReadOnlyCluster, IPutScriptResponse, IPutScriptRequest, PutScriptDescriptor, PutScriptRequest>
{
public PutScriptApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private static readonly string _name = "scrpt1";


protected override LazyResponses ClientUsage() => Calls(
fluent: (client, f) => client.PutScript(_name, f),
fluentAsync: (client, f) => client.PutScriptAsync(_name, f),
Expand All @@ -25,12 +25,18 @@ protected override LazyResponses ClientUsage() => Calls(

protected override HttpMethod HttpMethod => HttpMethod.PUT;
protected override string UrlPath => $"/_scripts/{_name}";
protected override int ExpectStatusCode => 200;
protected override bool ExpectIsValid => true;

protected override bool SupportsDeserialization => false;

protected override object ExpectJson { get; } = new
protected override object ExpectJson => new
{
script = new { lang = "painless", source = "1+1" }
script = new
{
lang = "painless",
source = "1+1"
}
};

protected override PutScriptDescriptor NewDescriptor() => new PutScriptDescriptor(_name);
Expand All @@ -42,5 +48,11 @@ protected override LazyResponses ClientUsage() => Calls(
{
Script = new PainlessScript("1+1")
};

protected override void ExpectResponse(IPutScriptResponse response)
{
response.ShouldBeValid();
response.Acknowledged.Should().BeTrue();
}
}
}