Skip to content

Commit

Permalink
chore: [ANDROSDK-1864] Remove not useful child type parameter from fi…
Browse files Browse the repository at this point in the history
…elds (#2166)
  • Loading branch information
marcamsn authored Jun 19, 2024
2 parents f98f861 + 11db87b commit 6bde10a
Show file tree
Hide file tree
Showing 142 changed files with 1,090 additions and 1,215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class OutsideRetrofitFactory {
.baseUrl(baseUrl.toHttpUrlOrNull()!!)
.client(okHttpClient)
.addConverterFactory(JacksonConverterFactory.create(objectMapper()))
.addConverterFactory(FilterConverterFactory.create())
.addConverterFactory(FieldsConverterFactory.create())
.addConverterFactory(FilterConverterFactory())
.addConverterFactory(FieldsConverterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.validateEagerly(true)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ internal object ApiSchemaFields {
private val propertyFields: Fields<ApiSchema.Companion.SchemaProperty> =
Fields.builder<ApiSchema.Companion.SchemaProperty>()
.fields(
ApiSchemaPropertyFh.field<String>(KLASS),
ApiSchemaPropertyFh.field<String>(PROPERTY_TYPE),
ApiSchemaPropertyFh.field<String>(CONSTANTS),
ApiSchemaPropertyFh.field(KLASS),
ApiSchemaPropertyFh.field(PROPERTY_TYPE),
ApiSchemaPropertyFh.field(CONSTANTS),
).build()

val allFields: Fields<ApiSchema> = Fields.builder<ApiSchema>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ import org.hisp.dhis.android.core.arch.api.filters.internal.Filter
import org.hisp.dhis.android.core.arch.api.filters.internal.InFilter
import org.hisp.dhis.android.core.arch.api.filters.internal.SingleValueFilter

internal data class Field<Parent, Child> private constructor(override val name: String) : Property<Parent, Child> {
internal data class Field<Parent> internal constructor(override val name: String) : Property<Parent> {

fun <V> eq(value: V): Filter<Parent, Child> {
fun <V> eq(value: V): Filter<Parent> {
return SingleValueFilter.eq(this, value.toString())
}

fun gt(value: String): Filter<Parent, Child> {
fun gt(value: String): Filter<Parent> {
return SingleValueFilter.gt(this, value)
}

fun like(value: String): Filter<Parent, Child> {
fun like(value: String): Filter<Parent> {
return SingleValueFilter.like(this, value)
}

fun `in`(values: Collection<String>): Filter<Parent, Child> {
fun `in`(values: Collection<String>): Filter<Parent> {
return InFilter.create(this, values)
}

companion object {
@JvmStatic
fun <T, K> create(name: String): Field<T, K> {
fun <T> create(name: String): Field<T> {
return Field(name)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
*/
package org.hisp.dhis.android.core.arch.api.fields.internal

internal data class Fields<T>(val fields: List<Property<T, *>>) {
internal data class Fields<T>(val fields: List<Property<T>>) {

class Builder<T> internal constructor() {
val fields: MutableList<Property<T, *>> = mutableListOf()
val fields: MutableList<Property<T>> = mutableListOf()

@SafeVarargs
fun fields(vararg properties: Property<T, *>): Builder<T> {
fun fields(vararg properties: Property<T>): Builder<T> {
require(properties.isNotEmpty()) { "properties should not be empty" }
fields.addAll(properties)
return this
}

fun fields(properties: Collection<Property<T, *>>): Builder<T> {
fun fields(properties: Collection<Property<T>>): Builder<T> {
require(properties.isNotEmpty()) { "properties should not be empty" }
fields.addAll(properties)
return this
Expand All @@ -60,10 +60,10 @@ internal data class Fields<T>(val fields: List<Property<T, *>>) {
return Builder()
}

private fun generateStringFromFields(properties: List<Property<*, *>>): String {
private fun generateStringFromFields(properties: List<Property<*>>): String {
val fieldsStringList = properties.map { field ->
when (field) {
is Field<*, *> -> field.name
is Field<*> -> field.name
is NestedField<*, *> ->
field.name +
if (field.children.isNotEmpty()) "[${generateStringFromFields(field.children)}]" else ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.android.core.arch.api.fields.internal

package org.hisp.dhis.android.core.arch.api.fields.internal;
import retrofit2.Converter
import java.io.IOException

import java.io.IOException;

import retrofit2.Converter;

class FieldsConverter implements Converter<Fields, String> {
FieldsConverter() {
// explicit empty constructor
}

@Override
@SuppressWarnings("unchecked")
public String convert(Fields fields) throws IOException {
return fields.generateString();
internal class FieldsConverter : Converter<Fields<*>, String> {
@Throws(IOException::class)
override fun convert(fields: Fields<*>): String {
return fields.generateString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,17 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.android.core.arch.api.fields.internal

package org.hisp.dhis.android.core.arch.api.fields.internal;
import org.hisp.dhis.android.core.arch.api.filters.internal.Which
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

import org.hisp.dhis.android.core.arch.api.filters.internal.Which;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import retrofit2.Converter;
import retrofit2.Retrofit;

public final class FieldsConverterFactory extends Converter.Factory {
public static FieldsConverterFactory create() {
return new FieldsConverterFactory();
}

private FieldsConverterFactory() {}

@Override
public Converter<?, String> stringConverter(Type typef,
Annotation[] annotations, Retrofit retrofit) {
for (Annotation annotation : annotations) {
if (annotation instanceof Which) {
return new FieldsConverter();
}
}
return null;
}
internal class FieldsConverterFactory : Converter.Factory() {
override fun stringConverter(
type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit,
): Converter<*, String>? = annotations.firstOrNull { it is Which }?.let { FieldsConverter() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
*/
package org.hisp.dhis.android.core.arch.api.fields.internal

internal data class NestedField<Parent, Child> private constructor(
internal data class NestedField<Parent, Child> internal constructor(
override val name: String,
val children: List<Property<Child, *>> = emptyList(),
) : Property<Parent, Child> {
val children: List<Property<Child>> = emptyList(),
) : Property<Parent> {
@SafeVarargs
fun with(vararg properties: Property<Child, *>): NestedField<Parent, Child> {
fun with(vararg properties: Property<Child>): NestedField<Parent, Child> {
return with(listOf(*properties))
}

fun with(properties: List<Property<Child, *>>?): NestedField<Parent, Child> {
fun with(properties: List<Property<Child>>?): NestedField<Parent, Child> {
return properties?.let {
NestedField(name, it)
} ?: create(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
*/
package org.hisp.dhis.android.core.arch.api.fields.internal

internal interface Property<Parent, Child> {
internal interface Property<Parent> {
val name: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ package org.hisp.dhis.android.core.arch.api.filters.internal

import org.hisp.dhis.android.core.arch.api.fields.internal.Field

internal interface Filter<T, K> {
val field: Field<T, K>
internal interface Filter<T> {
val field: Field<T>
val operator: String
val values: Collection<String>
fun generateString(): String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,16 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.android.core.arch.api.filters.internal;
package org.hisp.dhis.android.core.arch.api.filters.internal

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

import retrofit2.Converter;
import retrofit2.Retrofit;

public final class FilterConverterFactory extends Converter.Factory {
public static FilterConverterFactory create() {
return new FilterConverterFactory();
}

private FilterConverterFactory() {}

@Override
public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
for (Annotation annotation : annotations) {
if (annotation instanceof Where) {
return new FilterConverter();
}
}
return null;
}
internal class FilterConverterFactory : Converter.Factory() {
override fun stringConverter(
type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit,
): Converter<*, String>? = annotations.firstOrNull { it is Where }?.let { FilterConverter() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ package org.hisp.dhis.android.core.arch.api.filters.internal

import org.hisp.dhis.android.core.arch.api.fields.internal.Field

internal class InFilter<T, K>(
override val field: Field<T, K>,
internal class InFilter<T>(
override val field: Field<T>,
override val operator: String,
override val values: Collection<String>,
) : Filter<T, K> {
) : Filter<T> {
override fun generateString(): String {
val valuesString = values.joinToString(",")
return "${field.name}:$operator:[$valuesString]"
}

companion object {
fun <T, K> create(
field: Field<T, K>,
fun <T> create(
field: Field<T>,
values: Collection<String>,
): Filter<T, K> {
): Filter<T> {
// If the filter is incomplete, return null so the filter is not included in the request.
return InFilter(field, "in", values)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,34 @@ package org.hisp.dhis.android.core.arch.api.filters.internal

import org.hisp.dhis.android.core.arch.api.fields.internal.Field

internal class SingleValueFilter<T, K> private constructor(
override val field: Field<T, K>,
internal class SingleValueFilter<T> private constructor(
override val field: Field<T>,
override val operator: String,
override val values: Collection<String>,
) : Filter<T, K> {
) : Filter<T> {
override fun generateString(): String {
return "${field.name}:$operator:${values.first()}"
}

companion object {
private fun <T, K> create(
field: Field<T, K>,
private fun <T> create(
field: Field<T>,
operator: String,
value: String,
): Filter<T, K> {
): Filter<T> {
// If the filter is incomplete, return null so the filter is not included in the request.
return SingleValueFilter(field, operator, listOf(value))
}

fun <T, K> gt(field: Field<T, K>, value: String): Filter<T, K> {
fun <T> gt(field: Field<T>, value: String): Filter<T> {
return create(field, "gt", value)
}

fun <T, K> eq(field: Field<T, K>, value: String): Filter<T, K> {
fun <T> eq(field: Field<T>, value: String): Filter<T> {
return create(field, "eq", value)
}

fun <T, K> like(field: Field<T, K>, value: String): Filter<T, K> {
fun <T> like(field: Field<T>, value: String): Filter<T> {
return create(field, "like", value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ static Retrofit retrofit(OkHttpClient okHttpClient) throws D2Error {

.client(okHttpClient)
.addConverterFactory(JacksonConverterFactory.create(ObjectMapperFactory.objectMapper()))
.addConverterFactory(FilterConverterFactory.create())
.addConverterFactory(FieldsConverterFactory.create())
.addConverterFactory(new FilterConverterFactory())
.addConverterFactory(new FieldsConverterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.validateEagerly(true)
.build();
Expand Down
Loading

0 comments on commit 6bde10a

Please sign in to comment.