generated from AlgebraicJulia/AlgebraicTemplate.jl
-
Notifications
You must be signed in to change notification settings - Fork 8
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
InterTypes #75
Merged
Merged
InterTypes #75
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e4eebd
transfered intertype over
olynch c0cd424
fixed json writing
olynch c05ea8d
added python roundtrip test
olynch 7823047
include correct file
olynch c39a152
resolve conda packages
olynch f9eb29e
hopefully add is idempotent
olynch cf257c3
clean up files after tests
olynch b941bf7
bump that coverage
olynch 00f6af2
join path with slash
olynch a07f3a1
removed interschema
olynch 1933a91
disable conda for windows
olynch 97e9a7b
intertype modules
olynch 85f5a01
export whole module to jsonschema
olynch eeaf893
hash the type declaration
olynch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,5 @@ Manifest.toml | |
.DS_Store | ||
.vscode | ||
.ipynb_checkpoints | ||
test/.CondaPkg | ||
test/intertypes/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module InterTypes | ||
export InterType, InterTypeDecl, Binary, intertype, as_intertypes | ||
|
||
using MLStyle | ||
using ..Schemas | ||
|
||
struct Field{T} | ||
name::Symbol | ||
type::T | ||
end | ||
|
||
Base.nameof(field::Field) = field.name | ||
|
||
struct Variant{T} | ||
tag::Symbol | ||
fields::Vector{Field{T}} | ||
end | ||
|
||
Base.nameof(variant::Variant) = variant.tag | ||
|
||
@data InterType begin | ||
I32 | ||
U32 | ||
I64 | ||
U64 | ||
F64 | ||
Boolean | ||
Str | ||
Sym | ||
Binary | ||
List(elemtype::InterType) | ||
Map(keytype::InterType, valuetype::InterType) | ||
Record(fields::Vector{Field{InterType}}) | ||
Sum(variants::Vector{Variant{InterType}}) | ||
ACSetInterType(schema::TypedSchema{InterType}) | ||
Annot(desc::String, type::InterType) | ||
TypeRef(to::Symbol) | ||
end | ||
|
||
@data InterTypeDecl begin | ||
Alias(name::Symbol, type::InterType) | ||
SumType(name::Symbol, variants::Vector{Variant{InterType}}) | ||
Struct(name::Symbol, fields::Vector{Field{InterType}}) | ||
SchemaDecl(name::Symbol, schema::TypedSchema{Symbol, InterType}) | ||
NamedACSetType(name::Symbol, schemaname::Symbol) | ||
end | ||
|
||
Base.nameof(decl::InterTypeDecl) = @match decl begin | ||
Alias(name, _) => name | ||
SumType(name, _) => name | ||
Struct(name, _) => name | ||
end | ||
|
||
function intertype end | ||
|
||
include("json.jl") | ||
include("julia.jl") | ||
include("python.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
export intertype_to_jsonschema, jsonwrite, jsonread, parse_intertype, toexpr, @intertype_decls, as_intertypes | ||
|
||
using OrderedCollections | ||
using Base64 | ||
using MLStyle | ||
import JSON3 | ||
|
||
struct JSONFormat | ||
end | ||
|
||
struct InterTypeConversionError <: Exception | ||
expected::InterType | ||
got::Any | ||
end | ||
|
||
function read(format, type::Type, x) | ||
throw(InterTypeConversionError(intertype(type), x)) | ||
end | ||
|
||
function joinwith(io::IO, f, xs, separator) | ||
for x in xs[1:end-1] | ||
f(io, x) | ||
print(io, separator) | ||
end | ||
f(io, xs[end]) | ||
end | ||
|
||
jsonwrite(x) = sprint(jsonwrite, x) | ||
jsonwrite(io::IO, x) = write(io, JSONFormat(), x) | ||
function jsonread(s::String, ::Type{T}) where {T} | ||
json = JSON3.read(s) | ||
read(JSONFormat(), T, json) | ||
end | ||
|
||
intertype(::Type{Int32}) = I32 | ||
read(::JSONFormat, ::Type{Int32}, s::Real) = Int32(s) | ||
write(io::IO, ::JSONFormat, d::Int32) = print(io, d) | ||
|
||
intertype(::Type{UInt32}) = U32 | ||
read(::JSONFormat, ::Type{UInt32}, s::Real) = UInt32(s) | ||
write(io::IO, ::JSONFormat, d::UInt32) = print(io, d) | ||
|
||
intertype(::Type{Int64}) = I64 | ||
read(::JSONFormat, ::Type{Int64}, s::String) = parse(Int64, s) | ||
write(io::IO, ::JSONFormat, d::Int64) = print(io, "\"", d, "\"") | ||
|
||
intertype(::Type{UInt64}) = U64 | ||
read(::JSONFormat, ::Type{UInt64}, s::String) = parse(UInt64, s) | ||
write(io::IO, ::JSONFormat, d::UInt64) = print(io, "\"", d, "\"") | ||
|
||
intertype(::Type{Float64}) = F64 | ||
read(::JSONFormat, ::Type{Float64}, s::Real) = Float64(s) | ||
write(io::IO, ::JSONFormat, d::Float64) = print(io, d) | ||
|
||
intertype(::Type{Bool}) = Boolean | ||
read(::JSONFormat, ::Type{Bool}, s::Bool) = s | ||
write(io::IO, ::JSONFormat, d::Bool) = print(io, d) | ||
|
||
intertype(::Type{String}) = Str | ||
read(::JSONFormat, ::Type{String}, s::String) = s | ||
write(io::IO, ::JSONFormat, d::String) = JSON3.write(io, d) | ||
|
||
intertype(::Type{Symbol}) = Sym | ||
read(::JSONFormat, ::Type{Symbol}, s::String) = Symbol(s) | ||
write(io::IO, ::JSONFormat, d::Symbol) = JSON3.write(io, string(d)) | ||
|
||
intertype(::Type{Vector{UInt8}}) = Binary | ||
read(::JSONFormat, ::Type{Vector{UInt8}}, s::String) = base64decode(s) | ||
function write(io::IO, ::JSONFormat, d::Vector{UInt8}) | ||
print(io, "\"") | ||
Base.write(Base64EncodePipe(io), d) | ||
print(io, "\"") | ||
end | ||
|
||
intertype(::Type{Vector{T}}) where {T} = List(intertype(T)) | ||
function read(format::JSONFormat, ::Type{Vector{T}}, s::JSON3.Array) where {T} | ||
res = T[] | ||
for elt in s | ||
push!(res, read(format, T, elt)) | ||
end | ||
res | ||
end | ||
function write(io::IO, format::JSONFormat, d::Vector{T}) where {T} | ||
print(io, "[") | ||
joinwith(io, (io, x) -> write(io, format, x), d, ",") | ||
print(io, "]") | ||
end | ||
|
||
intertype(::Type{OrderedDict{K,V}}) where {K,V} = Map(intertype(K), intertype(V)) | ||
function read(format::JSONFormat, ::Type{OrderedDict{K, V}}, s::JSON3.Array) where {K, V} | ||
res = OrderedDict{K, V}() | ||
for elt in s | ||
(;key, value) = read(format, NamedTuple{(:key, :value), Tuple{K, V}}, elt) | ||
res[key] = value | ||
end | ||
res | ||
end | ||
function write(io::IO, format::JSONFormat, d::OrderedDict{K, V}) where {K, V} | ||
print(io, "[") | ||
joinwith(io, (io, x) -> write(io, format, (key=x[1], value=x[2])), collect(pairs(d)), ",") | ||
print(io, "]") | ||
end | ||
|
||
function intertype(::Type{T}) where {T<:Tuple} | ||
types = T.parameters | ||
Record(map(enumerate(types)) do (i, type) | ||
Field{InterType}(Symbol("_", i), intertype(type)) | ||
end) | ||
end | ||
function read(format::JSONFormat, ::Type{T}, s::JSON3.Object) where {T<:Tuple} | ||
keys = Tuple([Symbol("_", i) for i in 1:length(T.parameters)]) | ||
Tuple(read(format, NamedTuple{keys, T}, s)) | ||
end | ||
function write(io::IO, format::JSONFormat, d::T) where {T<:Tuple} | ||
keys = Tuple([Symbol("_", i) for i in 1:length(T.parameters)]) | ||
write(io, format, NamedTuple{keys, T}(d)) | ||
end | ||
|
||
function intertype(::Type{NamedTuple{names, T}}) where {names, T<:Tuple} | ||
types = T.parameters | ||
Record([Field{InterType}(name, intertype(type)) for (name, type) in zip(names, (types))]) | ||
end | ||
# TODO: comptime this | ||
function read(format::JSONFormat, ::Type{NamedTuple{names, T}}, s::JSON3.Object) where {names, T<:Tuple} | ||
keys(s) == Set(names) || error("wrong keys: expected $names got $(keys(s))") | ||
vals = Any[] | ||
for (name, type) in zip(names, T.parameters) | ||
push!(vals, read(format, type, s[name])) | ||
end | ||
NamedTuple{names, T}(vals) | ||
end | ||
function write(io::IO, format::JSONFormat, d::NamedTuple{names, T}) where {names, T<:Tuple} | ||
print(io, "{") | ||
function writekv(io, kv::Pair{Symbol, T}) where {T} | ||
(k, v) = kv | ||
JSON3.write(io, k) | ||
print(io, ":") | ||
write(io, format, v) | ||
end | ||
joinwith(io, writekv, [pairs(d)...], ",") | ||
print(io, "}") | ||
end | ||
|
||
const Object = OrderedDict{String, Any} | ||
|
||
function fieldproperties(fields::Vector{Field{InterType}}) | ||
map(fields) do field | ||
string(field.name) => intertype_to_jsonschema(field.type) | ||
end | ||
end | ||
|
||
function intertype_to_jsonschema(type::InterType) | ||
@match type begin | ||
I32 => Object( | ||
"type" => "integer", | ||
"\$comment" => "I32", | ||
"minimum" => typemin(Int32), | ||
"maximum" => typemax(Int32) | ||
) | ||
U32 => Object( | ||
"type" => "integer", | ||
"\$comment" => "U32", | ||
"minimum" => typemin(UInt32), | ||
"maximum" => typemax(UInt32) | ||
) | ||
I64 => Object( | ||
"type" => "string", | ||
"\$comment" => "I64" | ||
) | ||
U64 => Object( | ||
"type" => "string", | ||
"\$comment" => "U64" | ||
) | ||
F64 => Object( | ||
"type" => "number", | ||
"\$comment" => "F64" | ||
) | ||
Boolean => Object( | ||
"type" => "boolean", | ||
"\$comment" => "Boolean" | ||
) | ||
Str => Object( | ||
"type" => "string", | ||
"\$comment" => "Str" | ||
) | ||
Sym => Object( | ||
"type" => "string", | ||
"\$comment" => "Sym" | ||
) | ||
Binary => Object( | ||
"type" => "string", | ||
"contentEncoding" => "base64", | ||
"\$comment" => "Binary" | ||
) | ||
List(elemtype) => Object( | ||
"type" => "array", | ||
"items" => intertype_to_jsonschema(elemtype) | ||
) | ||
Map(keytype, valuetype) => Object( | ||
"type" => "array", | ||
"items" => Object( | ||
"type" => "object", | ||
"properties" => Object( | ||
"key" => intertype_to_jsonschema(keytype), | ||
"value" => intertype_to_jsonschema(valuetype) | ||
) | ||
) | ||
) | ||
Record(fields) => Object( | ||
"type" => "object", | ||
"properties" => Object(fieldproperties(fields)), | ||
"required" => string.(nameof.(fields)) | ||
) | ||
Sum(variants) => Object( | ||
"oneOf" => Vector{Object}(map(variants) do variant | ||
Object( | ||
"type" => "object", | ||
"properties" => Object( | ||
"tag" => Object( | ||
"const" => string(variant.tag) | ||
), | ||
fieldproperties(variant.fields)... | ||
), | ||
"required" => string.(nameof.(variant.fields)) | ||
) | ||
end) | ||
) | ||
Annot(desc, innertype) => begin | ||
innerschematype = intertype_to_jsonschema(innertype) | ||
innerschematype["description"] = desc | ||
innerschematype | ||
end | ||
TypeRef(to) => Object( | ||
"\$ref" => string(to) | ||
) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
Suppose it doesn’t matter if no other Serial formats are on the radar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually, I also want s-expression and msgpack serialization