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