Get the name and the value of every field for an object

I am writing my objects to and from my database. Database columns and field names are matching, so for convenience sake I would like to get an array of fieldnames and an array of field values to pass to the SQL INSERT INTO command. Is this the proper way of doing this task in Julia? I was hoping for some version of the .function operator. Asking to help me learn the language better.

struct Security

security = Security("sdf", "w23423")

julia> fieldnames(jhal.Security)
(:ticker, :CUSIP)

julia> getfield(security, :CUSIP)

julia> map(field -> getfield(security, field), fieldnames(jhal.Security))
("sdf", "w23423")
1 Like

First of all, your approach seems reasonable :+1: probably similar to what I would have done as well.

There is a package which I actually found just when reseaching for this question :wink:
it is NamedTupleTools.jl

With that you could do

using NamedTupleTools
nt = ntfromstruct(security)

I quite like NamedTuples, they reduce sometimes the amount of structs one needs to define :smiley: so it might be convenient for you too. But maybe it is also overkill here :wink:

(By the way, you probably know it already but it could be that there are already packages which would help you with interfacing with SQL. In particular also DataFrames.jl in case that you haven’t seen it already.)

I think you can do

getfield.(security, fieldnames(jhal.Security))


getfield.(security, fieldnames(typeof(security)))

I’m getting a

ERROR: MethodError: no method matching length(::Security)

when trying. Seems

getfield.((security,), fieldnames(Security))

doesn’t raise exception. Is this a version issue?
(I’m on 1.8.0)

Maybe. I didn’t actually test it. I forgot about how structs have a surprising default broadcasting behavior.