From 57d63725383216888bf40fe54e92c30d1a7e0540 Mon Sep 17 00:00:00 2001 From: Andrew Kille <68079167+apkille@users.noreply.github.com> Date: Sun, 11 Aug 2024 18:11:42 -0400 Subject: [PATCH] Modify @withmetadata to filter strings in field definitions (#77) Co-authored-by: Stefan Krastanov --- CHANGELOG.md | 4 ++++ Project.toml | 2 +- src/QSymbolicsBase/QSymbolicsBase.jl | 5 ++-- test/test_metadata.jl | 36 ++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test/test_metadata.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index cbf894c..0086a7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # News +## v0.4.2 - 2024-08-11 + +- `@withmetadata` now supports inline docstrings for struct fields + ## v0.4.1 - 2024-08-11 - Minor documentation improvements. diff --git a/Project.toml b/Project.toml index 8090b9c..b5021c0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "QuantumSymbolics" uuid = "efa7fd63-0460-4890-beb7-be1bbdfbaeae" authors = ["QuantumSymbolics.jl contributors"] -version = "0.4.1" +version = "0.4.2" [deps] Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" diff --git a/src/QSymbolicsBase/QSymbolicsBase.jl b/src/QSymbolicsBase/QSymbolicsBase.jl index f8a4ec3..8825cfe 100644 --- a/src/QSymbolicsBase/QSymbolicsBase.jl +++ b/src/QSymbolicsBase/QSymbolicsBase.jl @@ -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 @@ -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) diff --git a/test/test_metadata.jl b/test/test_metadata.jl new file mode 100644 index 0000000..5af08ff --- /dev/null +++ b/test/test_metadata.jl @@ -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 \ No newline at end of file