WriteArray()
(type)Write a data type, one array element at a time
This is a function you would write when programming a Murex data-type.
It’s called by builtins to allow them to write data structures one array element at a time.
The purpose of this function is to allow builtins to support sequential writes (where possible) and also create a standard interface for builtins, thus allowing them to be data-type agnostic.
WriteArray()
should return a struct
that
satisfies the following interface{}
:
package stdio
// ArrayWriter is a simple interface types can adopt for buffered writes of formatted arrays in structured types (eg JSON)
type ArrayWriter interface {
([]byte) error
Write(string) error
WriteString() error
Close}
Registering your WriteArray()
// To avoid confusion, this should only happen inside func init()
.RegisterWriteArray(/* your type name */, /* your writeArray func */) stdio
Example WriteArray()
function:
package string
import (
"github.com/lmorg/murex/lang/stdio"
)
type arrayWriter struct {
.Io
writer stdio}
func newArrayWriter(writer stdio.Io) (stdio.ArrayWriter, error) {
:= &arrayWriter{writer: writer}
w return w, nil
}
func (w *arrayWriter) Write(b []byte) error {
, err := w.writer.Writeln(b)
_return err
}
func (w *arrayWriter) WriteString(s string) error {
, err := w.writer.Writeln([]byte(s))
_return err
}
func (w *arrayWriter) Close() error { return nil }
Since not all data types will be stream-able (for example
json
), some types may need to cache the array and then to
write it once the array writer has been closed.
package json
import (
"github.com/lmorg/murex/lang/stdio"
"github.com/lmorg/murex/utils/json"
)
type arrayWriter struct {
[]string
array .Io
writer stdio}
func newArrayWriter(writer stdio.Io) (stdio.ArrayWriter, error) {
:= &arrayWriter{writer: writer}
w return w, nil
}
func (w *arrayWriter) Write(b []byte) error {
.array = append(w.array, string(b))
wreturn nil
}
func (w *arrayWriter) WriteString(s string) error {
.array = append(w.array, s)
wreturn nil
}
func (w *arrayWriter) Close() error {
, err := json.Marshal(w.array, w.writer.IsTTY())
bif err != nil {
return err
}
, err = w.writer.Write(b)
_return err
}
ReadArray()
(type): Read from a data type one array element at a timeReadArrayWithType()
(type): Read from a data type one array element at a time and return
the elements contents and data typeReadIndex()
(type): Data type handler for the index, [
,
builtinReadMap()
(type):
Treat data type as a key/value structure and read its contentsReadNotIndex()
(type): Data type handler for the bang-prefixed index,
![
, builtinThis document was generated from lang/stdio/interface_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.