celest-dev / celest

The Flutter cloud platform
https://celest.dev
Other
233 stars 11 forks source link

feat: 0.2.0 #51

Closed dnys1 closed 7 months ago

dnys1 commented 7 months ago

This release also adds a new way to control serialization called "custom overrides".

There are times when you want to use a type from a third-party package in an API, but it is not serializable and Celest complains when you do so. Because you do not control the package, you cannot make it serializable and you are stuck. Other times, the opinionated way Celest serializes (e.g. by using fromJson/toJson methods if available) doesn't align with how you've defined these APIs.

In both of these situations, custom overrides allow you to reimagine the types so that they can be serialized with Celest.

Consider a type called MyType which has fromJson/toJson methods you cannot control and which are causing errors with Celest. The simplest override of this type would look like this (defined in lib/models/overrides.dart):

import 'package:some_package/some_package.dart';

@override
extension type MyTypeOverride(MyType _) implements MyType {}

The @override annotation tells Celest to use this type instead of the original MyType when it encounters a MyType value at any point. The override applies globally and affects all instances of MyType when passing from client<->server. And since MyTypeOverride does not define fromJson/toJson methods, Celest will generate its own.

Custom overrides can do a lot more than override fromJson/toJson methods, though. They can redefine constructors, fields, and even field types. And custom overrides work for exception types too (define those in lib/exceptions/overrides.dart)!

Fixes

Deprecations

Breaking Changes

dnys1 commented 7 months ago

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.