Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use callable instead of property? #4

Open
thauk-copperleaf opened this issue Feb 27, 2018 · 2 comments
Open

Use callable instead of property? #4

thauk-copperleaf opened this issue Feb 27, 2018 · 2 comments

Comments

@thauk-copperleaf
Copy link

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?

@jamesbassett
Copy link
Contributor

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.

@thauk-copperleaf
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants