2626package algolia .definitions
2727
2828import algolia .http .{HttpPayload , POST }
29- import algolia .inputs .{
30- AddObjectOperation ,
31- BatchOperations ,
32- UpdateObjectOperation
33- }
29+ import algolia .inputs ._
3430import algolia .objects .RequestOptions
35- import org .json4s .Formats
31+ import org .json4s .JsonAST . JValue
3632import org .json4s .native .Serialization ._
33+ import org .json4s .{Extraction , Formats }
3734
3835case class IndexingBatchDefinition (
3936 index : String ,
@@ -51,16 +48,7 @@ case class IndexingBatchDefinition(
5148 copy(requestOptions = Some (requestOptions))
5249
5350 override private [algolia] def build (): HttpPayload = {
54- val operations = definitions.map {
55- case IndexingDefinition (_, None , Some (obj), _) =>
56- hasObjectId(obj) match {
57- case (true , o) => UpdateObjectOperation (o)
58- case (false , o) => AddObjectOperation (o)
59- }
60-
61- case IndexingDefinition (_, Some (objectId), Some (obj), _) =>
62- UpdateObjectOperation (addObjectId(obj, objectId))
63- }
51+ val operations = definitions.flatMap(transform)
6452
6553 HttpPayload (
6654 POST ,
@@ -70,4 +58,99 @@ case class IndexingBatchDefinition(
7058 requestOptions = requestOptions
7159 )
7260 }
61+
62+ private def transform (
63+ definition : Definition
64+ ): Iterable [BatchOperation [JValue ]] = {
65+ definition match {
66+ case IndexingDefinition (_, None , Some (obj), _) =>
67+ hasObjectId(obj) match {
68+ case (true , o) => Iterable (UpdateObjectOperation (o))
69+ case (false , o) => Iterable (AddObjectOperation (o))
70+ }
71+
72+ case IndexingDefinition (_, Some (objectId), Some (obj), _) =>
73+ Iterable (UpdateObjectOperation (addObjectId(obj, objectId)))
74+
75+ case ClearIndexDefinition (_, _) =>
76+ Iterable (ClearIndexOperation ())
77+
78+ case DeleteObjectDefinition (_, Some (oid), _) =>
79+ Iterable (DeleteObjectOperation (objectID = oid))
80+
81+ case SafeDeleteObjectDefinition (op, _) =>
82+ Iterable (DeleteObjectOperation (objectID = op.objectID))
83+
84+ case DeleteIndexDefinition (_, _) =>
85+ Iterable (DeleteIndexOperation ())
86+
87+ case PartialUpdateObjectOperationDefinition (
88+ operation,
89+ index,
90+ Some (objectId),
91+ Some (attribute),
92+ value,
93+ true ,
94+ _
95+ ) =>
96+ val body = Map (
97+ " objectID" -> objectId,
98+ attribute -> PartialUpdateObject (operation.name, value)
99+ )
100+ Iterable (
101+ PartialUpdateObjectOperation (Extraction .decompose(body))
102+ )
103+
104+ case PartialUpdateObjectOperationDefinition (
105+ operation,
106+ index,
107+ Some (objectId),
108+ Some (attribute),
109+ value,
110+ false ,
111+ _
112+ ) =>
113+ val body = Map (
114+ " objectID" -> objectId,
115+ attribute -> PartialUpdateObject (operation.name, value)
116+ )
117+ Iterable (
118+ PartialUpdateObjectNoCreateOperation (Extraction .decompose(body))
119+ )
120+
121+ case PartialUpdateObjectDefinition (
122+ _,
123+ Some (objectId),
124+ Some (attribute),
125+ value,
126+ _
127+ ) =>
128+ val body = Map (
129+ " objectID" -> objectId,
130+ attribute -> value
131+ )
132+ Iterable (
133+ PartialUpdateObjectOperation (Extraction .decompose(body))
134+ )
135+
136+ case IndexingBatchDefinition (_, defs, _) =>
137+ defs.flatMap(transform)
138+
139+ case PartialUpdateOneObjectDefinition (
140+ _,
141+ Some (obj),
142+ createIfNotExists,
143+ _
144+ ) =>
145+ if (createIfNotExists) {
146+ Iterable (
147+ PartialUpdateObjectOperation (Extraction .decompose(obj))
148+ )
149+ } else {
150+ Iterable (
151+ PartialUpdateObjectNoCreateOperation (Extraction .decompose(obj))
152+ )
153+ }
154+ }
155+ }
73156}
0 commit comments