Skip to content

Make Proxy fields optional #792

Closed
Closed

Description

@iko-soy

There is special treatment of Maybe when deriving instances using Generic -- Maybe fields become optional.

I think it might make sense to apply the same special treatment to the Proxy type -- Proxy fields should probably also be optional -- they can only have one value after all. (And requiring a null field seems a bit unnecessary)

Activity

  1. bergmark commented on Aug 7, 2020

    @bergmark
    Collaborator

    AFAIK, Proxy is the only way to do a strict null check without writing custom instances so this may be a breaking change for some users. If we introduce e.g. a data Null = Null for this purpose then we can refer people to that instead. I'd like to have a module containing types that are only meant to be used to force JSON representations, it's not straight-forward to know to use Proxy here. We already have DotNetTime which would fit in there as well, and speaking of time we've had other requests for specific serializations for them. E.g. only allowing millisecond precision for UTCTime, but we want to allow more precision there, a ms precision type would be nice to have.

  2. iko-soy commented on Oct 21, 2020

    @iko-soy
    Author

    I would like to provide my motivation for wanting this change.

    It is sometimes necessary to have a subset of the fields of a structure returned from the API. While it is possible to define two separate structures, it would be easier to have one parametrized structure.

    data Person f = Person
      { name :: Text
      , age :: Natural
      , occupation :: f Text
      , address :: f Text
      , friends :: f [Person]
      , pets :: f [Pet]
      }
    
    type FullPerson = Person Identity
    
    type PersonSummary = Person Proxy

    It is currently impossible to define a custom f to get the desired behavior -- any FromJSON instance would require the field being present, the instance itself would only have the opportunity to look at the value of the field.

    I would also be happy if we could introduce a custom functor for this purpose which would have special treatment. Changing the semantics of Proxy might not be such a good idea, and it wouldn't be obvious to newcomers why Proxy of all things would get special treatment.

    data Absent a = Absent
  3. phadej commented on Oct 21, 2020

    @phadej
    Collaborator

    #564 would solve that, by having a specific instances for field needs, we can make Maybe and Proxy just have own instances, and not try to hack it all into Generic and TH machinery in ad-hoc manner.

  4. self-assigned this
    on Feb 14, 2021
  5. added 2 commits that reference this issue on Jun 15, 2023
    8cf35da
    3797f8f
  6. phadej commented on Jun 15, 2023

    @phadej
    Collaborator

    The #1038 will make omitting () and Proxy possible.

    Proxy is the only way to do a strict null check without writing custom instances

    Using omitNothingFields and allowOmittedFields users can control behavior in their ToJSON/FromJSON instances.

    If there are some who want to allow omitted Maybe fields but do null check on some in the same record, they should probably write a manual instance. That behavior doesn't seem very uniform.

  7. added 3 commits that reference this issue on Jun 15, 2023
    d835b9d
    f0c4476
    b06eb2c
  8. added a commit that references this issue on Aug 7, 2023
    83363fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions