Skip to content

[Backport 8.0] Improvements for open enumeration types #6698

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

Merged
merged 1 commit into from
Oct 3, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,16 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
}
}


internal sealed class EnumStructConverter<T> : JsonConverter<T>
internal sealed class EnumStructConverter<T> : JsonConverter<T> where T : new()
{
// TODO: Rename if not valid

public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();

var instance = (T)Activator.CreateInstance(
typeof(T),
BindingFlags.Instance | BindingFlags.NonPublic,
args: new object[] { value },
args: new object[] { value }, // TODO: Perf - Review use of ArrayPool
binder: null,
culture: null)!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,23 @@ public override void Write(Utf8JsonWriter writer, HighlighterTagsSchema value, J
}
}

public partial struct HighlighterType
[JsonConverter(typeof(EnumStructConverter<HighlighterType>))]
public readonly partial struct HighlighterType
{
public const string Unified = "unified";
public const string Plain = "plain";
public const string FastVector = "fvh";
public HighlighterType(string value) => Value = value;
public readonly string Value { get; }

public static HighlighterType Unified { get; } = new HighlighterType("unified");
public static HighlighterType Plain { get; } = new HighlighterType("plain");
public static HighlighterType FastVector { get; } = new HighlighterType("fvh");
public override string ToString() => Value ?? string.Empty;
public static implicit operator string(HighlighterType highlighterType) => highlighterType.Value;
public static implicit operator HighlighterType(string value) => new(value);
public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) => obj is HighlighterType other && this.Equals(other);
public bool Equals(HighlighterType other) => Value == other.Value;
public static bool operator ==(HighlighterType a, HighlighterType b) => a.Equals(b);
public static bool operator !=(HighlighterType a, HighlighterType b) => !(a == b);
}

[JsonConverter(typeof(IBDistributionConverter))]
Expand Down Expand Up @@ -1305,12 +1317,24 @@ public override void Write(Utf8JsonWriter writer, ScoreMode value, JsonSerialize
}
}

public partial struct ScriptLanguage
[JsonConverter(typeof(EnumStructConverter<ScriptLanguage>))]
public readonly partial struct ScriptLanguage
{
public const string Painless = "painless";
public const string Mustache = "mustache";
public const string Java = "java";
public const string Expression = "expression";
public ScriptLanguage(string value) => Value = value;
public readonly string Value { get; }

public static ScriptLanguage Painless { get; } = new ScriptLanguage("painless");
public static ScriptLanguage Mustache { get; } = new ScriptLanguage("mustache");
public static ScriptLanguage Java { get; } = new ScriptLanguage("java");
public static ScriptLanguage Expression { get; } = new ScriptLanguage("expression");
public override string ToString() => Value ?? string.Empty;
public static implicit operator string(ScriptLanguage scriptLanguage) => scriptLanguage.Value;
public static implicit operator ScriptLanguage(string value) => new(value);
public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) => obj is ScriptLanguage other && this.Equals(other);
public bool Equals(ScriptLanguage other) => Value == other.Value;
public static bool operator ==(ScriptLanguage a, ScriptLanguage b) => a.Equals(b);
public static bool operator !=(ScriptLanguage a, ScriptLanguage b) => !(a == b);
}

[JsonConverter(typeof(ScriptSortTypeConverter))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public sealed partial class Highlight

[JsonInclude]
[JsonPropertyName("type")]
public string? Type { get; set; }
public Elastic.Clients.Elasticsearch.HighlighterType? Type { get; set; }
}

public sealed partial class HighlightDescriptor<TDocument> : SerializableDescriptorBase<HighlightDescriptor<TDocument>>
Expand Down Expand Up @@ -174,7 +174,7 @@ public HighlightDescriptor() : base()

private Elastic.Clients.Elasticsearch.HighlighterTagsSchema? TagsSchemaValue { get; set; }

private string? TypeValue { get; set; }
private Elastic.Clients.Elasticsearch.HighlighterType? TypeValue { get; set; }

