Skip to content

Commit

Permalink
modify @withmetadata to filter strings
Browse files Browse the repository at this point in the history
  • Loading branch information
apkille committed Aug 11, 2024
1 parent a2327f7 commit 014efc7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using SymbolicUtils
import SymbolicUtils: Symbolic,_isone,flatten_term,isnotflat,Chain,Fixpoint,Prewalk,sorted_arguments
using TermInterface
import TermInterface: isexpr,head,iscall,children,operation,arguments,metadata,maketerm
import MacroTools
import MacroTools: namify, @capture

using LinearAlgebra
Expand Down Expand Up @@ -70,11 +71,11 @@ macro withmetadata(strct)
ex = quote $strct end
if @capture(ex, (struct T_{params__} fields__ end) | (struct T_{params__} <: A_ fields__ end))
struct_name = namify(T)
args = (namify(i) for i in fields)
args = (namify(i) for i in fields if !MacroTools.isexpr(i, String, :string))
constructor = :($struct_name{S}($(args...)) where S = new{S}($((args..., :(Metadata()))...)))
elseif @capture(ex, struct T_ fields__ end)
struct_name = namify(T)
args = (namify(i) for i in fields)
args = (namify(i) for i in fields if !MacroTools.isexpr(i, String, :string))
constructor = :($struct_name($(args...)) = new($((args..., :(Metadata()))...)))
else @capture(ex, struct T_ end)
struct_name = namify(T)
Expand Down
36 changes: 36 additions & 0 deletions test/test_metadata.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@testitem "Test metadata decoration" begin
using QuantumSymbolics: Metadata, @withmetadata

@withmetadata struct Foo1
a::Int
end
@test Foo1(2).metadata isa Metadata

@withmetadata struct Foo2
"hi"
a::Int
end
@test Foo2(2).metadata isa Metadata

@withmetadata struct Foo3{T<:Int}
"hi"
a::T
"hi"
b::T
end
@test Foo3{Int}(2, 3).metadata isa Metadata

@withmetadata struct Foo4{T<:Int} <: Integer
a::T
b::T
end
@test Foo4{Int}(2, 3).metadata isa Metadata

@withmetadata struct Foo5 <: Integer
a
end
@test Foo5(2).metadata isa Metadata

@withmetadata struct Foo6 <: Integer end
@test Foo6().metadata isa Metadata
end

0 comments on commit 014efc7

Please sign in to comment.