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

Make Proxy fields optional #792

Closed
ilyakooo0 opened this issue Jul 17, 2020 · 4 comments
Closed

Make Proxy fields optional #792

ilyakooo0 opened this issue Jul 17, 2020 · 4 comments
Assignees

Comments

@ilyakooo0
Copy link

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)

@bergmark
Copy link
Collaborator

bergmark commented Aug 7, 2020

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.

@ilyakooo0
Copy link
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

@phadej
Copy link
Collaborator

phadej commented Oct 21, 2020

#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.

@phadej phadej self-assigned this Feb 14, 2021
phadej added a commit that referenced this issue Jun 15, 2023
- Add combinators for using omit* stuff in manually written instances
- Add Manual tests
- Cleanup OptionalFields.Common
- Fix TH and Generics
- Add combinators ToJSON1/2 and FromJSON1/2
- Const, Identity, Tagged and other newtypes
- Fix #687. ToJSON1 respects omitting fields
- Fix #571. Introduce allowOmittedFields to Generics/TH options.
- Resolve #792. () and Proxy can be omitted
@phadej
Copy link
Collaborator

phadej commented Jun 15, 2023

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.

phadej added a commit that referenced this issue Jun 15, 2023
- Add combinators for using omit* stuff in manually written instances
- Add Manual tests
- Cleanup OptionalFields.Common
- Fix TH and Generics
- Add combinators ToJSON1/2 and FromJSON1/2
- Const, Identity, Tagged and other newtypes
- Fix #687. ToJSON1 respects omitting fields
- Fix #571. Introduce allowOmittedFields to Generics/TH options.
- Resolve #792. () and Proxy can be omitted
phadej added a commit that referenced this issue Jun 15, 2023
- Add combinators for using omit* stuff in manually written instances
- Add Manual tests
- Cleanup OptionalFields.Common
- Fix TH and Generics
- Add combinators ToJSON1/2 and FromJSON1/2
- Const, Identity, Tagged and other newtypes
- Fix #687. ToJSON1 respects omitting fields
- Fix #571. Introduce allowOmittedFields to Generics/TH options.
- Resolve #792. () and Proxy can be omitted
@phadej phadej closed this as completed in b06eb2c Jun 19, 2023
JonathanLorimer pushed a commit to JonathanLorimer/aeson that referenced this issue Aug 7, 2023
- Add combinators for using omit* stuff in manually written instances
- Add Manual tests
- Cleanup OptionalFields.Common
- Fix TH and Generics
- Add combinators ToJSON1/2 and FromJSON1/2
- Const, Identity, Tagged and other newtypes
- Fix haskell#687. ToJSON1 respects omitting fields
- Fix haskell#571. Introduce allowOmittedFields to Generics/TH options.
- Resolve haskell#792. () and Proxy can be omitted
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