Skip to content

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

Closed
joshday wants to merge 3 commits into
mainfrom
jd/auto
Closed

joshday wants to merge 3 commits into
mainfrom
jd/auto

Conversation

@joshday

@joshday joshday commented Aug 30, 2023 •

Copy link
Copy Markdown

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

codecov Bot commented Aug 30, 2023 •

Copy link
Copy Markdown

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.

@quinnj

quinnj commented Oct 15, 2026

Copy link
Copy Markdown
Member

[comment generated via AI] Thanks for the proposal. StructTypes is in maintenance mode (JSON.jl 1.x builds on StructUtils instead) and only gets fixes now, so closing. JSON.jl 1.x handles abstract types with JSON.@choosetype.

@quinnj quinnj closed this Oct 15, 2026
@joshday
joshday deleted the jd/auto branch October 16, 2026 13:33
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.

2 participants