Search in sources :

Example 26 with ParseField

use of 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 27 with ParseField

use of 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 28 with ParseField

use of 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 29 with ParseField

use of 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)29 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)3 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 UncheckedIOException (java.io.UncheckedIOException)2 URI (java.net.URI)2 HashMap (java.util.HashMap)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 JsonXContent (org.elasticsearch.common.xcontent.json.JsonXContent)2