Search in sources :

Example 21 with ParseField

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

Example 22 with ParseField

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");
}
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)

Example 23 with ParseField

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

Example 24 with ParseField

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);
}
Also used : ParseField(org.elasticsearch.common.ParseField)

Example 25 with ParseField

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");
}
Also used : 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