Skip to content

Commit

Permalink
fix quoting issue (#92)
Browse files Browse the repository at this point in the history
* fix quoting issue

* fix tests
  • Loading branch information
ericphanson authored Jun 12, 2023
1 parent b86444c commit 3e4db06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Legolas"
uuid = "741b9549-f6ed-4911-9fbf-4a1c0c97f0cd"
authors = ["Beacon Biosignals, Inc."]
version = "0.5.10"
version = "0.5.11"

[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand Down
38 changes: 19 additions & 19 deletions src/schemas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ macro schema(schema_name, schema_prefix)
return quote
# This approach provides some safety against accidentally replacing another module's schema's name,
# without making it annoying to reload code/modules in an interactice development context.
m = Legolas._schema_declared_in_module(Val(Symbol($schema_name)))
m = $Legolas._schema_declared_in_module(Val(Symbol($schema_name)))
if m isa Module && string(m) != string(@__MODULE__)
throw(ArgumentError(string("A schema with this name was already declared by a different module: ", m)))
else
Legolas._schema_declared_in_module(::Val{Symbol($schema_name)}) = @__MODULE__
$Legolas._schema_declared_in_module(::Val{Symbol($schema_name)}) = @__MODULE__
if !isdefined(@__MODULE__, :__legolas_schema_name_from_prefix__)
$(esc(:__legolas_schema_name_from_prefix__))(::Val) = nothing
end
Expand Down Expand Up @@ -464,11 +464,11 @@ function _generate_schema_version_definitions(schema_version::SchemaVersion, par
end
quoted_schema_version_type = Base.Meta.quot(typeof(schema_version))
return quote
@inline Legolas.declared(::$quoted_schema_version_type) = true
@inline Legolas.identifier(::$quoted_schema_version_type) = $identifier_string
@inline Legolas.parent(::$quoted_schema_version_type) = $(Base.Meta.quot(parent))
Legolas.required_fields(::$quoted_schema_version_type) = $required_field_names_types
Legolas.declaration(::$quoted_schema_version_type) = $(Base.Meta.quot(schema_version_declaration))
@inline $Legolas.declared(::$quoted_schema_version_type) = true
@inline $Legolas.identifier(::$quoted_schema_version_type) = $identifier_string
@inline $Legolas.parent(::$quoted_schema_version_type) = $(Base.Meta.quot(parent))
$Legolas.required_fields(::$quoted_schema_version_type) = $required_field_names_types
$Legolas.declaration(::$quoted_schema_version_type) = $(Base.Meta.quot(schema_version_declaration))
end
end

Expand Down Expand Up @@ -658,9 +658,9 @@ function _generate_record_type_definitions(schema_version::SchemaVersion, record
$outer_constructor_definitions
$base_overload_definitions
$arrow_overload_definitions
Legolas.record_type(::$(Base.Meta.quot(typeof(schema_version)))) = $R
Legolas.schema_version_from_record(::$R) = $schema_version
Legolas._schema_version_from_record_type(::Type{<:$R}) = $schema_version
$Legolas.record_type(::$(Base.Meta.quot(typeof(schema_version)))) = $R
$Legolas.schema_version_from_record(::$R) = $schema_version
$Legolas._schema_version_from_record_type(::Type{<:$R}) = $schema_version
end
end

Expand Down Expand Up @@ -805,26 +805,26 @@ macro version(record_type, required_fields_block)
throw(SchemaVersionDeclarationError(string("missing prior `@schema` declaration for `", $quoted_schema_prefix, "` in current module")))
else
schema_name = (@__MODULE__).__legolas_schema_name_from_prefix__(Val($quoted_schema_prefix))
schema_version = Legolas.SchemaVersion{schema_name,$schema_version_integer}()
parent = Legolas._schema_version_from_record_type($(esc(parent_record_type)))
schema_version = $Legolas.SchemaVersion{schema_name,$schema_version_integer}()
parent = $Legolas._schema_version_from_record_type($(esc(parent_record_type)))

declared_identifier = string(schema_name, '@', $schema_version_integer)
if !isnothing(parent)
declared_identifier = string(declared_identifier, '>', Legolas.name(parent), '@', Legolas.version(parent))
declared_identifier = string(declared_identifier, '>', $Legolas.name(parent), '@', $Legolas.version(parent))
end
schema_version_declaration = declared_identifier => $(Base.Meta.quot(required_field_infos))

if Legolas.declared(schema_version) && Legolas.declaration(schema_version) != schema_version_declaration
if $Legolas.declared(schema_version) && $Legolas.declaration(schema_version) != schema_version_declaration
throw(SchemaVersionDeclarationError("invalid redeclaration of existing schema version; all `@version` redeclarations must exactly match previous declarations"))
elseif parent isa Legolas.SchemaVersion && Legolas.name(parent) == schema_name
elseif parent isa $Legolas.SchemaVersion && $Legolas.name(parent) == schema_name
throw(SchemaVersionDeclarationError("cannot extend from another version of the same schema"))
elseif parent isa Legolas.SchemaVersion && !(Legolas._has_valid_child_field_types($declared_field_names_types, Legolas.required_fields(parent)))
elseif parent isa $Legolas.SchemaVersion && !($Legolas._has_valid_child_field_types($declared_field_names_types, $Legolas.required_fields(parent)))
throw(SchemaVersionDeclarationError("declared field types violate parent's field types"))
else
Base.@__doc__($(Base.Meta.quot(record_type)))
$(esc(:eval))(Legolas._generate_schema_version_definitions(schema_version, parent, $declared_field_names_types, schema_version_declaration))
$(esc(:eval))(Legolas._generate_validation_definitions(schema_version))
$(esc(:eval))(Legolas._generate_record_type_definitions(schema_version, $(Base.Meta.quot(record_type))))
$(esc(:eval))($Legolas._generate_schema_version_definitions(schema_version, parent, $declared_field_names_types, schema_version_declaration))
$(esc(:eval))($Legolas._generate_validation_definitions(schema_version))
$(esc(:eval))($Legolas._generate_record_type_definitions(schema_version, $(Base.Meta.quot(record_type))))
end
end
nothing
Expand Down
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ end
@test isnothing(@schema "test.returns-nothing" ReturnsNothing)
end

# https://github.com/beacon-biosignals/Legolas.jl/issues/91
module Namespace91
using Test
using Legolas: @schema, @version, tobuffer, read

# Define something else with the name Legolas
struct Legolas end

@schema "test.a91" A91

@version A91V1 begin
a::Int
end

@testset "Macro invocation doesn't require Legolas module name in caller's scope" begin
# Try out a bunch of the auto-generated methods
# to ensure the wrong `Legolas` name isn't hidden in one of them
@test A91V1(; a=1) isa A91V1
@test hash(A91V1(; a=1)) isa UInt
@test A91V1(; a=1) == A91V1(; a=1)
@test isequal(A91V1(; a=1), A91V1(; a=1))
@test read(tobuffer([A91V1(; a=1)], A91V1SchemaVersion())).a == [1]
end
end # module

@schema "test.parent" Parent
@version ParentV1 begin
x::Vector
Expand Down

2 comments on commit 3e4db06

@ericphanson
Copy link
Member Author

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/85389

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.5.11 -m "<description of version>" 3e4db069b787fac9b73639c16f75e6e526497895
git push origin v0.5.11

Please sign in to comment.