Search in sources :

Example 26 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testParseNested.

public void testParseNested() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : 1, \"object\" : { \"test\": 2}}");
    class TestStruct {

        public int test;

        TestStruct object;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
    TestStruct s = new TestStruct();
    s.object = new TestStruct();
    objectParser.declareField((i, c, x) -> c.test = i.intValue(), new ParseField("test"), ValueType.INT);
    objectParser.declareField((i, c, x) -> objectParser.parse(parser, c.object, null), new ParseField("object"), ValueType.OBJECT);
    objectParser.parse(parser, s, null);
    assertEquals(s.test, 1);
    assertEquals(s.object.test, 2);
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 27 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testDeprecationWarnings.

public void testDeprecationWarnings() throws IOException {
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
    TestStruct s = new TestStruct();
    XContentParser parser = createParser(XContentType.JSON.xContent(), "{\"old_test\" : \"foo\"}");
    objectParser.declareField((i, v, c) -> v.test = i.text(), new ParseField("test", "old_test"), ObjectParser.ValueType.STRING);
    objectParser.parse(parser, s, null);
    assertEquals("foo", s.test);
    assertWarnings("Deprecated field [old_test] used, expected [test] instead");
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 28 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testIgnoreUnknownFields.

public void testIgnoreUnknownFields() throws IOException {
    XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
    b.startObject();
    {
        b.field("test", "foo");
        b.field("junk", 2);
    }
    b.endObject();
    b = shuffleXContent(b);
    XContentParser parser = createParser(JsonXContent.jsonXContent, b.bytes());
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, null);
    objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
    TestStruct s = objectParser.parse(parser, new TestStruct(), null);
    assertEquals(s.test, "foo");
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 29 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testParseEnumFromString.

public void testParseEnumFromString() throws IOException {
    class TestStruct {

        public TestEnum test;

        public void set(TestEnum value) {
            test = value;
        }
    }
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : \"FOO\" }");
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo");
    objectParser.declareString((struct, value) -> struct.set(TestEnum.valueOf(value)), new ParseField("test"));
    TestStruct s = objectParser.parse(parser, new TestStruct(), null);
    assertEquals(s.test, TestEnum.FOO);
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 30 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testExceptions.

public void testExceptions() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"test\" : \"foo\"}");
    class TestStruct {

        public void setTest(int test) {
        }
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("the_parser");
    TestStruct s = new TestStruct();
    objectParser.declareInt(TestStruct::setTest, new ParseField("test"));
    try {
        objectParser.parse(parser, s, null);
        fail("numeric value expected");
    } catch (ParsingException ex) {
        assertEquals(ex.getMessage(), "[the_parser] failed to parse field [test]");
        assertTrue(ex.getCause() instanceof NumberFormatException);
    }
    parser = createParser(JsonXContent.jsonXContent, "{\"not_supported_field\" : \"foo\"}");
    try {
        objectParser.parse(parser, s, null);
        fail("field not supported");
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "[the_parser] unknown field [not_supported_field], parser not found");
    }
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParsingException(org.elasticsearch.common.ParsingException) ParseField(org.elasticsearch.common.ParseField)

Aggregations

ParseField (org.elasticsearch.common.ParseField)31 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)6 List (java.util.List)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 ArrayList (java.util.ArrayList)4 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)4 UncheckedIOException (java.io.UncheckedIOException)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 Map (java.util.Map)2 CheckedFunction (org.elasticsearch.common.CheckedFunction)2 Nullable (org.elasticsearch.common.Nullable)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 ConstructingObjectParser.constructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg)2 ConstructingObjectParser.optionalConstructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg)2 ValueType (org.elasticsearch.common.xcontent.ObjectParser.ValueType)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2