-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ScoreFunction: variants are optional (#752)
Signed-off-by: 西狩 <923568791@qq.com>
- Loading branch information
1 parent
5239303
commit 00eb575
Showing
21 changed files
with
1,359 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
java-client/src/main/java/org/opensearch/client/json/DelegatingJsonpMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. 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. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.client.json; | ||
|
||
import jakarta.json.spi.JsonProvider; | ||
import jakarta.json.stream.JsonGenerator; | ||
import jakarta.json.stream.JsonParser; | ||
import javax.annotation.Nullable; | ||
|
||
public abstract class DelegatingJsonpMapper implements JsonpMapper { | ||
|
||
protected final JsonpMapper mapper; | ||
|
||
public DelegatingJsonpMapper(JsonpMapper mapper) { | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public JsonProvider jsonProvider() { | ||
return mapper.jsonProvider(); | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(JsonParser parser, Class<T> clazz) { | ||
return mapper.deserialize(parser, clazz); | ||
} | ||
|
||
@Override | ||
public <T> void serialize(T value, JsonGenerator generator) { | ||
mapper.serialize(value, generator); | ||
} | ||
|
||
@Override | ||
public boolean ignoreUnknownFields() { | ||
return mapper.ignoreUnknownFields(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public <T> T attribute(String name) { | ||
return mapper.attribute(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
java-client/src/main/java/org/opensearch/client/json/JsonpMapperFeatures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. 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. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.client.json; | ||
|
||
/** | ||
* Defines attribute names for {@link JsonpMapper} features. | ||
*/ | ||
public class JsonpMapperFeatures { | ||
|
||
/** | ||
* | ||
*/ | ||
public static final String SERIALIZE_TYPED_KEYS = JsonpMapperFeatures.class.getName() + ":SERIALIZE_TYPED_KEYS"; | ||
|
||
/** | ||
* Disables custom variants in union types (false by default). Used in tests to avoid interpreting wrong variant kinds as a custom | ||
* extension. | ||
*/ | ||
public static final String FORBID_CUSTOM_VARIANTS = JsonpMapperFeatures.class.getName() + ":FORBID_CUSTOM_VARIANTS"; | ||
|
||
} |
152 changes: 152 additions & 0 deletions
152
java-client/src/main/java/org/opensearch/client/json/JsonpMappingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. 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. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.client.json; | ||
|
||
import jakarta.json.stream.JsonLocation; | ||
import jakarta.json.stream.JsonParser; | ||
import jakarta.json.stream.JsonParsingException; | ||
import java.util.LinkedList; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* A mapping exception. The exception message contains the JSON path and location where the problem happened. | ||
*/ | ||
public class JsonpMappingException extends JsonParsingException { | ||
private final LinkedList<Object> path = new LinkedList<>(); | ||
private Object ref; | ||
|
||
public JsonpMappingException(String message, JsonLocation location) { | ||
super(message, location); | ||
} | ||
|
||
public JsonpMappingException(String message, Throwable cause, JsonLocation location) { | ||
super(message, cause, location); | ||
} | ||
|
||
public JsonpMappingException(Throwable cause, JsonLocation location) { | ||
super(cause.toString(), cause, location); | ||
} | ||
|
||
private static final Pattern identifier = Pattern.compile("[_a-zA-Z][_a-zA-Z0-9]*"); | ||
|
||
@Override | ||
public String getMessage() { | ||
StringBuilder sb = new StringBuilder("Error deserializing"); | ||
if (ref != null) { | ||
sb.append(' '); | ||
String className = ref.getClass().getName(); | ||
if (className.endsWith("$Builder")) { | ||
sb.append(className, 0, className.length() - "$Builder".length()); | ||
} else { | ||
sb.append(className); | ||
} | ||
} | ||
sb.append(": ").append(super.getMessage()); | ||
|
||
if (!path.isEmpty()) { | ||
sb.append(" (JSON path: "); | ||
path(sb); | ||
sb.append(") "); | ||
} | ||
|
||
sb.append(getLocation()); | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* The JSON path where this exception happened. | ||
*/ | ||
public String path() { | ||
StringBuilder sb = new StringBuilder(); | ||
path(sb); | ||
return sb.toString(); | ||
} | ||
|
||
// Package-visible for testing | ||
void path(StringBuilder sb) { | ||
String sep = ""; | ||
for (Object item : path) { | ||
if (item instanceof Integer) { | ||
sb.append("[").append(((Integer) item).intValue()).append("]"); | ||
} else { | ||
String str = item.toString(); | ||
if (identifier.matcher(str).matches()) { | ||
sb.append(sep).append(item); | ||
} else { | ||
sb.append("['").append(str).append("']"); | ||
} | ||
} | ||
sep = "."; | ||
} | ||
} | ||
|
||
public JsonpMappingException prepend(Object ref, String name) { | ||
return prepend0(ref, name); | ||
} | ||
|
||
public JsonpMappingException prepend(Object ref, int idx) { | ||
return prepend0(ref, idx); | ||
} | ||
|
||
private JsonpMappingException prepend0(Object ref, Object pathItem) { | ||
if (pathItem != null) { | ||
this.path.addFirst(pathItem); | ||
} | ||
// Keep the deepest object reference in the JSON hierarchy | ||
if (this.ref == null) { | ||
this.ref = ref; | ||
} | ||
return this; | ||
} | ||
|
||
public static JsonpMappingException from(Throwable cause, Object ref, String name, JsonParser parser) { | ||
return from0(cause, ref, name, parser); | ||
} | ||
|
||
public static JsonpMappingException from(Throwable cause, int index, JsonParser parser) { | ||
return from0(cause, null, index, parser); | ||
} | ||
|
||
private static JsonpMappingException from0(Throwable cause, Object ref, Object pathItem, JsonParser parser) { | ||
JsonpMappingException jme; | ||
|
||
if (cause instanceof JsonpMappingException) { | ||
jme = (JsonpMappingException) cause; | ||
} else { | ||
jme = new JsonpMappingException(cause, parser.getLocation()); | ||
} | ||
|
||
return jme.prepend0(ref, pathItem); | ||
} | ||
} | ||
|
Oops, something went wrong.