Skip to content

Commit

Permalink
improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 committed Sep 16, 2021
1 parent 7f5aaae commit ab9ef1c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ end
@batteries S hash=false # don't overload `Base.hash`
@batteries S kwconstructor=true # add a keyword constructor
```
For all supported options and defaults, consult the docstring:
```julia
julia>?@batteries
```

# Alternatives

Expand Down
42 changes: 37 additions & 5 deletions src/StructHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,48 @@ function def_kwconstructor(T, propertynames)
end

const BATTERIES_DEFAULTS = (
eq=true, hash=true,
kwconstructor=false, kwshow=false,
getproperties=true, constructorof=true,
eq = true ,
hash = true ,
kwconstructor = false,
kwshow = false,
getproperties = true ,
constructorof = true ,
)

const BATTERIES_DOCSTRINGS = (
eq = "Define `Base.(==)` structurally.",
hash = "Define `Base.hash` structurally.",
kwconstructor = "Add a keyword constructor.",
kwshow = "Overload `Base.show` such that the names of each field are printed.",
getproperties = "Overload `ConstructionBase.getproperties`.",
constructorof = "Overload `ConstructionBase.constructorof`.",
)

if (keys(BATTERIES_DEFAULTS) != keys(BATTERIES_DOCSTRINGS))
error("""
keys(BATTERIES_DEFAULTS) == key(BATTERIES_DOCSTRINGS) must hold.
Got:
keys(BATTERIES_DEFAULTS) = $(keys(BATTERIES_DEFAULTS))
keys(BATTERIES_DOCSTRINGS) = $(keys(BATTERIES_DOCSTRINGS))
""")
end
@assert keys(BATTERIES_DEFAULTS) == keys(BATTERIES_DOCSTRINGS)

function doc_batteries_options()
lines = map(propertynames(BATTERIES_DEFAULTS)) do key
"* **$key** = $(BATTERIES_DEFAULTS[key]):\n $(BATTERIES_DOCSTRINGS[key])"
end
join(lines, "\n")
end


const ALLOWED_KW = keys(BATTERIES_DEFAULTS)

"""
@batteries T [options]
Automatically derive several methods for type `T`.
Supported options are:
$BATTERIES_DEFAULTS
# Example
```julia
Expand All @@ -60,6 +88,10 @@ end
@batteries S hash=false # don't overload `Base.hash`
@batteries S kwconstructor=true # add a keyword constructor
```
Supported options and defaults are:
$(doc_batteries_options())
"""
macro batteries(T, kw...)
nt = parse_all_macro_kw(kw)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct Empty2 end
@batteries Empty1
@batteries Empty2

struct SErrors;a;b;c;end

@testset "@batteries" begin
@test SBatteries(1,2) == SBatteries(1,2)
Expand Down Expand Up @@ -63,7 +64,6 @@ struct Empty2 end
@test Empty1() != Empty2()
@test hash(Empty1()) != hash(Empty2())

struct SErrors;a;b;c;end
@test_throws Exception @macroexpand @batteries SErrors kwconstructor="true"
@test_throws Exception @macroexpand @batteries SErrors nonsense=true
@macroexpand @batteries SErrors kwconstructor=true
Expand Down

0 comments on commit ab9ef1c

Please sign in to comment.