Hi @Mng12345 thanks for clarifying.
Probably the simplest option here is to only use arrays, following the suggestion from @carere E.g.
// MyLib.res
type file = {
name: string,
path: string,
}
type files = Js.Dict.t<array<file>>
@module("mylib") external handleFiles: files => unit = "handler"
Example call:
let file1: MyLib.file = {name: "name1", path: "path1"}
let file2: MyLib.file = {name: "name2", path: "path2"}
let file3: MyLib.file = {name: "name3", path: "path3"}
let files = Js.Dict.empty()
files->Js.Dict.set("1", [file1])
files->Js.Dict.set("2", [file2, file3])
MyLib.handleFiles(files)
If you think it might be useful to use both single files and arrays of files, then you could do something like this:
// MyLib.res
type file = {
name: string,
path: string,
}
type fileOrFiles = File(file) | Files(array<file>)
// Loop through each value
// [1] Unwrap to just the file or array<file>
// [2] Encode as Js.Json.t using techniques described above
let encode: Js.Dict.t<fileOrFiles> => Js.Dict.t<Js.Json.t> = // TODO
// Helper external function
@module("mylib") external handleFiles_: Js.Dict.t<Js.Json.t> => unit = "handler"
let handleFiles: Js.Dict.t<fileOrFiles> => unit = files => handleFiles_(encode(files))
And use it something like this:
let files = Js.Dict.empty()
files->Js.Dict.set("1", MyLib.File({name: "name1", path: "path1"}))
files->Js.Dict.set(
"2",
MyLib.Files([{name: "name2", path: "path2"}, {name: "name3", path: "path3"}]),
)
MyLib.handleFiles(files)
There are some improvements to make this a little more type safe, but this is hopefully a simple start.
How does that look to you?