Search in sources :

Example 21 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 22 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 23 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)

Example 24 with ParseField

use of 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}]}");
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ArrayList(java.util.ArrayList) List(java.util.List) ParseField(org.elasticsearch.common.ParseField)

Example 25 with ParseField

use of 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");
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URI(java.net.URI) 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