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

Add BiDiLensSpec defaulted with factory method #1053

Closed
krissrex opened this issue Feb 5, 2024 · 5 comments
Closed

Add BiDiLensSpec defaulted with factory method #1053

krissrex opened this issue Feb 5, 2024 · 5 comments

Comments

@krissrex
Copy link
Contributor

krissrex commented Feb 5, 2024

I would appreciate a defaulted which is re-evaluated every time, so I can use e.g. Instant.now(), LocalDate.now() etc.

I currently do this:

val datetime = Query.localDate(DateTimeFormatter.ISO_LOCAL_DATE)
        .optional(
            "date",
            description = "A date for something"
        )

// Usage
val foo = datetime(req) ?: LocalDate.now(DEFAULT_ZONE)

And would appreciate if I could do this:

val datetime = Query.localDate(DateTimeFormatter.ISO_LOCAL_DATE)
        .defaulted(
            "date",
            { LocalDate.now(DEFAULT_ZONE) },
            description = "A date for something"
        )

// Usage
val foo = datetime(req)

The name does not need to be defaulted, if this collides with existing signatures. Other names could be: defaultedLazy (I don't want it lazy, it's active/recompute every time), defaultFactory, defaultedWithFactory, defaultedComputed, computeDefault, etc..


I tried adding the functionality in my own codebase, but I failed because paramMeta was private, and get and set as well, so extension functions on BiDiLensSpec or inheritance of BiDiLens didn't quickly solve my problem.

@krissrex
Copy link
Contributor Author

krissrex commented Feb 5, 2024

Label: feature request

@dzappold
Copy link
Contributor

dzappold commented Feb 5, 2024

Have you tried to use a Lens as second parameter?
I guess it could look similar to Lens(myMeta) { _ -> LocalTime.now() }

@krissrex
Copy link
Contributor Author

Have you tried to use a Lens as second parameter? I guess it could look similar to Lens(myMeta) { _ -> LocalTime.now() }

Yes, but I might have done it wrong. I struggled with the constructor, because I couldn't create a Meta object.
I looked at the official defaulted, which did:

        defaulted(name, Lens(Meta(false, location, paramMeta, name, description)) { default }, description)

, but I dont have access to location and paramMeta when I want to create myMeta.

  Query.instant().defaulted("start", Lens(meta = ???, { _ -> Instant.now() }))

The source code isn't obvious either, so I have no clue what location and meta are, and if I can construct them myself easily, or what data should even go into them. If you have any advice, I would appreciate it.

@krissrex
Copy link
Contributor Author

It seems the location and paramMeta come from the Query object, so something like this?

  Query.instant()
      .defaulted(
          "start",
          Lens(
              meta =
                  Meta(
                      required = false,
                      location = Query.location,
                      paramMeta = ParamMeta.StringParam,
                      name = "start",
                      description = "My description of start"),
              { _ -> Instant.now() }))

Quite verbose compared to e.g. a Query.instant().defaultFactory("start", { Instant.now() })

@daviddenton
Copy link
Member

I've made the defaulted mechanism take a LensExtractor (Request) -> T and made it a fun interface, so you can now do: ```
Query.int().defaulted("foo", { 123 })


Will be out in the next version

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

3 participants