Skip to content

Add API type to public API #14

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ julia> using PublicAPI
julia> apis = PublicAPI.of(PublicAPI);

julia> sort!(fullname.(apis))
3-element Vector{Tuple{Symbol, Symbol}}:
4-element Vector{Tuple{Symbol, Symbol}}:
(:PublicAPI, Symbol("@public"))
(:PublicAPI, Symbol("@strict"))
(:PublicAPI, :API)
(:PublicAPI, :of)
```

Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ PublicAPI
PublicAPI.@public
PublicAPI.@strict
PublicAPI.of
PublicAPI.API
```
15 changes: 14 additions & 1 deletion src/PublicAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ baremodule PublicAPI

export @public

module InternalPrelude
function _API end
end

macro public end
macro strict end

function of end

struct API
mod::Module
var::Symbol

InternalPrelude._API(mod::Module, var::Symbol) = new(mod, var)
end

module Internal

using ..PublicAPI: PublicAPI
using ..PublicAPI.InternalPrelude: _API
using ..PublicAPI: PublicAPI, API
import ..PublicAPI: @public, @strict

include("registry.jl")
Expand All @@ -33,5 +45,6 @@ Internal.define_docstring()
@public @public
@public @strict
@public of
@public API

end # baremodule PublicAPI
32 changes: 18 additions & 14 deletions src/query.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
struct API
mod::Module
var::Symbol
end

Base.fullname(api::API) = (fullname(api.mod)..., api.var)
Base.Module(api::API) = api.mod
Base.nameof(api::API) = api.var

"""
PublicAPI.of(provider::Module; [recursive = true]) -> apis::Vector
PublicAPI.API

List public API from the `provider` module.
A value representing an API.

Each element `api` of `apis` supports the following accessor functions:
An `api::API` supports the following accessor functions:

* `Module(api) :: Module`: module in which the API is defined
* `nameof(api) :: Symbol`: the name of the API in the module
* `fullname(api) :: Tuple{Vararg{Symbol}}`: the components of the
fully-qualified name; i.e., `(:Package, :SubModule, :function)` for
`Package.SubModule.function`.
"""
PublicAPI.API

Base.fullname(api::API) = (fullname(api.mod)..., api.var)
Base.Module(api::API) = api.mod
Base.nameof(api::API) = api.var

"""
PublicAPI.of(provider::Module; [recursive = true]) -> apis::Vector{PublicAPI.API}

List public API from the `provider` module.

See [`PublicAPI.API`](@ref) for methods supported by each element of `apis`.

The `provider` module itself is not included in the `apis`.

Expand All @@ -45,7 +49,7 @@ function PublicAPI.of(provider::Module; recursive::Bool = true)
registry = getfield(m, API_REGISTRY_NAME)
if registry isa Vector{Symbol}
for n in registry
push!(apis, API(m, n))
push!(apis, _API(m, n))
mayberecurse(n)
end
else
Expand All @@ -58,7 +62,7 @@ function PublicAPI.of(provider::Module; recursive::Bool = true)
end
n === nameof(m) && continue # avoid the module to be listed twice
if isdefined(m, n)
push!(apis, API(m, n))
push!(apis, _API(m, n))
mayberecurse(n)
else
# Should error?
Expand Down
4 changes: 2 additions & 2 deletions test/PublicAPITests/src/test_query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import ..PublicAPITests

function test_of()
apis = PublicAPI.of(PublicAPI)
@test sort!(nameof.(apis)) == [Symbol("@public"), Symbol("@strict"), :of]
@test Module.(apis) == fill(PublicAPI, 3)
@test sort!(nameof.(apis)) == [Symbol("@public"), Symbol("@strict"), :API, :of]
@test Module.(apis) == fill(PublicAPI, 4)
end

function test_recursive()
Expand Down