Move ObjectPath and XContentUtils to libs/x-content (#34803)

These are generally useful utility classes that do not need to live in the Watcher code
This commit is contained in:
Alan Woodward 2018-11-02 15:12:09 +00:00 committed by GitHub
parent 6b08d5fc89
commit e2af849f70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 97 additions and 60 deletions

View File

@ -1,48 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.xpack.core.watcher.support.xcontent;
import org.elasticsearch.common.Strings;
package org.elasticsearch.common.xcontent;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
public class ObjectPath {
/**
* Helper class to navigate nested objects using dot notation
*/
public final class ObjectPath {
private static final String[] EMPTY_ARRAY = new String[0];
private ObjectPath() {
}
/**
* Return the value within a given object at the specified path, or
* {@code null} if the path does not exist
*/
@SuppressWarnings("unchecked")
public static <T> T eval(String path, Object object) {
return (T) evalContext(path, object);
}
private static Object evalContext(String path, Object ctx) {
final String[] parts;
if (path == null || path.isEmpty()) parts = Strings.EMPTY_ARRAY;
if (path == null || path.isEmpty()) parts = EMPTY_ARRAY;
else parts = path.split("\\.");
StringBuilder resolved = new StringBuilder();
for (String part : parts) {
if (ctx == null) {
return null;
}
if (ctx instanceof Map) {
ctx = ((Map) ctx).get(part);
if (resolved.length() != 0) {
resolved.append(".");
}
resolved.append(part);
} else if (ctx instanceof List) {
try {
int index = Integer.parseInt(part);
ctx = ((List) ctx).get(index);
if (resolved.length() != 0) {
resolved.append(".");
}
resolved.append(part);
} catch (NumberFormatException nfe) {
return null;
}
@ -50,10 +63,6 @@ public class ObjectPath {
try {
int index = Integer.parseInt(part);
ctx = Array.get(ctx, index);
if (resolved.length() != 0) {
resolved.append(".");
}
resolved.append(part);
} catch (NumberFormatException nfe) {
return null;
}

View File

@ -1,20 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.xpack.core.watcher.common.xcontent;
import org.elasticsearch.common.xcontent.XContentParser;
package org.elasticsearch.common.xcontent;
import java.io.IOException;
public class XContentUtils {
public final class XContentUtils {
private XContentUtils() {
}
// TODO open this up in core
/**
* Convert a {@link XContentParser.Token} to a value
*/
public static Object readValue(XContentParser parser, XContentParser.Token token) throws IOException {
if (token == XContentParser.Token.VALUE_NULL) {
return null;

View File

@ -1,12 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.xpack.watcher.support.xcontent;
package org.elasticsearch.common.xcontent;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import java.util.ArrayList;
import java.util.Arrays;
@ -18,23 +31,23 @@ import static java.util.Collections.singletonMap;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.is;
public class MapPathTests extends ESTestCase {
public void testEval() throws Exception {
public class ObjectPathTests extends ESTestCase {
public void testEval() {
Map<String, Object> map = singletonMap("key", "value");
assertThat(ObjectPath.eval("key", map), is((Object) "value"));
assertThat(ObjectPath.eval("key1", map), nullValue());
}
public void testEvalList() throws Exception {
List list = Arrays.asList(1, 2, 3, 4);
public void testEvalList() {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Map<String, Object> map = singletonMap("key", list);
int index = randomInt(3);
assertThat(ObjectPath.eval("key." + index, map), is(list.get(index)));
}
public void testEvalArray() throws Exception {
public void testEvalArray() {
int[] array = new int[] { 1, 2, 3, 4 };
Map<String, Object> map = singletonMap("key", array);
@ -42,13 +55,13 @@ public class MapPathTests extends ESTestCase {
assertThat(((Number) ObjectPath.eval("key." + index, map)).intValue(), is(array[index]));
}
public void testEvalMap() throws Exception {
public void testEvalMap() {
Map<String, Object> map = singletonMap("a", singletonMap("b", "val"));
assertThat(ObjectPath.eval("a.b", map), is((Object) "val"));
}
public void testEvalMixed() throws Exception {
public void testEvalMixed() {
Map<String, Object> map = new HashMap<>();
Map<String, Object> mapA = new HashMap<>();

View File

@ -12,11 +12,12 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.watcher.common.xcontent.XContentUtils;
import org.elasticsearch.common.xcontent.XContentUtils;
import java.io.IOException;
import java.io.InputStream;

View File

@ -9,7 +9,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.watcher.support.Variables;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

View File

@ -8,8 +8,8 @@ package org.elasticsearch.xpack.watcher.condition;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.watcher.common.xcontent.XContentUtils;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.XContentUtils;
import org.elasticsearch.common.xcontent.ObjectPath;
import java.io.IOException;
import java.time.Clock;

View File

@ -8,8 +8,8 @@ package org.elasticsearch.xpack.watcher.condition;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.watcher.common.xcontent.XContentUtils;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.XContentUtils;
import org.elasticsearch.common.xcontent.ObjectPath;
import java.io.IOException;
import java.time.Clock;

View File

@ -9,7 +9,7 @@ import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
import java.io.IOException;

View File

@ -23,7 +23,7 @@ import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.watcher.WatcherFeatureSetUsage;
import org.elasticsearch.xpack.core.watcher.WatcherMetaData;
import org.elasticsearch.xpack.core.watcher.common.stats.Counters;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
import org.elasticsearch.xpack.core.watcher.transport.actions.stats.WatcherStatsAction;
import org.elasticsearch.xpack.core.watcher.transport.actions.stats.WatcherStatsResponse;

View File

@ -14,7 +14,7 @@ import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
import java.util.Map;

View File

@ -15,7 +15,7 @@ import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder;
import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode;
import org.elasticsearch.xpack.core.watcher.execution.ExecutionState;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequestBuilder;
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchResponse;
import org.elasticsearch.xpack.core.watcher.watch.Watch;

View File

@ -53,7 +53,7 @@ import org.elasticsearch.xpack.core.watcher.execution.Wid;
import org.elasticsearch.xpack.core.watcher.history.WatchRecord;
import org.elasticsearch.xpack.core.watcher.input.ExecutableInput;
import org.elasticsearch.xpack.core.watcher.input.Input;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.core.watcher.transform.ExecutableTransform;
import org.elasticsearch.xpack.core.watcher.transform.Transform;
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;

View File

@ -18,7 +18,7 @@ import org.elasticsearch.test.http.MockResponse;
import org.elasticsearch.test.http.MockWebServer;
import org.elasticsearch.xpack.core.watcher.execution.ExecutionState;
import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.watcher.common.http.HttpMethod;
import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate;
import org.elasticsearch.xpack.watcher.condition.InternalAlwaysCondition;

View File

@ -18,7 +18,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams;
import org.elasticsearch.xpack.core.watcher.watch.Payload;
import org.elasticsearch.xpack.watcher.common.http.HttpClient;

View File

@ -9,7 +9,7 @@ import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.hamcrest.Matchers;
import java.io.IOException;

View File

@ -8,7 +8,7 @@ package org.elasticsearch.xpack.watcher.support;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.execution.Wid;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
import org.elasticsearch.xpack.core.watcher.watch.Payload;
import org.elasticsearch.xpack.watcher.test.WatcherTestUtils;

View File

@ -10,7 +10,7 @@ import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
import org.elasticsearch.xpack.core.watcher.client.WatcherClient;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchResponse;
import org.elasticsearch.xpack.watcher.condition.ScriptCondition;

View File

@ -9,7 +9,7 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode;
import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchResponse;
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
import org.elasticsearch.xpack.watcher.actions.logging.LoggingAction;

View File

@ -12,7 +12,7 @@ import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
import org.elasticsearch.test.http.MockResponse;
import org.elasticsearch.test.http.MockWebServer;
import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchResponse;
import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchResponse;

View File

@ -19,7 +19,7 @@ import org.elasticsearch.test.StreamsUtils;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.upgrades.AbstractFullClusterRestartTestCase;
import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.xpack.security.support.SecurityIndexManager;
import org.elasticsearch.xpack.test.rest.XPackRestTestHelper;
import org.elasticsearch.xpack.watcher.actions.logging.LoggingAction;

View File

@ -20,7 +20,7 @@ import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import java.io.IOException;
import java.time.Instant;

View File

@ -12,7 +12,7 @@ import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.core.watcher.support.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.hamcrest.Matcher;
import java.io.IOException;