From fde6b1f5206ee4f615ae42eb27dc6946ce9e15df Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 16 Feb 2024 08:38:04 +0100 Subject: [PATCH 1/2] store more useful information in symbolic struct --- src/struct.jl | 22 ++++++++++++++++++++++ test/struct.jl | 19 +++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/struct.jl b/src/struct.jl index e6ff592f9..7aaa54307 100644 --- a/src/struct.jl +++ b/src/struct.jl @@ -68,9 +68,31 @@ function decodetyp(typ::TypeT) end struct Struct + juliatype::DataType v::Vector{StructElement} end + +""" + symstruct(T) + +Create a symbolic struct from a given type `T`. +""" +function symstruct(T) + elems = map(fieldnames(T)) do fieldname + StructElement(fieldtype(T, fieldname), fieldname) + end |> collect + Struct(T, elems) +end + +""" + juliatype(s::Struct) + +Get the Julia type that `s` is representing. +""" +juliatype(s::Struct) = getfield(s, :juliatype) +getelements(s::Struct) = getfield(s, :v) + function Base.getproperty(s::Struct, name::Symbol) v = getfield(s, :v) idx = findfirst(x -> nameof(x) == name, v) diff --git a/test/struct.jl b/test/struct.jl index b509abc51..adfeb1ee7 100644 --- a/test/struct.jl +++ b/test/struct.jl @@ -1,5 +1,5 @@ using Test, Symbolics -using Symbolics: StructElement, Struct, operation, arguments +using Symbolics: StructElement, Struct, operation, arguments, symstruct handledtypes = [Int8, Int16, @@ -17,9 +17,13 @@ for t in handledtypes end @variables t x(t) +struct Fisk + a::Int8 + b::Int +end a = StructElement(Int8, :a) b = StructElement(Int, :b) -s = Struct([a, b]) +s = Struct(Fisk, [a, b]) sa = s.a sb = s.b @test operation(sa) === getfield @@ -28,6 +32,8 @@ sb = s.b @test operation(sb) === getfield @test arguments(sb) == Any[s, 2] @test arguments(sb) isa Any +@test juliatype(s) == Fisk +@test symstruct(Fisk) == s sa1 = (setproperty!(s, :a, UInt8(1))) @test operation(sa1) === setfield! @@ -38,3 +44,12 @@ sb1 = (setproperty!(s, :b, "hi")) @test operation(sb1) === setfield! @test arguments(sb1) == Any[s, 2, "hi"] @test arguments(sb1) isa Any + +struct Jörgen + a::Int + b::Float64 +end + +ss = symstruct(Jörgen) + +@test getfield(ss, :v) == [StructElement(Int, :a), StructElement(Float64, :b)] \ No newline at end of file From 9d8d57736a2d00b5fe4ae0d2dec5ae8c7d5f7672 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 19 Feb 2024 19:52:58 +0100 Subject: [PATCH 2/2] test `symstruct` --- test/struct.jl | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/test/struct.jl b/test/struct.jl index adfeb1ee7..c9b178bc6 100644 --- a/test/struct.jl +++ b/test/struct.jl @@ -1,5 +1,5 @@ using Test, Symbolics -using Symbolics: StructElement, Struct, operation, arguments, symstruct +using Symbolics: StructElement, Struct, operation, arguments, symstruct, juliatype handledtypes = [Int8, Int16, @@ -23,17 +23,19 @@ struct Fisk end a = StructElement(Int8, :a) b = StructElement(Int, :b) +for s in [Struct(Fisk, [a, b]), symstruct(Fisk)] + sa = s.a + sb = s.b + @test operation(sa) === getfield + @test arguments(sa) == Any[s, 1] + @test arguments(sa) isa Any + @test operation(sb) === getfield + @test arguments(sb) == Any[s, 2] + @test arguments(sb) isa Any + @test juliatype(s) == Fisk +end + s = Struct(Fisk, [a, b]) -sa = s.a -sb = s.b -@test operation(sa) === getfield -@test arguments(sa) == Any[s, 1] -@test arguments(sa) isa Any -@test operation(sb) === getfield -@test arguments(sb) == Any[s, 2] -@test arguments(sb) isa Any -@test juliatype(s) == Fisk -@test symstruct(Fisk) == s sa1 = (setproperty!(s, :a, UInt8(1))) @test operation(sa1) === setfield! @@ -52,4 +54,4 @@ end ss = symstruct(Jörgen) -@test getfield(ss, :v) == [StructElement(Int, :a), StructElement(Float64, :b)] \ No newline at end of file +@test getfield(ss, :v) == [StructElement(Int, :a), StructElement(Float64, :b)]