Skip to content

Commit

Permalink
macro: named hidden fields
Browse files Browse the repository at this point in the history
Allow hide named field
  • Loading branch information
miRoox committed Nov 19, 2022
1 parent 97ae5d3 commit ceea2e8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@ function dumpstructinfo(m::Module, structdef::Expr)
error("invalid syntax: please specify a type/construct for $(node).")
elseif node isa Expr && node.head == :(::)
rawtype = node.args[end]
if length(node.args) == 2 # x::Int
push!(infos, FieldInfo(m, node.args[1], rawtype, line))
if length(node.args) == 2
name = node.args[1]
if name isa Symbol # x::Int
push!(infos, FieldInfo(m, name, rawtype, line))
elseif name isa Expr && name.head == :vect && only(name.args) isa Symbol # [x]::Int
push!(infos, FieldInfo(m, only(name.args), rawtype, line; hidden=true))
else
error("invalid syntax: invalid field $(name).")
end
elseif length(node.args) == 1 # ::Padded(4)
push!(infos, FieldInfo(m, gensym("(anonymous)"), rawtype, line; hidden=true))
else
Expand Down
39 changes: 37 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,16 @@ end
pixel::SizedArray(UInt8, this.height, this.width)
end
end
@testset "expand pass" for ex in [structonly, withname, withoverwrite]
withhidden = quote
@construct struct Bitmap <: AbstractImage
[signature]::Const(b"BMP")
[width]::Overwrite(UInt32, convert(UInt32, size(this.pixel, 2)))
[height]::Overwrite(UInt32, convert(UInt32, size(this.pixel, 1)))
::Padded(8)
pixel::SizedArray(UInt8, this.height, this.width)
end
end
@testset "expand pass" for ex in [structonly, withname, withoverwrite, withhidden]
@test @capture @show(macroexpand(@__MODULE__, ex)) begin
begin
doc_
Expand Down Expand Up @@ -681,6 +690,16 @@ end
end
end
)
invalidfield = (
ErrorException,
quote
@construct struct Bitmap <: AbstractImage
[]::Const(b"BMP")
width::UInt32
height::UInt32
end
end
)
deducefailed = (
ErrorException,
quote
Expand All @@ -704,7 +723,7 @@ end
end
end
)
@testset "expand error" for (err, ex) in [notstruct, missingtype, deducefailed, nodefaultconstruct]
@testset "expand error" for (err, ex) in [notstruct, missingtype, invalidfield, deducefailed, nodefaultconstruct]
@static if Base.VERSION >= v"1.7-"
@test_throws err macroexpand(@__MODULE__, ex)
else
Expand Down Expand Up @@ -734,5 +753,21 @@ end
@test_throws EOFError deserialize(Bitmap1, b"BMP\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03")
@test_throws ValidationError deserialize(Bitmap1, b"PMB\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03\x09")
end
@testset "Bitmap2" begin
@construct struct Bitmap2 <: AbstractImage
::Padded(Const(b"BMP"), 4)
[width]::Overwrite(UInt16le, _ -> convert(UInt16, size(this.pixel, 2)))
[height]::Overwrite(UInt16le, _ -> convert(UInt16, size(this.pixel, 1)))
pixel::SizedArray(UInt8, this.height, this.width)
end
@test Bitmap2 <: AbstractImage
@test fieldnames(Bitmap2) == (:pixel,)
@test fieldtype(Bitmap2, :pixel) == Matrix{UInt8}
@test_broken estimatesize(Bitmap2) == UnboundedSize(4 + 2 * sizeof(UInt16)) # known issue: should redesign container
@test serialize(Bitmap2(UInt8[1 2 3; 7 8 9])) == b"BMP\x00\x03\x00\x02\x00\x01\x07\x02\x08\x03\x09"
@test deserialize(Bitmap2, b"BMP\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03\x09").pixel == UInt8[1 2 3; 7 8 9]
@test_throws EOFError deserialize(Bitmap2, b"BMP\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03")
@test_throws ValidationError deserialize(Bitmap2, b"PMB\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03\x09")
end
end
end

0 comments on commit ceea2e8

Please sign in to comment.