Marshal()
(type)Converts structured memory into a structured file format (eg for stdio)
This is a function you would write when programming a Murex
data-type. The marshal function takes in a Go (golang) type
or struct
and returns a byte slice of a “string”
representation of that format (eg in JSON) or an error.
This marshaller is then registered to Murex inside an
init()
function and Murex builtins can use that marshaller
via the MarshalData()
API.
Registering Marshal()
(for writing builtin
data-types)
// To avoid data races, this should only happen inside func init()
.Marshallers[ /* your type name */ ] = /* your readIndex func */ lang
Using an existing marshaller (eg inside a builtin command)
// See documentation on lang.MarshalData for more details
, err := lang.MarshalData(p, dataType, data) b
Defining a marshaller for a murex data-type
package example
import (
"encoding/json"
"github.com/lmorg/murex/lang"
)
func init() {
// Register data-type
.Marshallers["json"] = marshal
lang}
// Describe marshaller
func marshal(p *lang.Process, v interface{}) ([]byte, error) {
if p.Stdout.IsTTY() {
// If STDOUT is a TTY (ie not pipe, text file or other destination other
// than a terminal) then output JSON in an indented, human readable,
// format....
return json.MarshalIndent(v, "", " ")
}
// ....otherwise we might as well output it in a minified format
return json.Marshal(v)
}
*lang.Process
: Process’s runtime state. Typically
expressed as the variable p
interface{}
: data you wish to marshalUnmarshal()
(type): Converts a structured file format into structured
memorylang.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_marshal_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.