1

I have a redis entity using a kotlin data class and its Id should be a combination of few other fields from the same class. Can this be achieved by defining setter for Id field instead of computing it outside of data class?

@RedisHash("Game")
data class Game(
  @Id
  val generatedId: String = "Default_ID",
  val name: String,
  val location: String,
  val homeTeam: String,
  val awayTeam: String

)

// want something like this

var generatedId : String = "DEFAULT_ID"
  get() = "${name}${location}"

// or even better

var generated_Id : String =  "${name}${location}"

1 Answer 1

2

Did you try to do something like this?

@RedisHash("Game")
data class Game(
    val name: String,
    val location: String,
    val homeTeam: String,
    val awayTeam: String,
    @Id
    val generatedId: String = "${name}${location}"
)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a ton, I was sure I was missing something basic there .. it was a matter of moving generated after both name and location :-).
a followup question is there a way I can restrict setting this value from outside? I imagine writing a setter that throws an exception?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.