use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testBasics.
public void testBasics() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"test_number\" : 2,\n" + " \"test_array\": [1,2,3,4]\n" + "}");
class TestStruct {
public String test;
int testNumber;
List<Integer> ints = new ArrayList<>();
public void setTestNumber(int testNumber) {
this.testNumber = testNumber;
}
public void setInts(List<Integer> ints) {
this.ints = ints;
}
}
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
TestStruct s = new TestStruct();
objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
objectParser.declareInt(TestStruct::setTestNumber, new ParseField("test_number"));
objectParser.declareIntArray(TestStruct::setInts, new ParseField("test_array"));
objectParser.parse(parser, s, null);
assertEquals(s.test, "foo");
assertEquals(s.testNumber, 2);
assertEquals(s.ints, Arrays.asList(1, 2, 3, 4));
assertEquals(objectParser.toString(), "ObjectParser{name='foo', fields=[" + "FieldParser{preferred_name=test, supportedTokens=[VALUE_STRING], type=STRING}, " + "FieldParser{preferred_name=test_array, supportedTokens=[START_ARRAY, VALUE_STRING, VALUE_NUMBER], type=INT_ARRAY}, " + "FieldParser{preferred_name=test_number, supportedTokens=[VALUE_STRING, VALUE_NUMBER], type=INT}]}");
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testUseClassicPullParsingSubParser.
/**
* This test ensures we can use a classic pull-parsing parser
* together with the object parser
*/
public void testUseClassicPullParsingSubParser() throws IOException {
class ClassicParser {
URI parseURI(XContentParser parser) throws IOException {
String fieldName = null;
String host = "";
int port = 0;
XContentParser.Token token;
while ((token = parser.currentToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
if (fieldName.equals("host")) {
host = parser.text();
} else {
throw new IllegalStateException("boom");
}
} else if (token == XContentParser.Token.VALUE_NUMBER) {
if (fieldName.equals("port")) {
port = parser.intValue();
} else {
throw new IllegalStateException("boom");
}
}
parser.nextToken();
}
return URI.create(host + ":" + port);
}
}
class Foo {
public String name;
public URI uri;
public void setName(String name) {
this.name = name;
}
public void setURI(URI uri) {
this.uri = uri;
}
}
class CustomParseContext {
public final ClassicParser parser;
CustomParseContext(ClassicParser parser) {
this.parser = parser;
}
public URI parseURI(XContentParser parser) {
try {
return this.parser.parseURI(parser);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"url\" : { \"host\": \"http://foobar\", \"port\" : 80}, \"name\" : \"foobarbaz\"}");
ObjectParser<Foo, CustomParseContext> objectParser = new ObjectParser<>("foo");
objectParser.declareString(Foo::setName, new ParseField("name"));
objectParser.declareObjectOrDefault(Foo::setURI, (p, s) -> s.parseURI(p), () -> null, new ParseField("url"));
Foo s = objectParser.parse(parser, new Foo(), new CustomParseContext(new ClassicParser()));
assertEquals(s.uri.getHost(), "foobar");
assertEquals(s.uri.getPort(), 80);
assertEquals(s.name, "foobarbaz");
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ObjectParserTests method testAllVariants.
public void testAllVariants() throws IOException {
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
builder.startObject();
builder.field("int_field", randomBoolean() ? "1" : 1);
if (randomBoolean()) {
builder.array("int_array_field", randomBoolean() ? "1" : 1);
} else {
builder.field("int_array_field", randomBoolean() ? "1" : 1);
}
builder.field("double_field", randomBoolean() ? "2.1" : 2.1d);
if (randomBoolean()) {
builder.array("double_array_field", randomBoolean() ? "2.1" : 2.1d);
} else {
builder.field("double_array_field", randomBoolean() ? "2.1" : 2.1d);
}
builder.field("float_field", randomBoolean() ? "3.1" : 3.1f);
if (randomBoolean()) {
builder.array("float_array_field", randomBoolean() ? "3.1" : 3.1);
} else {
builder.field("float_array_field", randomBoolean() ? "3.1" : 3.1);
}
builder.field("long_field", randomBoolean() ? "4" : 4);
if (randomBoolean()) {
builder.array("long_array_field", randomBoolean() ? "4" : 4);
} else {
builder.field("long_array_field", randomBoolean() ? "4" : 4);
}
builder.field("string_field", "5");
if (randomBoolean()) {
builder.array("string_array_field", "5");
} else {
builder.field("string_array_field", "5");
}
boolean nullValue = randomBoolean();
builder.field("boolean_field", nullValue);
builder.field("string_or_null", nullValue ? null : "5");
builder.endObject();
XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string());
class TestStruct {
int int_field;
long long_field;
float float_field;
double double_field;
String string_field;
List<Integer> int_array_field;
List<Long> long_array_field;
List<Float> float_array_field;
List<Double> double_array_field;
List<String> string_array_field;
boolean null_value;
String string_or_null = "adsfsa";
public void setInt_field(int int_field) {
this.int_field = int_field;
}
public void setLong_field(long long_field) {
this.long_field = long_field;
}
public void setFloat_field(float float_field) {
this.float_field = float_field;
}
public void setDouble_field(double double_field) {
this.double_field = double_field;
}
public void setString_field(String string_field) {
this.string_field = string_field;
}
public void setInt_array_field(List<Integer> int_array_field) {
this.int_array_field = int_array_field;
}
public void setLong_array_field(List<Long> long_array_field) {
this.long_array_field = long_array_field;
}
public void setFloat_array_field(List<Float> float_array_field) {
this.float_array_field = float_array_field;
}
public void setDouble_array_field(List<Double> double_array_field) {
this.double_array_field = double_array_field;
}
public void setString_array_field(List<String> string_array_field) {
this.string_array_field = string_array_field;
}
public void setNull_value(boolean null_value) {
this.null_value = null_value;
}
public void setString_or_null(String string_or_null) {
this.string_or_null = string_or_null;
}
}
ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
objectParser.declareInt(TestStruct::setInt_field, new ParseField("int_field"));
objectParser.declareIntArray(TestStruct::setInt_array_field, new ParseField("int_array_field"));
objectParser.declareLong(TestStruct::setLong_field, new ParseField("long_field"));
objectParser.declareLongArray(TestStruct::setLong_array_field, new ParseField("long_array_field"));
objectParser.declareDouble(TestStruct::setDouble_field, new ParseField("double_field"));
objectParser.declareDoubleArray(TestStruct::setDouble_array_field, new ParseField("double_array_field"));
objectParser.declareFloat(TestStruct::setFloat_field, new ParseField("float_field"));
objectParser.declareFloatArray(TestStruct::setFloat_array_field, new ParseField("float_array_field"));
objectParser.declareString(TestStruct::setString_field, new ParseField("string_field"));
objectParser.declareStringArray(TestStruct::setString_array_field, new ParseField("string_array_field"));
objectParser.declareStringOrNull(TestStruct::setString_or_null, new ParseField("string_or_null"));
objectParser.declareBoolean(TestStruct::setNull_value, new ParseField("boolean_field"));
TestStruct parse = objectParser.parse(parser, new TestStruct(), null);
assertArrayEquals(parse.double_array_field.toArray(), Arrays.asList(2.1d).toArray());
assertEquals(parse.double_field, 2.1d, 0.0d);
assertArrayEquals(parse.long_array_field.toArray(), Arrays.asList(4L).toArray());
assertEquals(parse.long_field, 4L);
assertArrayEquals(parse.string_array_field.toArray(), Arrays.asList("5").toArray());
assertEquals(parse.string_field, "5");
assertArrayEquals(parse.int_array_field.toArray(), Arrays.asList(1).toArray());
assertEquals(parse.int_field, 1);
assertArrayEquals(parse.float_array_field.toArray(), Arrays.asList(3.1f).toArray());
assertEquals(parse.float_field, 3.1f, 0.0f);
assertEquals(nullValue, parse.null_value);
if (nullValue) {
assertNull(parse.string_or_null);
} else {
assertEquals(parse.string_field, "5");
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ConstructingObjectParserTests method testCalledOneTime.
/**
* Tests the non-constructor fields are only set on time.
*/
public void testCalledOneTime() throws IOException {
boolean ctorArgOptional = randomBoolean();
class CalledOneTime {
CalledOneTime(String yeah) {
Matcher<String> yeahMatcher = equalTo("!");
if (ctorArgOptional) {
// either(yeahMatcher).or(nullValue) is broken by https://github.com/hamcrest/JavaHamcrest/issues/49
yeahMatcher = anyOf(yeahMatcher, nullValue());
}
assertThat(yeah, yeahMatcher);
}
boolean fooSet = false;
void setFoo(String foo) {
assertFalse(fooSet);
fooSet = true;
}
}
ConstructingObjectParser<CalledOneTime, Void> parser = new ConstructingObjectParser<>("one_time_test", (a) -> new CalledOneTime((String) a[0]));
parser.declareString(CalledOneTime::setFoo, new ParseField("foo"));
parser.declareString(ctorArgOptional ? optionalConstructorArg() : constructorArg(), new ParseField("yeah"));
// ctor arg first so we can test for the bug we found one time
XContentParser xcontent = createParser(JsonXContent.jsonXContent, "{\n" + " \"yeah\": \"!\",\n" + " \"foo\": \"foo\"\n" + "}");
CalledOneTime result = parser.apply(xcontent, null);
assertTrue(result.fooSet);
// and ctor arg second just in case
xcontent = createParser(JsonXContent.jsonXContent, "{\n" + " \"foo\": \"foo\",\n" + " \"yeah\": \"!\"\n" + "}");
result = parser.apply(xcontent, null);
assertTrue(result.fooSet);
if (ctorArgOptional) {
// and without the constructor arg if we've made it optional
xcontent = createParser(JsonXContent.jsonXContent, "{\n" + " \"foo\": \"foo\"\n" + "}");
result = parser.apply(xcontent, null);
}
assertTrue(result.fooSet);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.
the class ConstructingObjectParserTests method testIgnoreUnknownFields.
public void testIgnoreUnknownFields() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"junk\" : 2\n" + "}");
class TestStruct {
public final String test;
TestStruct(String test) {
this.test = test;
}
}
ConstructingObjectParser<TestStruct, Void> objectParser = new ConstructingObjectParser<>("foo", true, a -> new TestStruct((String) a[0]));
objectParser.declareString(constructorArg(), new ParseField("test"));
TestStruct s = objectParser.apply(parser, null);
assertEquals(s.test, "foo");
}
Aggregations