-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thomas Farr <[email protected]>
- Loading branch information
Showing
6 changed files
with
193 additions
and
18 deletions.
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
46 changes: 46 additions & 0 deletions
46
java-codegen/src/main/java/org/opensearch/client/codegen/model/TaggedUnionShape.java
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,46 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.codegen.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TaggedUnionShape extends ObjectShape { | ||
private final List<Variant> variants = new ArrayList<>(); | ||
|
||
public TaggedUnionShape(Namespace parent, String className, String typedefName) { | ||
super(parent, className, typedefName); | ||
} | ||
|
||
public void addVariant(String name, Type type) { | ||
variants.add(new Variant(name, type)); | ||
} | ||
|
||
public List<Variant> getVariants() { | ||
return variants; | ||
} | ||
|
||
public static class Variant { | ||
private final String name; | ||
private final Type type; | ||
|
||
protected Variant(String name, Type type) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
} | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...src/main/resources/org/opensearch/client/codegen/templates/ObjectShape/Serialize.mustache
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
89 changes: 89 additions & 0 deletions
89
...egen/src/main/resources/org/opensearch/client/codegen/templates/TaggedUnionShape.mustache
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,89 @@ | ||
{{>ObjectShape/ClassDeclaration}} { | ||
public enum Kind { | ||
{{#variants}}{{name}}{{^-last}},{{/-last}}{{/variants}} | ||
} | ||
|
||
private final Kind _kind; | ||
private final Object _value; | ||
|
||
@Override | ||
public final Kind _kind() { | ||
return _kind; | ||
} | ||
|
||
@Override | ||
public final Object _value() { | ||
return _value; | ||
} | ||
|
||
private {{className}}(Kind kind, Object value) { | ||
this._kind = kind; | ||
this._value = value; | ||
} | ||
|
||
private {{className}}(Builder builder) { | ||
this._kind = {{TYPES.Client.Util.ApiTypeHelper}}.requireNonNull(builder._kind, builder, "<variant kind>"); | ||
this._value = {{TYPES.Client.Util.ApiTypeHelper}}.requireNonNull(builder._value, builder, "<variant value>"); | ||
} | ||
|
||
public static {{className}} of({{type.builderFnType}} fn) { | ||
return fn.apply(new Builder()).build(); | ||
} | ||
|
||
{{#variants}} | ||
/** | ||
* Is this variant instance of kind {@code {{name}}}? | ||
*/ | ||
public boolean is{{name}}() { | ||
return _kind == Kind.{{name}}; | ||
} | ||
|
||
/** | ||
* Get the {@code {{name}}} variant value. | ||
* | ||
* @throws IllegalStateException if the current variant is not the {@code {{name}}} kind. | ||
*/ | ||
public {{type}} {{name}}() { | ||
return {{TYPES.Client.Util.TaggedUnionUtils}}.get(this, Kind.{{name}}); | ||
} | ||
|
||
{{/variants}} | ||
|
||
@Override | ||
public void serialize({{TYPES.Jakarta.Json.Stream.JsonGenerator}} generator, {{TYPES.Client.Json.JsonpMapper}} mapper) { | ||
if (_value instanceof {{TYPES.Client.Json.JsonpSerializable}}) { | ||
(({{TYPES.Client.Json.JsonpSerializable}}) _value).serialize(generator, mapper); | ||
} | ||
} | ||
|
||
public static class Builder extends {{TYPES.Client.Util.ObjectBuilderBase}} implements {{TYPES.Client.Util.ObjectBuilder}}<{{className}}> { | ||
private Kind _kind; | ||
private Object _value; | ||
{{#variants}} | ||
public {{TYPES.Client.Util.ObjectBuilder}}<{{className}}> {{name}}({{type}} v) { | ||
this._kind = Kind.{{name}}; | ||
this._value = v; | ||
return this; | ||
} | ||
|
||
public {{TYPES.Client.Util.ObjectBuilder}}<{{className}}> {{name}}({{type.builderFnType}} fn) { | ||
return this.{{name}}(fn.apply(new {{type.builderType}}()).build()); | ||
} | ||
|
||
{{/variants}} | ||
@Override | ||
public {{className}} build() { | ||
_checkSingleUse(); | ||
return new {{className}}(this); | ||
} | ||
} | ||
|
||
private static {{TYPES.Client.Json.JsonpDeserializer}}<{{className}}> build{{className}}Deserializer() { | ||
return new {{TYPES.Client.Json.UnionDeserializer.builderType}}<{{className}}, Kind, Object>({{className}}::new, false) | ||
{{#variants}}.addMember(Kind.{{name}}, {{type}}._DESERIALIZER){{/variants}} | ||
.build(); | ||
} | ||
|
||
public static final {{TYPES.Client.Json.JsonpDeserializer}}<{{className}}> _DESERIALIZER = {{TYPES.Client.Json.JsonpDeserializer}}.lazy({{className}}::build{{className}}Deserializer); | ||
} |