Export an F-Mesh mesh as indented JSON: its structure, or its state in one cycle of a run. Use it to feed other tools, diff two versions of a mesh, or snapshot a mesh in a test.
New() returns an Exporter. It implements this module's
export.Exporter, like every format in this
module. It has no options and no state: reuse one value for many meshes.
go get github.com/hovsep/fmesh-export/jsonimport "github.com/hovsep/fmesh-export/json"
data, err := json.New().Export(fm){
"name": "pricing",
"meta": {"env": "prod"},
"components": [
{"name": "dst", "inputs": [{"name": "in"}], "outputs": []},
{"name": "src", "description": "emits prices", "inputs": [], "outputs": [{"name": "out"}]}
],
"pipes": [
{"from": {"component": "src", "port": "out"}, "to": {"component": "dst", "port": "in"}}
]
}- Components and ports are in name order. Pipes are grouped by source port, in the same order, and each port's pipes are in wiring order. The same mesh always exports the same bytes.
descriptionandmetaare left out when empty.components,inputs,outputsandpipesare always present.- Unmarshal into
json.Meshto read it back.
ri, err := fm.Run(ctx)
for _, c := range ri.Cycles.All() {
frame, err := json.New().ExportCycle(fm, c)
}The document is a json.Cycle: the cycle number, the mesh structure, and one results entry
per component, in name order:
{"component": "dst", "code": "Finished with error", "activated": true, "errors": ["component returned an error: boom"]}- A component with no result in the cycle had no input: its code is
No input. - A nil mesh returns
export.ErrNilMesh, and a nil cycleexport.ErrNilCycle. - To export each cycle while the mesh runs, call
ExportCyclefrom anAfterCyclehook.
If you also import the standard library's encoding/json in the same file, give one of them an
alias.