aminya / AcuteML.jl

Acute Markup Language - HTML/XML in Julia
https://aminya.github.io/AcuteML.jl/dev
MIT License
6 stars 4 forks source link

Consider using a trait based API instead of macros #153

Open kskyten opened 3 years ago

kskyten commented 3 years ago

Nice package! I really like the way JSON3.jl handles mapping structs to JSON and back. It does this by annotating structs with traits from StructTypes.jl. I wonder if a similar approach could be used with AcuteML. This way you could easily serialize your struct in either JSON or XML. I'm not sure how well XML maps to the existing StructTypes traits, but there is probably some overlap.

kskyten commented 3 years ago

Related to JuliaData/StructTypes.jl#7

aminya commented 3 years ago

AcuteML is like the JSX of Julia. This library aims to make it easy for developers to build their HTML or XML applications with the least amount of boilerplate code. This has several benefits:

# Here you can serialize any HTML that follows this structure. Serialization here means both parsing and creating.

@aml mutable struct Body "~"
    h1, "~"
    p::Vector{String}, "~"
end

@aml mutable struct Page doc"html"
    body::Body, "~"
end

See MusicXML.jl for an example of a library that uses AcuteML: https://github.com/JuliaMusic/MusicXML.jl

If you plan to inject custom code to your AcuteML components there are multiple ways you can do this: https://aminya.github.io/AcuteML.jl/dev/extraConstructors/ https://aminya.github.io/AcuteML.jl/dev/customValueTypes/ https://aminya.github.io/AcuteML.jl/dev/customConstructors/

Extra Contructors · AcuteML.jl
Custom Value Types · AcuteML.jl
Custom Contructors · AcuteML.jl

You can also directly use the AcuteML's backend if you wish (although not necessary at all).

DOM/XPATH API of AcuteML.jl

I wrote VarStructs which is similar to AcuteML, but is for any type of data. You can easily define your requirements for data using VarStructs, and everything will be handled for you.

GitHub
aminya/VarStructs.jl
Variable Julia Structs with dispatching. Contribute to aminya/VarStructs.jl development by creating an account on GitHub.
kskyten commented 3 years ago

I don't necessarily see how what I'm thinking of is mutually exclusive. Here is an example to clarify this. Say I have a struct

struct Foo
  x::String
  y::Int

and I want to serialize it in XML, JSON and possible some other serialization format. If each of the serialization libraries defines their custom APIs with macros, it is difficult to combine them and they have to be added as dependencies.

I'm asking whether it is possible to use a common API for defining the mapping to various serialization formats like Rust's Serde does. As far as I know, StructTypes.jl comes the closest to this kind of API. If it doesn't suit AcuteML, it would be useful to know what is missing so a better API can be built.