kassava icon indicating copy to clipboard operation
kassava copied to clipboard

Use callable instead of property?

Open thauk-copperleaf opened this issue 7 years ago • 2 comments

Instead of providing a list of properties, can I refer to a member of the class that's a callable function, which will return a the value to use for comparisons?

thauk-copperleaf avatar Feb 27 '18 01:02 thauk-copperleaf

Interesting...what use cases did you have in mind? Instance functions, extension functions?

By using callable you lose the ability to enforce that the callable's are actually compatible with the object's being compared (there's no receiver in the type signature like with KProperty), so you run the risk of getting IllegalArgumentException: object is not an instance of declaring class.

jamesbassett avatar Feb 27 '18 06:02 jamesbassett

I have a bunch of POKOs that have Hibernate annotations, and matching "mixin" classes that are used to serialize the POKOs with Jackson. An oversimplified example might look like this:

@Entity
class Foo {
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(
            name = "UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )
    @Column(name = "id", updatable = false, nullable = false)
    @JsonIgnore
    var id: UUID? = null

    @Column(name = "name")
    var name: String? = null

    fun getTransformedName(): String? {
        return name?.capitalize()
    }
}

@JsonPropertyOrder("name", "transformedName")
class FooMixin {
    @JsonProperty("transformedName", access = JsonProperty.Access.READ_ONLY)
    abstract fun getTransformedName(): String?
}

Let's say I need to compare two Foo entities for functional equality. That means I want to skip the id field, but I want to compare the name field, and also what is essentially a transformed name field.

thauk-copperleaf avatar Feb 27 '18 07:02 thauk-copperleaf