public HighlightDescriptor<TDocument> HighlightQuery(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? highlightQuery)
{
Expand Down Expand Up @@ -326,7 +326,7 @@ public HighlightDescriptor<TDocument> TagsSchema(Elastic.Clients.Elasticsearch.H
return Self;
}

public HighlightDescriptor<TDocument> Type(string? type)
public HighlightDescriptor<TDocument> Type(Elastic.Clients.Elasticsearch.HighlighterType? type)
{
TypeValue = type;
return Self;
Expand Down Expand Up @@ -538,7 +538,7 @@ public HighlightDescriptor() : base()

private Elastic.Clients.Elasticsearch.HighlighterTagsSchema? TagsSchemaValue { get; set; }

private string? TypeValue { get; set; }
private Elastic.Clients.Elasticsearch.HighlighterType? TypeValue { get; set; }

public HighlightDescriptor HighlightQuery(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? highlightQuery)
{
Expand Down Expand Up @@ -690,7 +690,7 @@ public HighlightDescriptor TagsSchema(Elastic.Clients.Elasticsearch.HighlighterT
return Self;
}

public HighlightDescriptor Type(string? type)
public HighlightDescriptor Type(Elastic.Clients.Elasticsearch.HighlighterType? type)
{
TypeValue = type;
return Self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public sealed partial class HighlightField

[JsonInclude]
[JsonPropertyName("type")]
public string? Type { get; set; }
public Elastic.Clients.Elasticsearch.HighlighterType? Type { get; set; }
}

public sealed partial class HighlightFieldDescriptor<TDocument> : SerializableDescriptorBase<HighlightFieldDescriptor<TDocument>>
Expand Down Expand Up @@ -174,7 +174,7 @@ public HighlightFieldDescriptor() : base()

private Elastic.Clients.Elasticsearch.HighlighterTagsSchema? TagsSchemaValue { get; set; }

private string? TypeValue { get; set; }
private Elastic.Clients.Elasticsearch.HighlighterType? TypeValue { get; set; }

public HighlightFieldDescriptor<TDocument> HighlightQuery(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? highlightQuery)
{
Expand Down Expand Up @@ -332,7 +332,7 @@ public HighlightFieldDescriptor<TDocument> TagsSchema(Elastic.Clients.Elasticsea
return Self;
}

public HighlightFieldDescriptor<TDocument> Type(string? type)
public HighlightFieldDescriptor<TDocument> Type(Elastic.Clients.Elasticsearch.HighlighterType? type)
{
TypeValue = type;
return Self;
Expand Down Expand Up @@ -548,7 +548,7 @@ public HighlightFieldDescriptor() : base()

private Elastic.Clients.Elasticsearch.HighlighterTagsSchema? TagsSchemaValue { get; set; }

private string? TypeValue { get; set; }
private Elastic.Clients.Elasticsearch.HighlighterType? TypeValue { get; set; }

public HighlightFieldDescriptor HighlightQuery(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? highlightQuery)
{
Expand Down Expand Up @@ -712,7 +712,7 @@ public HighlightFieldDescriptor TagsSchema(Elastic.Clients.Elasticsearch.Highlig
return Self;
}

public HighlightFieldDescriptor Type(string? type)
public HighlightFieldDescriptor Type(Elastic.Clients.Elasticsearch.HighlighterType? type)
{
TypeValue = type;
return Self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class InlineScript
{
[JsonInclude]
[JsonPropertyName("lang")]
public string? Language { get; set; }
public Elastic.Clients.Elasticsearch.ScriptLanguage? Language { get; set; }

[JsonInclude]
[JsonPropertyName("options")]
Expand All @@ -50,15 +50,15 @@ public InlineScriptDescriptor() : base()
{
}

private string? LanguageValue { get; set; }
private Elastic.Clients.Elasticsearch.ScriptLanguage? LanguageValue { get; set; }

private Dictionary<string, string>? OptionsValue { get; set; }

private Dictionary<string, object>? ParamsValue { get; set; }

private string SourceValue { get; set; }

public InlineScriptDescriptor Language(string? language)
public InlineScriptDescriptor Language(Elastic.Clients.Elasticsearch.ScriptLanguage? language)
{
LanguageValue = language;
return Self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public sealed partial class LanguageContext

[JsonInclude]
[JsonPropertyName("language")]
public string Language { get; init; }
public Elastic.Clients.Elasticsearch.ScriptLanguage Language { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class RoleTemplateInlineScript
{
[JsonInclude]
[JsonPropertyName("lang")]
public string? Lang { get; set; }
public Elastic.Clients.Elasticsearch.ScriptLanguage? Lang { get; set; }

[JsonInclude]
[JsonPropertyName("options")]
Expand All @@ -50,15 +50,15 @@ public RoleTemplateInlineScriptDescriptor() : base()
{
}

private string? LangValue { get; set; }
private Elastic.Clients.Elasticsearch.ScriptLanguage? LangValue { get; set; }

private Dictionary<string, string>? OptionsValue { get; set; }

private Dictionary<string, object>? ParamsValue { get; set; }

private Union<string, Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer> SourceValue { get; set; }

public RoleTemplateInlineScriptDescriptor Lang(string? lang)
public RoleTemplateInlineScriptDescriptor Lang(Elastic.Clients.Elasticsearch.ScriptLanguage? lang)
{
LangValue = lang;
return Self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class StoredScript
{
[JsonInclude]
[JsonPropertyName("lang")]
public string Language { get; set; }
public Elastic.Clients.Elasticsearch.ScriptLanguage Language { get; set; }

[JsonInclude]
[JsonPropertyName("options")]
Expand All @@ -46,13 +46,13 @@ public StoredScriptDescriptor() : base()
{
}

private string LanguageValue { get; set; }
private Elastic.Clients.Elasticsearch.ScriptLanguage LanguageValue { get; set; }

private Dictionary<string, string>? OptionsValue { get; set; }

private string SourceValue { get; set; }

public StoredScriptDescriptor Language(string language)
public StoredScriptDescriptor Language(Elastic.Clients.Elasticsearch.ScriptLanguage language)
{
LanguageValue = language;
return Self;
Expand Down