Skip to content

Commit

Permalink
Modify @withmetadata to filter strings in field definitions (#77)
Browse files Browse the repository at this point in the history

Co-authored-by: Stefan Krastanov <[email protected]>
  • Loading branch information
apkille and Krastanov authored Aug 11, 2024
1 parent a2327f7 commit 57d6372
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
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

2 comments on commit 57d6372

@Krastanov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/112912

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.2 -m "<description of version>" 57d63725383216888bf40fe54e92c30d1a7e0540
git push origin v0.4.2

Please sign in to comment.