Skip to content

Commit

Permalink
Config passing (#152)
Browse files Browse the repository at this point in the history
* Basic config passing as string --ECL

* MInor changes to options field --ECL

* Store buildOptions as object in MongoDB rather than string

* (Oops, incomplete commit)

* Fix without using Json.net

* No json.net

* Fixed usings

* Fixes as per PR review
  • Loading branch information
Enkidu93 authored Oct 6, 2023
1 parent 6922a65 commit 99875d1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/Serval.Client/Client.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4320,6 +4320,9 @@ public partial class TranslationBuild
[Newtonsoft.Json.JsonProperty("dateFinished", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset? DateFinished { get; set; } = default!;

[Newtonsoft.Json.JsonProperty("options", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string? Options { get; set; } = default!;

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
Expand Down Expand Up @@ -4364,6 +4367,9 @@ public partial class TranslationBuildConfig
[Newtonsoft.Json.JsonProperty("pretranslate", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Collections.Generic.IList<PretranslateCorpusConfig>? Pretranslate { get; set; } = default!;

[Newtonsoft.Json.JsonProperty("options", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string? Options { get; set; } = default!;

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
Expand Down
3 changes: 2 additions & 1 deletion src/Serval.Grpc/Protos/serval/translation/v1/engine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ message StartBuildRequest {
string engine_type = 1;
string engine_id = 2;
string build_id = 3;
repeated Corpus corpora = 4;
optional string options = 4;
repeated Corpus corpora = 5;
}

message CancelBuildRequest {
Expand Down
2 changes: 2 additions & 0 deletions src/Serval.Shared/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
global using Grpc.Core;
global using Grpc.Net.ClientFactory;
global using System.Text.Json;
global using System.Text.Json.Serialization;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Mvc;
Expand Down
19 changes: 19 additions & 0 deletions src/Serval.Shared/Utils/ObjectToInferredTypesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Serval.Shared.Utils;

public class ObjectToInferredTypesConverter : JsonConverter<object>
{
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.True => true,
JsonTokenType.False => false,
JsonTokenType.Number when reader.TryGetInt64(out long l) => l,
JsonTokenType.Number => reader.GetDouble(),
JsonTokenType.String when reader.TryGetDateTime(out DateTime datetime) => datetime,
JsonTokenType.String => reader.GetString()!,
_ => JsonDocument.ParseValue(ref reader).RootElement.Clone()
};

public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options) =>
JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options);
}
2 changes: 2 additions & 0 deletions src/Serval.Translation/Contracts/TranslationBuildConfigDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public class TranslationBuildConfigDto
{
public string? Name { get; set; }
public IList<PretranslateCorpusConfigDto>? Pretranslate { get; set; }

public string? Options { get; set; }
}
1 change: 1 addition & 0 deletions src/Serval.Translation/Contracts/TranslationBuildDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public class TranslationBuildDto
/// </summary>
public JobState State { get; set; }
public DateTime? DateFinished { get; set; }
public string? Options { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,10 @@ CancellationToken cancellationToken
{
return BadRequest(ioe.Message);
}
catch (ArgumentException ae)
{
return BadRequest(ae.Message);
}
if (!await _engineService.StartBuildAsync(build, cancellationToken))
return NotFound();

Expand Down Expand Up @@ -976,6 +980,19 @@ private static Build Map(Engine engine, TranslationBuildConfigDto source)
}
build.Pretranslate = pretranslateCorpora;
}
try
{
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
build.Options = JsonSerializer.Deserialize<IDictionary<string, object>>(
source.Options ?? "{}",
jsonSerializerOptions
);
}
catch (Exception e)
{
throw new ArgumentException($"Unable to parse field 'options' : {e.Message}");
}
return build;
}

Expand Down Expand Up @@ -1014,7 +1031,8 @@ private TranslationBuildDto Map(Build source)
PercentCompleted = source.PercentCompleted,
Message = source.Message,
State = source.State,
DateFinished = source.DateFinished
DateFinished = source.DateFinished,
Options = JsonSerializer.Serialize(source.Options)
};
}

Expand Down
1 change: 1 addition & 0 deletions src/Serval.Translation/Models/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class Build : IEntity
public string? Message { get; set; }
public JobState State { get; set; } = JobState.Pending;
public DateTime? DateFinished { get; set; }
public IDictionary<string, object>? Options { get; set; }
}
1 change: 1 addition & 0 deletions src/Serval.Translation/Services/EngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public async Task<bool> StartBuildAsync(Build build, CancellationToken cancellat
EngineType = engine.Type,
EngineId = engine.Id,
BuildId = build.Id,
Options = JsonSerializer.Serialize(build.Options),
Corpora =
{
engine.Corpora.Select(c =>
Expand Down
7 changes: 4 additions & 3 deletions src/Serval.Translation/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
global using System.Diagnostics.CodeAnalysis;
global using System.Linq.Expressions;
global using Asp.Versioning;
global using Asp.Versioning;
global using Grpc.Core;
global using Grpc.Net.ClientFactory;
global using MassTransit;
Expand All @@ -23,3 +21,6 @@
global using Serval.Translation.Models;
global using Serval.Translation.Services;
global using SIL.DataAccess;
global using System.Diagnostics.CodeAnalysis;
global using System.Linq.Expressions;
global using System.Text.Json;

0 comments on commit 99875d1

Please sign in to comment.