Skip to content

Commit 569502e

Browse files
authored
Set up Documenter (#158)
* Set up Documenter * more docs, add action * more docs * fix Documenter script * typo * add to LOAD_PATH * move doctests to runtests.jl * interpolate types into doctest strings
1 parent 69c29a1 commit 569502e

File tree

12 files changed

+366
-22
lines changed

12 files changed

+366
-22
lines changed

.github/workflows/Documenter.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Documenter
2+
on:
3+
pull_request:
4+
branches:
5+
- master
6+
push:
7+
branches:
8+
- master
9+
tags: '*'
10+
jobs:
11+
docs:
12+
name: Documentation
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: julia-actions/setup-julia@v1
17+
with:
18+
version: '1'
19+
- run: |
20+
julia --project -e '
21+
using Pkg
22+
Pkg.instantiate()'
23+
julia --project=docs -e '
24+
using Pkg
25+
Pkg.instantiate()'
26+
- run: julia --project=docs docs/make.jl
27+
env:
28+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ Tables = "1"
1414
julia = "1"
1515

1616
[extras]
17+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
1718
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1819
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
1920
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2021
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
2122

2223
[targets]
23-
test = ["Test", "OffsetArrays", "PooledArrays", "WeakRefStrings"]
24+
test = ["Test", "OffsetArrays", "PooledArrays", "WeakRefStrings", "Documenter"]

docs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
site/

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[deps]
2+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"

docs/make.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
push!(LOAD_PATH, dirname(@__DIR__))
2+
3+
using Documenter
4+
using StructArrays
5+
6+
makedocs(
7+
sitename = "StructArrays",
8+
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
9+
modules = [StructArrays]
10+
)
11+
12+
# Documenter can also automatically deploy documentation to gh-pages.
13+
# See "Hosting Documentation" and deploydocs() in the Documenter manual
14+
# for more information.
15+
deploydocs(
16+
repo = "github.com/JuliaArrays/StructArrays.jl.git",
17+
push_preview = true,
18+
)

docs/src/index.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# StructArrays.jl
2+
3+
4+
```@meta
5+
CurrentModule = StructArrays
6+
```
7+
8+
# Type
9+
10+
```@docs
11+
StructArray
12+
```
13+
14+
# Constructors
15+
16+
```@docs
17+
StructArray(tup::Union{Tuple,NamedTuple})
18+
StructArray(::Base.UndefInitializer, sz::Dims)
19+
StructArray(v)
20+
collect_structarray
21+
```
22+
23+
# Accessors
24+
25+
```@docs
26+
fieldarrays
27+
```
28+
29+
# Lazy iteration
30+
31+
```@docs
32+
LazyRow
33+
LazyRows
34+
```
35+
36+
# Advanced APIs
37+
38+
```@docs
39+
StructArrays.append!!
40+
StructArrays.replace_storage
41+
StructArrays.staticschema
42+
StructArrays.createinstance
43+
```
44+
45+
# Internals
46+
47+
```@docs
48+
StructArrays.get_ith
49+
StructArrays.map_params
50+
StructArrays._map_params
51+
StructArrays.buildfromschema
52+
StructArrays.bypass_constructor
53+
StructArrays.iscompatible
54+
```

src/collect.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _axes(itr, ::Base.HasLength) = (Base.OneTo(length(itr)),)
2727
_axes(itr, ::Base.HasShape) = axes(itr)
2828

2929
"""
30-
`collect_structarray(itr; initializer = default_initializer)`
30+
collect_structarray(itr; initializer = default_initializer)
3131
3232
Collects `itr` into a `StructArray`. The user can optionally pass a `initializer`, that is to say
3333
a function `(S, d) -> v` that associates to a type and a size an array of eltype `S`
@@ -128,18 +128,18 @@ function _widenarray(dest::AbstractArray, i, ::Type{T}) where T
128128
end
129129

130130
"""
131-
`append!!(dest, itr) -> dest′`
132-
133-
Try to append `itr` into a vector `dest`. Widen element type of
134-
`dest` if it cannot hold the elements of `itr`. That is to say,
131+
dest = StructArrays.append!!(dest, itr)
135132
133+
Try to append `itr` into a vector `dest`, widening the element type of `dest` if
134+
it cannot hold the elements of `itr`. That is to say,
136135
```julia
137136
vcat(dest, StructVector(itr)) == append!!(dest, itr)
138137
```
138+
holds. Note that the `dest` argument may or may not be the same object as the
139+
returned value.
139140
140-
holds. Note that `dest′` may or may not be the same object as `dest`.
141-
The state of `dest` is unpredictable after `append!!`
142-
is called (e.g., it may contain just half of the elements from `itr`).
141+
The state of `dest` is unpredictable after `append!!` is called (e.g., it may
142+
contain some, none or all the elements from `itr`).
143143
"""
144144
append!!(dest::AbstractVector, itr) =
145145
_append!!(dest, itr, Base.IteratorSize(itr))

src/interface.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
const Tup = Union{Tuple, NamedTuple}
22
const EmptyTup = Union{Tuple{}, NamedTuple{(), Tuple{}}}
33

4+
"""
5+
StructArrays.staticschema(T)
6+
7+
The default schema for an element type `T`. A schema is a `Tuple` or
8+
`NamedTuple` type containing the necessary fields to construct `T`. By default,
9+
this will have fields with the same names and types as `T`.
10+
11+
This can be overloaded for custom types if required, in which case
12+
[`StructArrays.createinstance`](@ref) should also be defined.
13+
14+
```julia-repl
15+
julia> StructArrays.staticschema(Complex{Float64})
16+
NamedTuple{(:re, :im),Tuple{Float64,Float64}}
17+
```
18+
"""
419
@generated function staticschema(::Type{T}) where {T}
520
name_tuple = Expr(:tuple, [QuoteNode(f) for f in fieldnames(T)]...)
621
type_tuple = Expr(:curly, :Tuple, [Expr(:call, :fieldtype, :T, i) for i in 1:fieldcount(T)]...)
@@ -9,6 +24,18 @@ end
924

1025
staticschema(::Type{T}) where {T<:Tup} = T
1126

27+
"""
28+
StructArrays.createinstance(T, args...)
29+
30+
Construct an instance of type `T` from its backing representation. `args` here
31+
are the elements of the `Tuple` or `NamedTuple` type specified
32+
[`staticschema(T)`](@ref).
33+
34+
```julia-repl
35+
julia> StructArrays.createinstance(Complex{Float64}, (re=1.0, im=2.0)...)
36+
1.0 + 2.0im
37+
```
38+
"""
1239
function createinstance(::Type{T}, args...) where {T}
1340
isconcretetype(T) ? bypass_constructor(T, args) : T(args...)
1441
end

src/lazy.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
"""
2+
LazyRow(s::StructArray, i)
3+
4+
A lazy representation of `s[i]`. `LazyRow(s, i)` does not materialize the `i`th
5+
row but returns a lazy wrapper around it on which `getproperty` does the correct
6+
thing. This is useful when the row has many fields only some of which are
7+
necessary. It also allows changing columns in place.
8+
9+
See [`LazyRows`](@ref) to get an iterator of `LazyRow`s.
10+
11+
# Examples
12+
13+
```julia-repl
14+
julia> t = StructArray((a = [1, 2], b = ["x", "y"]));
15+
16+
julia> LazyRow(t, 2).a
17+
2
18+
19+
julia> LazyRow(t, 2).a = 123
20+
123
21+
22+
julia> t
23+
2-element StructArray(::Array{Int64,1}, ::Array{String,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,String}}:
24+
(a = 1, b = "x")
25+
(a = 123, b = "y")
26+
```
27+
"""
128
struct LazyRow{T, N, C, I}
229
columns::StructArray{T, N, C, I} # a `Columns` object
330
index::I
@@ -29,6 +56,20 @@ iscompatible(::Type{<:LazyRow{R}}, ::Type{S}) where {R, S<:StructArray} = iscomp
2956

3057
(s::ArrayInitializer)(::Type{<:LazyRow{T}}, d) where {T} = buildfromschema(typ -> s(typ, d), T)
3158

59+
"""
60+
LazyRows(s::StructArray)
61+
62+
An iterator of [`LazyRow`](@ref)s of `s`.
63+
64+
# Examples
65+
66+
```julia-repl
67+
julia> map(t -> t.b ^ t.a, LazyRows(t))
68+
2-element Array{String,1}:
69+
"x"
70+
"yy"
71+
```
72+
"""
3273
struct LazyRows{T, N, C, I} <: AbstractArray{LazyRow{T, N, C, I}, N}
3374
columns::StructArray{T, N, C, I}
3475
end

0 commit comments

Comments
 (0)