xaml-codegen

    A command-line program that writes the XAML codecs of MoonBit types from their attributes

    xaml
    xml
    serialization
    codegen
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    18 hours ago
    Downloads
    11

    #moonrockz/xaml-codegen

    A command-line program that writes the XAML codecs of your MoonBit types. You describe a type with attributes in its declaration, and a build rule runs the program when the file changes.

    moonrockz/xaml reads and writes a value when its type has an implementation of the trait Xaml. You can write it by hand, but then each field stands there three times, beside its declaration. With this program the declaration is enough:

    // models.mbt

    ///|
    #xaml.type(namespace="urn:tasks")
    pub(all) struct Task {
    #xaml.member(is_name=true)
    id : String
    title : String
    #xaml.member(default=false)
    done : Bool
    after : Task?
    #xaml.member(content=true)
    tasks : Array[Task]
    }

    This type reads and writes documents such as:

    <Task xmlns="urn:tasks" Title="Release"> <Task Done="True" Title="Publish"/> </Task>

    #Set it up

    The package of your types imports moonrockz/xaml, and its moon.pkg has a rule that runs the program:

    import {
    "moonrockz/xaml",
    }

    rule(
    name: "xaml",
    command: "moonx moonrockz/xaml-codegen@<version> $input -o $output",
    )

    dev_build(rule: "xaml", input: ["models.mbt"], output: "models.g.mbt")

    <version> is the version of moonrockz/xaml that you use. moonx is a part of the MoonBit toolchain. It downloads the program and runs it, so there is nothing to install. From then on moon check, moon build and moon test write models.g.mbt again after a change.

    • Commit the generated file. Moon does not run the rules of packages that others depend on.
    • Several inputs give one output. Constants that the attributes name can be in any of them, or in another file.
    • Import moonrockz/xaml without an alias, because the code uses the name @xaml.

    #The code

    For the type above the program writes:

    // Generated by xaml-codegen from models.mbt. Do not edit.

    ///|
    let xaml_key_Task : @xaml.XamlKey[Task] = @xaml.XamlKey::new(
    "urn:tasks", "Task",
    )

    ///|
    pub impl @xaml.Xaml for Task with fn xaml_codec() {
    xaml_key_Task.object(object => {
    let m_id = object.add_name("Id", it => it.id)
    let m_title = object.add_member("Title", it => it.title)
    let m_done = object.add_member("Done", it => it.done, default=false)
    let m_after = object.add_member("After", it => it.after)
    let m_tasks = object.add_list("Tasks", it => it.tasks, content=true)
    members => Task::{
    id: members.get(m_id),
    title: members.get(m_title),
    done: members.get(m_done),
    after: members.get(m_after),
    tasks: members.get(m_tasks),
    }
    })
    }

    This is what you would write by hand, so you can read it and step through it. If you need something that the attributes cannot say, take the code over: copy it to your file, change it, and remove the attributes.

    #Attributes

    AttributeOnArguments
    #xaml.typeA struct or an enumnamespace, name
    #xaml.memberA field, or a value of a casename, default, content, is_name, of
    #xaml.caseA case of an enumname, inline

    Every field is a member, so most need no attribute.

    • of is for a case only, because the values of a case cannot have attributes. The attribute is on the case, and of=width names the label of the value.
    • namespace is a text or the name of a constant, such as namespace=TASKS.
    • name replaces the name in XAML. Without it, a type and a case have their own names, and the member of story_points is StoryPoints.
    • content=true puts the member into the element itself, without an element such as <Task.Tasks>.
    • is_name=true makes a String the name of the object, with which x:Reference finds it.
    • A field of the type Array is a list, and one of the type Map is a map. Both are empty when a document does not give them.
    • Any other member is required, unless it has a default or its type is optional.

    #Defaults

    TypeExample
    Booldefault=false
    An enumdefault=Low
    A numberdefault="5", default="-1.5"
    String, Chardefault="none"
    An optional onedefault=Some("5"), default=None
    Any of them, and all othersdefault=usual_limit, the name of a constant or a let

    Numbers are in quotes because the attributes of MoonBit have none without them. The program checks that the text is a number of the type.

    #Enums

    An enum in which no case has a value is an enumeration: Priority="High" is the case High.

    In any other enum each case is an element, and its values are its members:

    // shapes.mbt

    ///|
    #xaml.type(namespace="urn:shapes")
    pub(all) struct Image {
    source : String
    }

    ///|
    #xaml.type(namespace="urn:shapes")
    pub(all) enum Shape {
    Circle(radius~ : Double)
    #xaml.member(of=height, default="1")
    Rectangle(width~ : Double, height~ : Double)
    Line(Double, Double)
    #xaml.case(name="Text")
    Label(String)
    Group(Array[Shape])
    #xaml.case(inline=true)
    Picture(Image)
    Empty
    }

    <Group xmlns="urn:shapes"> <Circle Radius="1.5"/> <Rectangle Width="2"/> <Line Item1="3" Item2="4"/> <Text>hi</Text> <Image Source="logo.png"/> <Empty/> </Group>

    • A value with a label has the name that a field would have.
    • The only value without a label is the member Item and the content of the element. Several ones are Item1, Item2 and so on, as in F#.
    • inline=true is for a case with one value whose type has an element of its own, as Image here, or a text.
    • Two cases in one namespace cannot have the same name, also when they are in different enums. #xaml.case(name=...) gives one of them another name.
    • When an enumeration gets its first case with a value, its documents change: Shape="Empty" is an element from then on.

    #What you describe by hand

    • A type with one text for the whole value, such as 1,2 for a point, with XamlKey::text.
    • A type with checks. The code creates a struct from its fields, and does not call a constructor function.
    • Generic types, and structs without names for their fields.
    • A list in a list, which needs a struct with the inner list.

    Types with attributes and types that are described by hand can be members of each other. Read and write values shows how to write a description.

    #Mistakes

    The compiler does not look at attributes of others, so the program does. A mistake stops the build with its place:

    models.mbt:8:3: #xaml.member: there is no argument defualt, only name, default, content and is_name

    The program reports all mistakes at once. It writes nothing then, so the last good code stays. It cannot see whether the type of a member has an implementation of Xaml. If there is none, the compiler reports it in the generated file, at the line of the member.

    #The command

    Usage: xaml-codegen [--check] <input.mbt>... -o <output.mbt>

    --check writes nothing, and fails when the output is missing or has other code. Use it in CI:

    moonx moonrockz/xaml-codegen@<version> --check models.mbt -o models.g.mbt

    A package can also do without the rule and run the command in a script. Then name the output in moon.pkg, so that moon fmt does not change it:

    formatter(ignore: [ "models.g.mbt" ])

    version

    fn version() -> String

    The version of this module.