StructTypes.jl
StructTypes.jl copied to clipboard
Add `@auto` macro to automatically provide StructTypes definitions for a type tree
The goal here is to remove a lot of the boilerplate necessary for dealing with abstract types:
abstract type Vehicle end
StructTypes.@auto Vehicle # Can be called *before* the concrete type definitions
struct Car <: Vehicle
make::String
model::String
seatingCapacity::Int
topSpeed::Float64
end
struct Truck <: Vehicle
make::String
model::String
payloadCapacity::Float64
end
c = Car("Mercedes-Benz", "S500", 5, 250.1)
s = JSON3.write(c)
JSON3.read(s, Vehicle) == c
Under the hood:
- A
__type__key is added to the serialized JSON (configurable via@auto MyType mysubtypekey. - I rely on
evalto get the deserialized type from the__type__. I examine theMeta.parse-edExprto order to determine whether it's safe toeval. I typically don't mess witheval, so I may have missed something that makes this unsafe.
Codecov Report
Patch coverage: 34.78% and project coverage change: -1.97% :warning:
Comparison is base (
9cca137) 75.31% compared to head (6e1b9f4) 73.34%.
Additional details and impacted files
@@ Coverage Diff @@
## main #97 +/- ##
==========================================
- Coverage 75.31% 73.34% -1.97%
==========================================
Files 2 2
Lines 401 424 +23
==========================================
+ Hits 302 311 +9
- Misses 99 113 +14
| Files Changed | Coverage Δ | |
|---|---|---|
| src/macros.jl | 65.21% <34.78%> (-30.44%) |
:arrow_down: |
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.