Skip to content

Commit 3bdf652

Browse files
committed
feat(ext-core+ext-type-graph): define types relations
1 parent ba0c438 commit 3bdf652

6 files changed

Lines changed: 137 additions & 0 deletions

File tree

lib/apix.schema.extensions/core.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ defmodule Apix.Schema.Extensions.Core do
1515

1616
alias Apix.Schema.Extensions.Core.Const
1717

18+
@boolean_schemas [
19+
And,
20+
Or,
21+
Not
22+
]
23+
24+
@doc false
25+
def boolean_schemas, do: @boolean_schemas
26+
1827
@manifest %Extension{
1928
module: __MODULE__,
2029
delegates: [
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
defmodule Apix.Schema.Extensions.Core.And do
22
use Apix.Schema
33

4+
alias Apix.Schema.Ast
5+
alias Apix.Schema.Context
6+
47
schema t: _, params: [:schema1, :schema2] do
58
validate valid?(it, schema1) and valid?(it, schema2)
9+
10+
relate %Context{module: __MODULE__, schema: :t, params: [_schema1, _schema2]} = _it, _to, do: []
11+
relate _it, %Context{module: __MODULE__, schema: :t, params: [_schema1, _schema2]} = _to, do: []
12+
13+
relate %Ast{args: [schema1, schema2]} = it, to do
14+
[
15+
# Default subtyping
16+
{:subtype, it, to},
17+
{:supertype, to, it},
18+
# Identity subtyping
19+
{:subtype, it, it},
20+
{:supertype, it, it},
21+
# And-specific subtyping
22+
{:subtype, it, schema1},
23+
{:subtype, it, schema2},
24+
{:supertype, schema1, it},
25+
{:supertype, schema2, it}
26+
]
27+
end
628
end
729
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
defmodule Apix.Schema.Extensions.Core.Any do
22
use Apix.Schema
33

4+
alias Apix.Schema.Ast
5+
alias Apix.Schema.Context
6+
7+
alias Apix.Schema.Ast.Meta
8+
9+
alias Apix.Schema.Extensions.Core
10+
11+
@boolean_schemas Core.boolean_schemas()
12+
@it_ast %Ast{module: __MODULE__, schema: :t, args: []} |> Meta.maybe_put_in(env: __ENV__, generated_by: Core.manifest())
13+
414
schema t: _ do
515
validate true
16+
17+
relate it, to do
18+
[
19+
{:subtype, it, to},
20+
{:supertype, to, it},
21+
{:subtype, it, it},
22+
{:supertype, it, it}
23+
]
24+
end
25+
26+
relationship _it, %Context{module: m} = _peer, existing when m in @boolean_schemas, do: existing
27+
28+
relationship %Context{} = it, peer, existing do
29+
[
30+
{:subtype, peer, @it_ast},
31+
{:subtype, @it_ast, it},
32+
{:supertype, peer, @it_ast},
33+
{:supertype, @it_ast, it}
34+
| existing
35+
]
36+
end
637
end
738
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
defmodule Apix.Schema.Extensions.Core.None do
22
use Apix.Schema
33

4+
alias Apix.Schema.Ast
5+
alias Apix.Schema.Context
6+
7+
alias Apix.Schema.Ast.Meta
8+
9+
alias Apix.Schema.Extensions.Core
10+
11+
@boolean_schemas Core.boolean_schemas()
12+
@it_ast %Ast{module: __MODULE__, schema: :t, args: []} |> Meta.maybe_put_in(env: __ENV__, generated_by: Core.manifest())
13+
414
schema t: _ do
515
validate false
16+
17+
relate it, to do
18+
[
19+
{:subtype, it, to},
20+
{:supertype, to, it},
21+
{:subtype, it, it},
22+
{:supertype, it, it}
23+
]
24+
end
25+
26+
relationship _it, %Context{module: m} = _peer, existing when m in @boolean_schemas, do: existing
27+
28+
relationship %Context{} = it, peer, existing do
29+
[
30+
{:subtype, it, @it_ast},
31+
{:subtype, @it_ast, peer},
32+
{:supertype, peer, @it_ast},
33+
{:supertype, @it_ast, it}
34+
| existing
35+
]
36+
end
637
end
738
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
defmodule Apix.Schema.Extensions.Core.Not do
22
use Apix.Schema
33

4+
alias Apix.Schema.Ast
5+
alias Apix.Schema.Context
6+
47
schema t: _, params: [:schema] do
58
validate not valid?(it, schema)
9+
10+
relate %Context{module: __MODULE__, schema: :t, params: [_schema]} = _it, _to, do: []
11+
relate _it, %Context{module: __MODULE__, schema: :t, params: [_schema]} = _to, do: []
12+
13+
relate %Ast{args: [schema]} = it, to do
14+
[
15+
# Default subtyping
16+
{:subtype, it, to},
17+
{:supertype, to, it},
18+
# Identity subtyping
19+
{:subtype, it, it},
20+
{:supertype, it, it},
21+
# Not-specific subtyping
22+
{:not_subtype, schema, it},
23+
{:not_subtype, it, schema},
24+
{:not_supertype, it, schema},
25+
{:not_supertype, schema, it}
26+
]
27+
end
628
end
729
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
defmodule Apix.Schema.Extensions.Core.Or do
22
use Apix.Schema
33

4+
alias Apix.Schema.Ast
5+
alias Apix.Schema.Context
6+
47
schema t: _, params: [:schema1, :schema2] do
58
validate valid?(it, schema1) or valid?(it, schema2)
9+
10+
relate %Context{module: __MODULE__, schema: :t, params: [_schema1, _schema2]} = _it, _to, do: []
11+
relate _it, %Context{module: __MODULE__, schema: :t, params: [_schema1, _schema2]} = _to, do: []
12+
13+
relate %Ast{args: [schema1, schema2]} = it, to do
14+
[
15+
# Default subtyping
16+
{:subtype, to, it},
17+
{:supertype, it, to},
18+
# Identity subtyping
19+
{:subtype, it, it},
20+
{:supertype, it, it},
21+
# Or-specific subtyping
22+
{:subtype, schema1, it},
23+
{:subtype, schema2, it},
24+
{:supertype, it, schema1},
25+
{:supertype, it, schema2}
26+
]
27+
end
628
end
729
end

0 commit comments

Comments
 (0)