Skip to content
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

Default type mappings for reading json acsets #45

Open
KevinDCarlson opened this issue Jul 11, 2023 · 4 comments
Open

Default type mappings for reading json acsets #45

KevinDCarlson opened this issue Jul 11, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@KevinDCarlson
Copy link

Daniel Filonik suggests making it so that read_json_acset will guess that you want JSON strings to go to Julia strings, floats to Julia floats, and so on, so that you don't have to specify all the parameters of the acset type you're going to explicitly when they ought to be obvious.

@epatters epatters added the enhancement New feature or request label Jul 11, 2023
@epatters
Copy link
Member

Migrating to ACSets.jl since the JSON serialization code is now there.

@epatters epatters transferred this issue from AlgebraicJulia/Catlab.jl Jul 11, 2023
@filonik-cmu
Copy link

filonik-cmu commented Jul 12, 2023

This problem arises in particular when importing schemas with lots of AttrTypes. It is especially unpleasant with static acsets (although type system limitations might make it difficult to do anything about it), because the AttrTypes have to be provided to the resulting parametric acset type in the same order that they are read from JSON. If you work even with modest databases, you get types like:

MyDB{String,Float64,String,String,Int64,String}

Now, if for some reason the ordering of the AttrTypes in the JSON changes, everything breaks. I have been thinking whether it might be possible to side-step the issue entirely by having sensible defaults for primitive types in different formats/languages, whenever it is "obvious" how to convert values to and from Julia.

In a way, there are two separate issues at play:

  1. Improving the ergonomics/robustness of specifying type assignments in general.
  2. Having default assignments between types in the serialized and deserialized representations.

To make things concrete, with dynamic acsets this is relatively simple to address, because the type assignments are stored in a Dict, so it would be as simple as providing something like:

# JavaScript -> Julia
# See: https://developer.mozilla.org/en-US/docs/Glossary/Primitive
default_javascript_type_assignments = Dict(
    :boolean => Bool,
    :number => Float64,
    :string => String,
    :symbol => Symbol,
    #:bigint => ?,
    #:null => ?,
    #:undefined => ?,
)

Or, it may be better to use Julia's types:

# Julia -> Julia
# See: https://docs.julialang.org/en/v1/manual/types/#Primitive-Types
default_julia_type_assignments = Dict(
    :Bool => Bool, # boolean
    :Int8 => Int8, # number
    :Int16 => Int16, # number
    :Int32 => Int32, # number
    :Int64 => Int64, # number
    #:Int128 => Int128, # BigInt (not supported in vanilla JSON)
    :UInt8 => UInt8, # number
    :UInt16 => UInt16, # number
    :UInt32 => UInt32, # number
    :UInt64 => UInt64, # number
    #:UInt128 => UInt128, # BigInt (not supported in vanilla JSON)
    :Float16 => Float16, # number
    :Float32 => Float32, # number
    :Float64 => Float64, # number
    :Char => Char, # string
    :String => String, # string
)

Initially, it makes sense to focus on JavaScript/JSON, because it is the default serialization format. It would be nice if you could round trip acsets between Julia and JSON without having to explicitly specify the AttrTypes. In the future, it would be even nicer to support other formats with default primitive type assignments, e.g. Apache Arrow or SQLite.

Of course the user should still always be able to provide their own custom type assignments, but it would be nice to be able to omit them if you are only using primitive types from certain known formats/languages.

@filonik-cmu
Copy link

filonik-cmu commented Jul 12, 2023

Perhaps better way of framing what I was trying to get across above is that there are two mappings at play:

  1. Catlab AttrTypes to Julia types (type assignments).
  2. Julia types to types in the serialized representation (e.g. JSON).

The simplest case would be when both mappings are trivial, in which case it would be nice if the user didn't have to explicitly specify them at all.

@KevinDCarlson
Copy link
Author

New comments from Daniel today:

The real problem is that we have our schema in Catlab and it's generic over the actual types populating the AttrTypes to create a concrete acset type. Thus we'd like to serialize a schema with fixed AttrTypes, probably both on its own and along with the serialization of an instance. We mainly just need primitive types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants