Search in sources :

Example 16 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ParseFieldRegistry method lookupReturningNullIfNotFound.

/**
     * Lookup a value from the registry by name while checking that the name matches the ParseField.
     *
     * @param name The name of the thing to look up.
     * @return The value being looked up or null if it wasn't found.
     * @throws ParsingException if the named thing isn't in the registry or the name was deprecated and deprecated names aren't supported.
     */
public T lookupReturningNullIfNotFound(String name) {
    Tuple<ParseField, T> parseFieldAndValue = registry.get(name);
    if (parseFieldAndValue == null) {
        return null;
    }
    ParseField parseField = parseFieldAndValue.v1();
    T value = parseFieldAndValue.v2();
    boolean match = parseField.match(name);
    //this is always expected to match, ParseField is useful for deprecation warnings etc. here
    assert match : "ParseField did not match registered name [" + name + "][" + registryName + "]";
    return value;
}
Also used : ParseField(org.elasticsearch.common.ParseField)

Example 17 with ParseField

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

use of 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)

Example 19 with ParseField

use of 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 20 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)

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