-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCreateCollectionRequest.java
More file actions
83 lines (75 loc) · 3.05 KB
/
Copy pathCreateCollectionRequest.java
File metadata and controls
83 lines (75 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package io.weaviate.client6.v1.api.collections;
import java.util.Collections;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import io.weaviate.client6.v1.internal.json.JSON;
import io.weaviate.client6.v1.internal.rest.Endpoint;
import io.weaviate.client6.v1.internal.rest.SimpleEndpoint;
/**
* Create a collection from a {@link CollectionConfig} or from a raw JSON schema
* definition.
*
* @param <T> Type of the payload. A {@code String} is sent verbatim, anything
* else is serialized first.
*/
public record CreateCollectionRequest<T>(T collection) {
/**
* Endpoint which sends the payload to {@code POST /schema}.
*
* <p>
* A {@code String} payload is forwarded byte-for-byte, so it may use any
* option the server accepts — including ones this client version does not
* model. Nothing is validated client-side either: a typo in a key surfaces as a
* server error rather than a compile error.
*
* <p>
* The server echoes the stored configuration back, which this endpoint discards
* — a raw payload may well describe a collection that
* {@link CollectionConfig} cannot represent.
*/
public static <T> Endpoint<CreateCollectionRequest<T>, Void> endpoint() {
return SimpleEndpoint.sideEffect(
request -> "POST",
request -> "/schema/",
request -> Collections.emptyMap(),
request -> request.collection instanceof String json
? json
: JSON.serialize(request.collection));
}
/**
* Name of the collection defined by a raw JSON document (its {@code "class"}
* key), which the client needs in order to return a handle for it.
*
* <p>
* This is the only part of the document the client reads. Calling it before
* sending doubles as validation: a document that is not usable as a
* {@code POST /schema} payload fails before the request leaves the process
* rather than after a round-trip.
*
* @throws IllegalArgumentException in case the string is not a JSON object or
* does not carry a {@code "class"} name.
*/
public static String collectionNameFromJson(String json) {
if (json == null || json.isBlank()) {
throw new IllegalArgumentException("collection JSON must not be null or blank");
}
JsonElement document;
try {
document = JSON.toJsonElement(json);
} catch (JsonParseException e) {
throw new IllegalArgumentException("collection JSON is not valid JSON", e);
}
if (!document.isJsonObject()) {
throw new IllegalArgumentException("collection JSON must be a JSON object");
}
var collectionName = document.getAsJsonObject().get("class");
if (collectionName == null
|| !collectionName.isJsonPrimitive()
|| !collectionName.getAsJsonPrimitive().isString()
|| collectionName.getAsString().isBlank()) {
throw new IllegalArgumentException(
"collection JSON must have a non-empty string \"class\" key with the collection name");
}
return collectionName.getAsString();
}
}