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

handle mutable struct definitions #8

Merged
merged 6 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
name: TagBot
on:
schedule:
- cron: 0 * * * *
issue_comment:
types:
- created
workflow_dispatch:
inputs:
lookback:
default: 3
permissions:
contents: write
jobs:
TagBot:
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
ssh: ${{ secrets.DOCUMENTER_KEY }}
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run tests

on:
push:
branches:
- master
pull_request:

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: ['1.6', '1', 'nightly']
julia-arch: [x64, x86]
os: [ubuntu-latest, windows-latest, macOS-latest]
exclude:
- os: macOS-latest
julia-arch: x86

steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.julia-version }}
arch: ${{ matrix.julia-arch }}
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
with:
annotate: true
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v3
with:
files: lcov.info
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name = "ProtoStructs"
uuid = "437b6fc4-8e8e-11e9-3fa1-ad391e66c018"
authors = ["Simon Christ"]
version = "1.0.0"
version = "1.1.0"

[compat]
julia = "1"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
92 changes: 66 additions & 26 deletions src/ProtoStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ macro proto( expr )
if expr.head != Symbol("struct")
throw(ArgumentError("Expected expression to be a type definition."))
end
ismutable = expr.args[1]

name = expr.args[2]
type_parameters = nothing
Expand Down Expand Up @@ -68,37 +69,76 @@ macro proto( expr )
ex
end

ex = quote
if !@isdefined $name
struct $name{NT<:NamedTuple}
properties::NT
end # struct
else
the_methods = collect(methods($name))
Base.delete_method(the_methods[1])
Base.delete_method(the_methods[3])
end # if

$(
if type_parameters === nothing
:( $name(args...) = $name(NamedTuple{$field_names, $field_types}(args)) )
ex = if ismutable
quote
if !@isdefined $name
struct $name{AD<:AbstractDict}
properties::AD
end # struct
else
:( $name($(fields...)) where {$(type_parameters...)} = $name(NamedTuple{$field_names, $field_types}(($(field_names...),))) )
the_methods = collect(methods($name))
Base.delete_method(the_methods[1])
Base.delete_method(the_methods[3])
end # if

$(
if type_parameters === nothing
:( $name(args...) = $name(Dict{Symbol,Any}(zip($field_names, args))) )
else
:( $name($(fields...)) where {$(type_parameters...)} = $name(Dict{Symbol,Any}(zip($field_names, ($(field_names...),)))) )
end
)

function $name($params_ex)
$name($(call_args...))
end
)

function $name($params_ex)
$name($(call_args...))

function Base.getproperty( o::$name, s::Symbol )
return getindex( getfield(o, :properties), s)
end # function

function Base.setproperty!( o::$name, s::Symbol, v )
dict = getfield(o, :properties)
return haskey(dict, s) ? setindex!( dict, v, s) : error(string("type ", $name, " has no field ", s))
end # function

function Base.propertynames( o::$name )
return Tuple(keys( getfield(o, :properties) ))
end # function
end
else
quote
if !@isdefined $name
struct $name{NT<:NamedTuple}
properties::NT
end # struct
else
the_methods = collect(methods($name))
Base.delete_method(the_methods[1])
Base.delete_method(the_methods[3])
end # if

function Base.getproperty( o::$name, s::Symbol )
return getproperty( getfield(o, :properties), s )
end # function
$(
if type_parameters === nothing
:( $name(args...) = $name(NamedTuple{$field_names, $field_types}(args)) )
else
:( $name($(fields...)) where {$(type_parameters...)} = $name(NamedTuple{$field_names, $field_types}(($(field_names...),))) )
end
)

function Base.propertynames( o::$name )
return propertynames( getfield(o, :properties) )
end # function
end # quote
function $name($params_ex)
$name($(call_args...))
end

function Base.getproperty( o::$name, s::Symbol )
return getproperty( getfield(o, :properties), s )
end # function

function Base.propertynames( o::$name )
return propertynames( getfield(o, :properties) )
end # function
end # quote
end
ex |> esc
end # macro

Expand Down
25 changes: 23 additions & 2 deletions test/test_ProtoStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test_me_kw2 = @test_nowarn TestMe(A=1, B="2", C=complex(1), D=5, E="tadaa")
@test length(methods(TestMe)) == 3
end # testset

ProtoStructs.@proto struct TestKw{T, V <: Real}
@proto struct TestKw{T, V <: Real}
A::Int = 1
B = :no
C::T = nothing
Expand All @@ -55,4 +55,25 @@ end
@test tw.C === nothing
@test tw.D == 1.2
@test tw.E == "yepp"
end
end

@proto mutable struct TestMutation{T, V <: Real}
A::Int = 1
B = :no
C::T = nothing
D::V
E::String
end

@testset "Mutation" begin
tm = @test_nowarn TestMutation(D = 1.2, E = "yepp")
tm.A = 2
tm.E = "nope"
@test_throws ErrorException tm.this = "is wrong"
@test tm isa TestMutation
@test tm.A == 2
@test tm.B == :no
@test tm.C === nothing
@test tm.D == 1.2
@test tm.E == "nope"
end