Skip to content

Commit

Permalink
fix: versioning example
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Jun 6, 2024
1 parent 878eca8 commit 65fe26b
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,9 @@ public abstract partial class LogEntry : SystemLogEntry {
}
```

> [!TIP]
> Note that the `[Id]` attribute has been moved to `[BaseLogEntry]`.
To introduce a new version, you first need to create a common base type for all the versions.

We first rename the current `LogEntry` to `LogEntry1` and introduce a new abstract type, `LogEntry` which then extends the `SystemLogEntry` type that we don't have direct control over. Then, we simply update the original `LogEntry` model to inherit from `BaseLogEntry`.
We first rename the current `LogEntry` to `LogEntry1` and introduce a new abstract type which extends `SystemLogEntry` — a type that we don't have direct control over. Then, we simply update the `LogEntry1` model to inherit from the abstract `LogEntry`.

By default, instantiable introspective types have a default version of `1`. We will go ahead and add the `[Version]` attribute anyways to make it more clear.

Expand All @@ -236,7 +233,7 @@ public abstract partial class LogEntry : SystemLogEntry { }

// Used to be LogEntry, but is now LogEntry1.
[Meta, Version(1)]
public partial class LogEntry1 : BaseLogEntry {
public partial class LogEntry1 : LogEntry {
[Save("text")]
public required string Text { get; init; }

Expand All @@ -245,6 +242,9 @@ public partial class LogEntry1 : BaseLogEntry {
}
```

> [!TIP]
> Note that the `[Id]` attribute is only on the abstract base log entry type.
Finally, we can introduce a new version:

```csharp
Expand All @@ -255,7 +255,7 @@ public enum LogType {
}

[Meta, Version(2)]
public partial class LogEntry2 : BaseLogEntry {
public partial class LogEntry2 : LogEntry {
[Save("text")]
public required string Text { get; init; }

Expand All @@ -272,10 +272,10 @@ We can update the previous example by marking the first model as outdated:

```csharp
[Meta, Id("log_entry")]
public abstract partial class BaseLogEntry { }
public abstract partial class LogEntry { }

[Meta, Version(1)]
public partial class LogEntry : BaseLogEntry, IOutdated {
public partial class LogEntry1 : LogEntry, IOutdated {
[Save("text")]
public required string Text { get; init; }

Expand Down Expand Up @@ -310,7 +310,7 @@ var options = new JsonSerializerOptions {
Converters = { new IdentifiableTypeConverter(new Blackboard()) }
};

var model = JsonSerializer.Deserialize<BaseLogEntry>(json, options);
var model = JsonSerializer.Deserialize<LogEntry>(json, options);
```

## 🪝 Serialization Hooks
Expand Down Expand Up @@ -412,7 +412,7 @@ The serialization system has built-in support for a number of types. If a type i
[discord-badge]: https://raw.githubusercontent.com/chickensoft-games/chickensoft_site/main/static/img/badges/discord_badge.svg
[discord]: https://discord.gg/gSjaPgMmYW
[read-the-docs-badge]: https://raw.githubusercontent.com/chickensoft-games/chickensoft_site/main/static/img/badges/read_the_docs_badge.svg
[docs]: https://chickensoft.games/docsickensoft%20Discord-%237289DA.svg?style=flat&logo=discord&logoColor=white
[docs]: https://chickensoft.games/docs
[line-coverage]: Chickensoft.Serialization.Tests/badges/line_coverage.svg
[branch-coverage]: Chickensoft.Serialization.Tests/badges/branch_coverage.svg

Expand Down

0 comments on commit 65fe26b

Please sign in to comment.