@@ -38,14 +38,14 @@ data class CompositeQueryResponse(val compositeResponse: List<QueryResponse>) {
3838 @JsonClass(generateAdapter = true )
3939 data class Body (val done : Boolean , val records : List <Record >, val totalSize : Int ) {
4040 @JsonClass(generateAdapter = true )
41- @AdaptedBy(RecordAdapterFactory ::class )
41+ @AdaptedBy(RecordFactory ::class )
4242 data class Record (val attributes : Attributes , val recordBody : Map <String , Any ?>) {
4343 @JsonClass(generateAdapter = true )
4444 data class Attributes (val type : String , val url : String )
4545 }
4646 }
4747
48- class RecordAdapterFactory : JsonAdapter .Factory {
48+ class RecordFactory : JsonAdapter .Factory {
4949 override fun create (
5050 type : Type ,
5151 annotations : Set <Annotation >,
@@ -57,42 +57,42 @@ data class CompositeQueryResponse(val compositeResponse: List<QueryResponse>) {
5757 null
5858 }
5959 }
60- }
6160
62- class RecordAdapter (val moshi : Moshi ) : JsonAdapter<Record>() {
63- private val options = JsonReader .Options .of(" attributes" )
64- @OptIn(ExperimentalStdlibApi ::class )
65- private val attributesJsonAdapter = moshi.adapter<Attributes >()
66- @OptIn(ExperimentalStdlibApi ::class ) private val dynamicJsonAdapter = moshi.adapter<Any >()
67-
68- override fun fromJson (reader : JsonReader ): Record ? {
69- reader.beginObject()
70- var attributes: Attributes ? = null
71- val recordBody = mutableMapOf<String , Any ?>()
72- while (reader.hasNext()) {
73- when (reader.selectName(options)) {
74- 0 -> {
75- if (attributes != null ) {
76- throw JsonDataException (" Duplicate attributes Node" )
61+ class RecordAdapter (val moshi : Moshi ) : JsonAdapter<Record>() {
62+ private val options = JsonReader .Options .of(" attributes" )
63+ @OptIn(ExperimentalStdlibApi ::class )
64+ private val attributesJsonAdapter = moshi.adapter<Attributes >()
65+ @OptIn(ExperimentalStdlibApi ::class ) private val dynamicJsonAdapter = moshi.adapter<Any >()
66+
67+ override fun fromJson (reader : JsonReader ): Record ? {
68+ reader.beginObject()
69+ var attributes: Attributes ? = null
70+ val recordBody = mutableMapOf<String , Any ?>()
71+ while (reader.hasNext()) {
72+ when (reader.selectName(options)) {
73+ 0 -> {
74+ if (attributes != null ) {
75+ throw JsonDataException (" Duplicate attributes Node" )
76+ }
77+ attributes = attributesJsonAdapter.fromJson(reader)
7778 }
78- attributes = attributesJsonAdapter.fromJson(reader)
79+ - 1 -> recordBody[reader.nextName()] = reader.readJsonValue()!!
80+ else -> throw AssertionError ()
7981 }
80- - 1 -> recordBody[reader.nextName()] = reader.readJsonValue()!!
81- else -> throw AssertionError ()
8282 }
83+ reader.endObject()
84+ return Record (attributes!! , recordBody)
8385 }
84- reader.endObject()
85- return Record (attributes!! , recordBody)
86- }
8786
88- override fun toJson (writer : JsonWriter , record : Record ? ) =
89- with (writer) {
90- objW(record) {
91- name(" attributes" )
92- attributesJsonAdapter.toJson(this @with, attributes)
93- mapW(recordBody, dynamicJsonAdapter)
87+ override fun toJson (writer : JsonWriter , record : Record ? ) =
88+ with (writer) {
89+ objW(record) {
90+ name(" attributes" )
91+ attributesJsonAdapter.toJson(this @with, attributes)
92+ mapW(recordBody, dynamicJsonAdapter)
93+ }
9494 }
95- }
95+ }
9696 }
9797 }
9898
@@ -116,21 +116,31 @@ data class CompositeQueryResponse(val compositeResponse: List<QueryResponse>) {
116116 compositeResponse
117117 .mapNotNull { it as ? ErrorQueryResponse }
118118 .filter {
119- it.httpStatusCode !in SUCCESSFUL &&
119+ it.httpStatusCode !in SUCCESSFUL_HTTP_STATUSES &&
120120 it.body.firstOrNull()?.let { error ->
121121 error.errorCode == PROCESSING_HALTED &&
122122 error.message == OPERATION_IN_TRANSACTION_FAILED_ERROR
123123 } != true
124124 }
125125 }
126126
127+ @Json(ignore = true )
128+ @get:JvmName(" isSuccessful" )
129+ val isSuccessful: Boolean by lazy {
130+ compositeResponse.all { it.httpStatusCode in SUCCESSFUL_HTTP_STATUSES }
131+ }
132+
127133 @Json(ignore = true )
128134 @get:JvmName(" errorResponseCount" )
129- val errorResponseCount: Int by lazy { errorResponses.size }
135+ val errorResponseCount: Int by lazy {
136+ compositeResponse.count { it.httpStatusCode !in SUCCESSFUL_HTTP_STATUSES }
137+ }
130138
131139 @Json(ignore = true )
132140 @get:JvmName(" successResponseCount" )
133- val successResponseCount: Int by lazy { compositeResponse.size - errorResponseCount }
141+ val successResponseCount: Int by lazy {
142+ compositeResponse.count { it.httpStatusCode in SUCCESSFUL_HTTP_STATUSES }
143+ }
134144
135145 @Json(ignore = true )
136146 @get:JvmName(" firstErrorResponse" )
@@ -148,7 +158,7 @@ data class CompositeQueryResponse(val compositeResponse: List<QueryResponse>) {
148158 DiMorphicAdapter .of(
149159 QueryResponse ::class .java,
150160 " httpStatusCode" ,
151- { it.nextInt() in SUCCESSFUL },
161+ { it.nextInt() in SUCCESSFUL_HTTP_STATUSES },
152162 SuccessQueryResponse ::class .java,
153163 ErrorQueryResponse ::class .java,
154164 )
0 commit comments