diff --git a/Project.toml b/Project.toml index 3ceac3f..9cd8803 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Legolas" uuid = "741b9549-f6ed-4911-9fbf-4a1c0c97f0cd" authors = ["Beacon Biosignals, Inc."] -version = "0.5.5" +version = "0.5.6" [deps] Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45" diff --git a/src/schemas.jl b/src/schemas.jl index 1e5ffa4..ff4591c 100644 --- a/src/schemas.jl +++ b/src/schemas.jl @@ -461,16 +461,18 @@ function _generate_record_type_definitions(schema_version::SchemaVersion, record field_definitions = Expr[] field_assignments = Expr[] for (fname, ftype) in pairs(record_fields) - fdef = :($fname::$(Base.Meta.quot(ftype))) + T = Base.Meta.quot(ftype) + fdef = :($fname::$T) info = get(declared_field_infos, fname, nothing) if !isnothing(info) - fstmt = info.statement if info.parameterize T = Symbol("_", string(fname, "_T")) push!(type_param_defs, :($T <: $(info.type))) push!(names_of_parameterized_fields, fname) fdef = :($fname::$T) - fstmt = :($fname = $(fstmt.args[2])) + fstmt = :($fname = $(info.statement.args[2])) + else + fstmt = :($fname = convert($T, $(info.statement.args[2]))::$T) end push!(field_assignments, fstmt) end diff --git a/test/runtests.jl b/test/runtests.jl index 8f1984e..dd50491 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -442,3 +442,22 @@ end # overwriting another module's reserved schema name is disallowed @test_throws ArgumentError("A schema with this name was already declared by a different module: $A") @schema("a.cross", Cross) + +##### +##### local field variable handling in record constructors (ref https://github.com/beacon-biosignals/Legolas.jl/issues/76) +##### + +Legolas.@schema "unconstrained-field" UnconstrainedField + +Legolas.@version UnconstrainedFieldV1 begin + field::Any +end + +Legolas.@schema "constrained-field" ConstrainedField + +Legolas.@version ConstrainedFieldV1 > UnconstrainedFieldV1 begin + field::Int = parse(Int, field) +end + +c = ConstrainedFieldV1(field = "1") +@test c.field == 1