Skip to content

Commit

Permalink
feat: add support for nested PDUs
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Sep 29, 2023
1 parent 72b01b6 commit 7a2fbbd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ name = "PDUs"
uuid = "07db6e9f-91ad-4fbd-9fd8-5722e51e9a72"
authors = ["Mandar Chitre <mandar@nus.edu.sg>"]
version = "0.1.0"

[deps]

[compat]
3 changes: 2 additions & 1 deletion src/PDUs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ function Base.read(io::IO, T::Type{<:PDU}; nbytes=missing)
v = n === nothing ? varread(io, V) : V[read(io, V) for _ 1:n]
push!(data, f => F <: AbstractString ? strip(String(v), ['\0']) : ptoh.(v))
else
push!(data, f => read(io, F))
n = length(T, Val(f), PDUInfo(nbytes, s -> lookup(data, s)))
push!(data, f => read(io, F; nbytes=something(n, missing)))
end
end
T(map(kv -> kv[2], data)...)
Expand Down
56 changes: 56 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ using PDUs
@test f1 == f2
@test length(buf) == 18 + 255

end

@testset "strings" begin

struct TestPDU <: PDU
n::UInt8
s::String
Expand Down Expand Up @@ -105,3 +109,55 @@ using PDUs
@test f1 == f2

end

@testset "nested" begin

struct InnerPDU <: PDU
a::UInt16
b::String
end

struct OuterPDU <: PDU
n::UInt16
inner::InnerPDU
end

f1in = InnerPDU(0x1234, "hello")
f1 = OuterPDU(0x5678, f1in)
buf = Vector{UInt8}(f1)
f2 = OuterPDU(buf)

@test f1.n == f2.n
@test f1.inner == f2.inner
@test f1 == f2
@test length(buf) == 10
@test buf == [0x56, 0x78, 0x12, 0x34, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f]

PDUs.length(::Type{InnerPDU}, ::Val{:b}, info) = info.get(:a)

f1in = InnerPDU(0x05, "hello")
f1 = OuterPDU(0x5678, f1in)
buf = Vector{UInt8}(f1)
f2 = OuterPDU(buf)

@test f1.n == f2.n
@test f1.inner == f2.inner
@test f1 == f2
@test length(buf) == 9
@test buf == [0x56, 0x78, 0x00, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f]

PDUs.length(::Type{InnerPDU}, ::Val{:b}, info) = info.length - 2
PDUs.length(::Type{OuterPDU}, ::Val{:inner}, info) = info.length - 2

f1in = InnerPDU(0x1234, "hello")
f1 = OuterPDU(0x5678, f1in)
buf = Vector{UInt8}(f1)
f2 = OuterPDU(buf)

@test f1.n == f2.n
@test f1.inner == f2.inner
@test f1 == f2
@test length(buf) == 9
@test buf == [0x56, 0x78, 0x12, 0x34, 0x68, 0x65, 0x6c, 0x6c, 0x6f]

end

2 comments on commit 7a2fbbd

@mchitre
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 updated: JuliaRegistries/General/92457

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.1.0 -m "<description of version>" 7a2fbbdf991ac8bc4caecb19d7fc2d510ce75d19
git push origin v0.1.0

Please sign in to comment.