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

Add @auto macro to automatically provide StructTypes definitions for a type tree #97

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

joshday
Copy link

@joshday joshday commented Aug 30, 2023

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:

  1. A __type__ key is added to the serialized JSON (configurable via @auto MyType mysubtypekey.
  2. I rely on eval to get the deserialized type from the __type__. I examine the Meta.parse-ed Expr to order to determine whether it's safe to eval. I typically don't mess with eval, so I may have missed something that makes this unsafe.

@codecov
Copy link

codecov bot commented Aug 30, 2023

Codecov Report

Patch coverage: 34.78% and project coverage change: -1.97% ⚠️

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%) ⬇️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Successfully merging this pull request may close these issues.

1 participant