Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use common methods for Base functions instead of generating #103

Merged
merged 8 commits into from
Oct 3, 2023
Merged
15 changes: 11 additions & 4 deletions src/schemas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,19 @@ end
##### Base overload definitions
#####

function Base.:(==)(x::T, y::T) where {T<:AbstractRecord}
return all(i -> getfield(x, i) == getfield(y, i), 1:fieldcount(T))
_typeof(r::AbstractRecord) = Base.unwrap_unionall(Base.typename(typeof(r)).wrapper)
ararslan marked this conversation as resolved.
Show resolved Hide resolved

_type_equal(x::R, y::R) where {R<:AbstractRecord} = true
_type_equal(x::AbstractRecord, y::AbstractRecord) = _typeof(x) === _typeof(y)

function Base.:(==)(x::AbstractRecord, y::AbstractRecord)
_type_equal(x, y) || return false
return all(i -> getfield(x, i) == getfield(y, i), 1:nfields(x))
end

function Base.isequal(x::T, y::T) where {T<:AbstractRecord}
return all(i -> isequal(getfield(x, i), getfield(y, i)), 1:fieldcount(T))
function Base.isequal(x::AbstractRecord, y::AbstractRecord)
_type_equal(x, y) || return false
return all(i -> isequal(getfield(x, i), getfield(y, i)), 1:nfields(x))
end

function Base.hash(r::AbstractRecord, h::UInt)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ end
@test hash(c) isa UInt # NOTE: can't rely on particular values
@test UnionMissingV1(; a=missing, b=1) != UnionMissingV1(; a=missing, b=2)
@test !isequal(UnionMissingV1(; a=missing, b=1), UnionMissingV1(; a=missing, b=2))
@test ParamV1(; i=one(Int32)) == ParamV1(; i=one(Int64))
@test isequal(ParamV1(; i=one(Int32)), ParamV1(; i=one(Int64)))
end
Copy link
Member

Choose a reason for hiding this comment

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

Should add some tests comparing ChildV1 to a ParentV1 and a ParentV1 to a ChildV1. In particuar I am thinking about _compare_fields(eq, ::AbstractRecord, ::AbstractRecord)

Copy link
Member Author

Choose a reason for hiding this comment

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

Is what I just added what you had in mind?

end

Expand Down