0

hope I can get some valued assistance with a little issue I'm trying to work.

I've got an API endpoint which is sending data with the following request schema:

{
  "type": String,
  "coordinates": [
    0.949492,
    48.77163
  ]
}

As can be seen; the coordinates from the search are provided as two INT values, without parameters.

I'm trying to create an automated test for this, and I've put the above in a data class so it can be used all over the suite as-needed.

My data class is currently looking like the below example, but I don't know how to properly define a list for coordinates without a val or var parameter. I've defined it as a var called "list" for now so it stops throwing compilation errors. How should I be representing this list of coordinates?

data class SearchRequest(
    val type: String,
    val coordinates: List<Coordinates>
)


data class Coordinates(
    var list: Int
)
3
  • 1
    It looks like it's just a list of floating point numbers, so you could define val coordinates: List<Float> in your first class and you don't need a second class. Commented Oct 10, 2022 at 15:23
  • Thanks very much for the feedback. Also suggested by another, so definitely the way to go! Thanks again. Commented Oct 10, 2022 at 15:31
  • 1
    You might have realised already, but a data class requires at least one parameter, and all its constructor parameters need to be a val or var. That's what makes it a data class - all its data is the stuff in the constructor params, and that's what's used in its generated copy function, as well as its equals/hashCode/toString overrides. So if you ever do want a class with normal, non-val/var constructor parameters, you need a regular class! (Or maybe a builder function that can set up a data class after constructing it, using your extra params to poke at it) Commented Oct 10, 2022 at 18:01

2 Answers 2

1

The second parameter is a list of Float values, there is no need to create a separate class for that, Float can be used:

data class SearchRequest(
    val type: String,
    val coordinates: List<Float>
)
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh brilliant. So simple, so easy. Your help and answer are very much appreciated!
0

In addition to the answers above, you can also download a plugin from android studio that does this for you by just pasting the API's JSON format. The plugin name is JSON to Kotlin class converter, I think.👍🏻

Comments

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.