ReadArrayWithType()
(type)Read from a data type one array element at a time and return the elements contents and data type
This is a function you would write when programming a Murex data-type.
It’s called by builtins to allow them to read data structures one array element at a time.
The purpose of this function is to allow builtins to support sequential reads (where possible) and also create a standard interface for builtins, thus allowing them to be data-type agnostic.
This differs from ReadArray() because it also returns the data type.
There is a good chance ReadArray() might get deprecated in the medium to long term.
Registering your ReadArrayWithType()
// To avoid confusion, this should only happen inside func init()
/* your type name */, /* your readArray func */) stdio.RegisterReadArrayWithType(
Example ReadArrayWithType()
function:
package string
import (
"bufio"
"context"
"fmt"
"strings"
"github.com/lmorg/murex/lang/stdio"
"github.com/lmorg/murex/lang/types"
)
func readArrayWithType(ctx context.Context, read stdio.Io, callback func(interface{}, string)) error {
scanner := bufio.NewScanner(read)for scanner.Scan() {
select {
case <-ctx.Done():
return scanner.Err()
default:
callback(strings.TrimSpace(scanner.Text()), types.String)
}
}
err := scanner.Err()if err != nil {
return fmt.Errorf("error while reading a %s array: %s", types.String, err.Error())
}return nil
}
If your data type is not a stream-able array, it is then recommended that you pass your array to lang.ArrayWithTypeTemplate()
which is a handler to convert Go structures into Murex arrays. This also makes writing ReadArray()
handlers easier since you can just pass lang.ArrayTemplate()
your marshaller. For example:
package json
import (
"context"
"github.com/lmorg/murex/lang"
"github.com/lmorg/murex/lang/stdio"
"github.com/lmorg/murex/lang/types"
"github.com/lmorg/murex/utils/json"
)
func readArrayWithType(ctx context.Context, read stdio.Io, callback func(interface{}, string)) error {
// Create a marshaller function to pass to ArrayWithTypeTemplate
func(v interface{}) ([]byte, error) {
marshaller := return json.Marshal(v, read.IsTTY())
}
return lang.ArrayWithTypeTemplate(ctx, types.Json, marshaller, json.Unmarshal, read, callback)
}
The downside of this is that you’re then unmarshalling the entire file, which could be slow on large files and also breaks the streaming nature of UNIX pipelines.
stdio.Io
: stream to read from (eg stdin)func(interface{}, string)
: callback function. Each callback will be the value in its native Go data type (eg string, int, float64, bool) for an array elementReadIndex()
(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, ![
, builtinWriteArray()
(type): Write a data type, one array element at a timelang.ArrayTemplate()
(template API): Unmarshals a data type into a Go struct and returns the results as an arraylang.ArrayWithTypeTemplate()
(template API): Unmarshals a data type into a Go struct and returns the results as an array with data type includedThis 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 Sep 18 21:18:57 UTC 2024 against commit c037883c03788357164e9846c84d9f777251495d9452a8e.
Current version is 6.3.4225 (develop) which has been verified against tests cases.