Unmarshal()
(type)Converts a structured file format into structured memory
This is a function you would write when programming a Murex
data-type. The unmarshal function takes in a byte slice and returns a Go
(golang) type
or struct
or an error.
This unmarshaller is then registered to Murex inside an
init()
function and Murex builtins can use that
unmarshaller via the UnmarshalData()
API.
Registering Unmarshal()
(for writing builtin
data-types)
// To avoid data races, this should only happen inside func init()
.Unmarshallers[ /* your type name */ ] = /* your readIndex func */ lang
Using an existing unmarshaller (eg inside a builtin command)
// See documentation on lang.UnmarshalData for more details
, err := lang.UnmarshalData(p *lang.Process, dataType string) v
Defining a marshaller for a murex data-type
package example
import (
"encoding/json"
"github.com/lmorg/murex/lang"
)
func init() {
// Register data-type
.Unmarshallers["example"] = unmarshal
lang}
// Describe unmarshaller
func unmarshal(p *lang.Process) (interface{}, error) {
// Read data from STDIN. Because JSON expects closing tokens, we should
// read the entire stream before unmarshalling it. For formats like CSV or
// jsonlines which are more line based, we might want to read STDIN line by
// line. However given there is just one data return, you still effectively
// head to read the entire file before returning the structure. There are
// other APIs for iterative returns for streaming data - more akin to the
// traditional way UNIX pipes would work.
, err := p.Stdin.ReadAll()
bif err != nil {
return nil, err
}
var v interface{}
= json.Unmarshal(b, &v)
err
// Return the Go data structure or error
return v, err
}
*lang.Process
: Process’s runtime state. Typically
expressed as the variable p
Marshal()
(type):
Converts structured memory into a structured file format (eg for
stdio)lang.MarshalData()
(system API): Converts structured memory into a Murex data-type (eg
for stdio)lang.UnmarshalData()
(system API): Converts a Murex data-type into structured memoryThis document was generated from lang/define_unmarshal_doc.yaml.
This site's content is rebuilt automatically from murex's source code after each merge to the master
branch. Downloadable murex binaries are also built with the website.
Last built on Wed Jan 15 23:07:50 UTC 2025 against commit b4c4296b4c429617fd41527ea0efef33c52c15ef2b64972.
Current version is 6.4.2063 (develop) which has been verified against tests cases.