Skip to content

Conversation

gdotdesign
Copy link
Member

@gdotdesign gdotdesign commented Mar 23, 2025

This PR adds the ability to automatically serialize custom types into an opinionated format.

Most of the time, developers don't really care what format the data is in, they just want to persist it somewhere (like local storage) and load it back. Right now, they have to lean about custom decoders and encoders if the data is not a record (we have automatic serialization for those) and that's a roadblock for that use case.

After this, encode and decode will work on variant types:

type User {
  LoggedIn(name : String, age: Number)
  LoggedOut
}

let loggedInUser =
  User.LoggedIn(name: "Joe", age: 38)

// Encodes it to an object: { type: "User.LoggedIn", value: ["Joe", 38] }
let encoded =
  encode loggedInUser 

(decode encoded as User) == loggedInUser

The serialized structure is:

{
  type: ...
  value: [...]
}
  • type is the fully qualified name of the variant
  • value contain the parameters in defined order

The PR also contains:

  • A fix for styles helper function
  • A fix for normalize event tests
  • Formatting of the runtime files
  • Test for the runtime decoders

@gdotdesign gdotdesign added enhancement New feature or request language Language feature labels Mar 23, 2025
@gdotdesign gdotdesign requested a review from Sija March 23, 2025 12:10
@gdotdesign gdotdesign self-assigned this Mar 23, 2025
@gdotdesign gdotdesign added this to the 0.24.0 milestone Mar 23, 2025
@gdotdesign gdotdesign marked this pull request as ready for review March 23, 2025 12:18
@Sija Sija changed the title Automatically serliaze custom types. Automatically serialize custom types Mar 23, 2025
@Sija
Copy link
Member

Sija commented Mar 23, 2025

In vast majority of the cases, you don't control the backend, so the serialization format must be universal and even if you do, you don't want to tailor it for the specific frontend and its serialization format. Therefore I'd suggest to rewrite this into a general key: value format, which should cover majority of the use cases.

@gdotdesign
Copy link
Member Author

There is no universal format for serializing ADTs. In Mint, they come in three formats:

type ADT {
  // With labelled parameters
  Labelled(name : String, age : Number, active : Bool)

  // With parameters (no labels)
  Parameters(String, Number, Bool)

  // Just a tag no additional values
  Empty 
}

So key: value can only cover one of the three formats. Having different serialization based on the format would be confusing IMHO.

Mint also have records:

type User {
  active : Bool,
  name: String,
  age: Number,
}

They do serialize to key: value format.

As far as I can tell no mainstream language which have ADTs have automatic serialization for them, and even if they do it will never be 100% compatible. The point of this feature is to allow persisting and loading back Mint data easily.

@gdotdesign gdotdesign merged commit bfc971f into master Apr 3, 2025
3 checks passed
@gdotdesign gdotdesign deleted the auto-seralize-types branch April 3, 2025 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request language Language feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants