Stream: contributing

Topic: Glue generate Types in JSON


view this post on Zulip Luke Boswell (Apr 21 2024 at 09:02):

Is this useful for others? Is it worth adding in a PR?

My goal was to understand the platform types better.

For example if I run roc glue JsonGlue.roc test-glue/ main.roc on the following, i get the below JSON. Note I had to add Encoding ability to the Types which was an extremely minor addition.

platform "test"
    requires {} { main : Foo }
    exposes []
    packages {}
    imports []
    provides [mainForHost]

Foo := [Number U8, String Str]

mainForHost : Foo
mainForHost = main

Generates types.json

{
    "aligns": [1, 1, 8, 8],
    "deps": [],
    "entrypoints": [{ "T": ["mainForHost", 3] }],
    "sizes": [1, 1, 24, 32],
    "target": {
        "architecture": { "X86x64": [] },
        "operatingSystem": { "Mac": [] }
    },
    "types": [
        { "Unit": [] },
        { "Num": [{ "U8": [] }] },
        { "RocStr": [] },
        {
            "TagUnion": [
                {
                    "NonRecursive": [
                        {
                            "discriminantOffset": 24,
                            "discriminantSize": 1,
                            "name": "Foo",
                            "tags": [
                                {
                                    "name": "Number",
                                    "payload": { "Some": [1] }
                                },
                                { "name": "String", "payload": { "Some": [2] } }
                            ]
                        }
                    ]
                }
            ]
        }
    ],
    "typesByName": [{ "T": ["Foo", 3] }]
}

I tested Inspect out too, which works well, but I just wanted a format that I could read in my editor and easily format with newlines etc.

view this post on Zulip Luke Boswell (Apr 21 2024 at 09:03):

app "json-glue"
    packages {
        pf: "../platform/main.roc",
        json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.7.0/xuaMzXRVG_SEhOFZucS3iBozlRdObWsfKaYZMHVE_q0.tar.br",
    }
    imports [
        pf.Types.{ Types },
        pf.File.{ File },
        json.Core.{json},
    ]
    provides [makeGlue] to pf

makeGlue : List Types -> Result (List File) Str
makeGlue = \typesByArch ->
    typesByArch
    |> List.map convertTypesToFile
    |> List.concat staticFiles
    |> Ok

staticFiles : List File
staticFiles = []

convertTypesToFile : Types -> File
convertTypesToFile = \types ->

    content =
        Encode.toBytes types json
        |> Str.fromUtf8
        |> Result.withDefault "INVALID UTF8 ENCODED FROM JSON"

    { name: "types.json", content  }

view this post on Zulip Anton (Apr 22 2024 at 09:36):

Perhaps a good fit for the examples repo?

view this post on Zulip Luke Boswell (Apr 23 2024 at 22:39):

Given it's a super small change, and this might help others. I'll make a quick PR to add the Encoding, and Inspect abilities to the Types so others can do this without changing the compiler.

view this post on Zulip Luke Boswell (Apr 23 2024 at 22:47):

Ok PR is https://github.com/roc-lang/roc/pull/6670


Last updated: Jul 06 2025 at 12:14 UTC