Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Having trouble extracting a selectManyField #55

Closed
futurechimp opened this issue Apr 22, 2015 · 15 comments
Closed

Having trouble extracting a selectManyField #55

futurechimp opened this issue Apr 22, 2015 · 15 comments

Comments

@futurechimp
Copy link

I'm building a little survey API which deals with Questions and Responses to those questions. Assuming I've got a responseForm with a selectManyField which looks something like this:

class ResponseForm {
  def apply(data: Response) = {
    responseForm(data)
  }

  def responseForm = {
    form[Response](f => List(
      f.field(_.questionId),
      f.selectManyField(_.responses)(identity)
        .possibleValues(_ => List("tulips", "roses", "daffodils"))
    ))
  }
}

And I'm answer a Question such as "What sorts of flowers do you like?". I have a Response case class which looks like this:

case class Response(id: ObjectId, questionId: ObjectId, responses: Set[String])

I am having trouble extracting a Response object from the following input:

{
  "questionId": "55375f46d4c637a207558830",
  "responses": ["tulips", "roses"]
}

The json4s parsedBody of the incoming JSON looks like this:

org.json4s.JValue = JObject(List((questionId,JString(55375f46d4c637a207558830)), (responses,JArray(List(JString(tulips),JString(roses))))))

However the Supler extraction doesn't pull out the values for responses and put them in the Set of responses.

We do:

val form = ResponseForm.responseForm.withNewEmpty.applyJSONValues(parsedBody)

We end up with:

Response(null,55375f46d4c637a207558830,Set())

We would expect:

Response(null,55375f46d4c637a207558830,Set(tulips, roses))

Are we doing something wrong? We're using Supler 0.3.0-SNAPSHOT.

@szimano
Copy link
Member

szimano commented Apr 22, 2015

Can you please try the same with 0.4.0-SNAPSHOT?

@futurechimp
Copy link
Author

Will do.

@futurechimp
Copy link
Author

0.4.0-SNAPSHOT has the same behavior.

@futurechimp
Copy link
Author

I can put up a demo project to show what's going on, if that'll help. I'm not clear whether we are Doing It Wrong or whether there's a bug.

@szimano
Copy link
Member

szimano commented Apr 22, 2015

that would be awesome, if you could put it somewhere on github. thanks :)

@futurechimp
Copy link
Author

On it. Back in 5.

@futurechimp
Copy link
Author

@szimano
Copy link
Member

szimano commented Apr 23, 2015

on it

@szimano
Copy link
Member

szimano commented Apr 23, 2015

ok, looked at it and the thing is that if you have a select field, then the possible values are sent with indexes, not the actual ones - so you must send something like this:

{
                    "questionId": "55375f46d4c637a207558830",
                    "responses": [
                      "0", "1"
                    ]
                  }

which will then println

Response(,55375f46d4c637a207558830,Set(yes, no))

@szimano
Copy link
Member

szimano commented Apr 23, 2015

but then, client part of supler should be sending them properly - if that does not work, can you please publish your webapp?

@futurechimp
Copy link
Author

Aha! I see.

We are only building the API right now, we haven't progressed to the point of building an HTML client for it (and it's not totally clear at the moment what clients will exist).

The use of indexes does work, e.g. this:

  def main(args: Array[String]) {
    val input = """{
                    "questionId": "55375f46d4c637a207558830",
                    "responses": [
                      "0"
                    ]
                  }"""

    val parsedBody = parse(input)
    val form = ResponseForm.responseForm.withNewEmpty.applyJSONValues(parsedBody)
    println(form.obj)
  }

Generates:

Response(,55375f46d4c637a207558830,Set(yes))

So I can see what the problem was. Supler needs to be able to tie its allowable values to incoming data via indexes. I was under the impression that it'd just check whether input was in the possibleValues list (which might be easier for non-Supler clients to deal with).

@szimano, thanks very much for the help. Now I understand what's going on.

@szimano
Copy link
Member

szimano commented Apr 23, 2015

Glad this helped.

You are right, the indexes were the interim solution that have lasted till now. I've created #56 for this.

Just out of curiosity - why are you planning on building your own HTML client? What is missing in the one we have created?

@futurechimp
Copy link
Author

It's not that we wouldn't use Supler for the front-end, just that we're not sure at the moment whether we'll be building an Android client, and iOS client, a SPA client, or what - we're in the very early stages and are just getting moving with a working API.

We were discussing things internally, there are a couple of projects we've done in the past where Supler or something like it would really have helped us, so we'll definitely be trying out the front-end stuff soon.

@szimano
Copy link
Member

szimano commented Apr 23, 2015

ok, fair enough.

once i'm done with modals, iOS and/or android clients are next on my plate.

@adamw
Copy link
Member

adamw commented Apr 27, 2015

You can provide a customid for a select value using SelectOneField.idForValue where you have to provide a function U => I where U is the type of the selected value, and I some basic type (Int, String, etc.).

If an idForValue is not provided, list indexes will be used (which I think was the problem here). Doesn't this solve #56?

I'll have to update the docs later :)

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

No branches or pull requests

3 participants