lang.ArrayTemplate()
(template API) - API ReferenceUnmarshals a data type into a Go struct and returns the results as an array
This is a template API you can use for your custom data types to wrap around an existing Go marshaller and return a Murex array which is consistent with other structures such as nested JSON or YAML documents.
It should only be called from ReadArray()
functions.
Because lang.ArrayTemplate()
relies on a marshaller, it means any types that rely on this API are not going to be stream-able.
Example calling lang.ArrayTemplate()
function:
package json
import (
"context"
"github.com/lmorg/murex/lang"
"github.com/lmorg/murex/lang/stdio"
"github.com/lmorg/murex/utils/json"
)
func readArray(ctx context.Context, read stdio.Io, callback func([]byte)) error {
// Create a marshaller function to pass to ArrayTemplate
func(v interface{}) ([]byte, error) {
marshaller := return json.Marshal(v, read.IsTTY())
}
return lang.ArrayTemplate(ctx, marshaller, json.Unmarshal, read, callback)
}
package lang
import (
"context"
"fmt"
"github.com/lmorg/murex/lang/stdio"
"github.com/lmorg/murex/utils"
)
// ArrayTemplate is a template function for reading arrays from marshalled data
func ArrayTemplate(ctx context.Context, marshal func(interface{}) ([]byte, error), unmarshal func([]byte, interface{}) error, read stdio.Io, callback func([]byte)) error {
b, err := read.ReadAll()if err != nil {
return err
}
if len(utils.CrLfTrim(b)) == 0 {
return nil
}
var v interface{}
err = unmarshal(b, &v)
if err != nil {
return err
}
switch v := v.(type) {
case string:
return readArrayByString(v, callback)
case []string:
return readArrayBySliceString(ctx, v, callback)
case []interface{}:
return readArrayBySliceInterface(ctx, marshal, v, callback)
case map[string]string:
return readArrayByMapStrStr(ctx, v, callback)
case map[string]interface{}:
return readArrayByMapStrIface(ctx, marshal, v, callback)
case map[interface{}]string:
return readArrayByMapIfaceStr(ctx, v, callback)
case map[interface{}]interface{}:
return readArrayByMapIfaceIface(ctx, marshal, v, callback)
default:
jBytes, err := marshal(v)if err != nil {
return err
}
callback(jBytes)
return nil
}
}
func readArrayByString(v string, callback func([]byte)) error {
byte(v))
callback([]
return nil
}
func readArrayBySliceString(ctx context.Context, v []string, callback func([]byte)) error {
for i := range v {
select {
case <-ctx.Done():
return nil
default:
byte(v[i]))
callback([]
}
}
return nil
}
func readArrayBySliceInterface(ctx context.Context, marshal func(interface{}) ([]byte, error), v []interface{}, callback func([]byte)) error {
if len(v) == 0 {
return nil
}
for i := range v {
select {
case <-ctx.Done():
return nil
default:
switch v := v[i].(type) {
case string:
byte(v))
callback([]
case []byte:
callback(v)
default:
jBytes, err := marshal(v)if err != nil {
return err
}
callback(jBytes)
}
}
}
return nil
}
func readArrayByMapIfaceIface(ctx context.Context, marshal func(interface{}) ([]byte, error), v map[interface{}]interface{}, callback func([]byte)) error {
for key, val := range v {
select {
case <-ctx.Done():
return nil
default:
byte(fmt.Sprint(key) + ": ")
bKey := []
b, err := marshal(val)if err != nil {
return err
}append(bKey, b...))
callback(
}
}
return nil
}
func readArrayByMapStrStr(ctx context.Context, v map[string]string, callback func([]byte)) error {
for key, val := range v {
select {
case <-ctx.Done():
return nil
default:
byte(key + ": " + val))
callback([]
}
}
return nil
}
func readArrayByMapStrIface(ctx context.Context, marshal func(interface{}) ([]byte, error), v map[string]interface{}, callback func([]byte)) error {
for key, val := range v {
select {
case <-ctx.Done():
return nil
default:
byte(key + ": ")
bKey := []
b, err := marshal(val)if err != nil {
return err
}append(bKey, b...))
callback(
}
}
return nil
}
func readArrayByMapIfaceStr(ctx context.Context, v map[interface{}]string, callback func([]byte)) error {
for key, val := range v {
select {
case <-ctx.Done():
return nil
default:
byte(fmt.Sprint(key) + ": " + val))
callback([]
}
}
return nil
}
func(interface{}) ([]byte, error)
: data type’s marshallerfunc([]byte, interface{}) error
: data type’s unmarshallerstdio.Io
: stream to read from (eg STDIN)func([]byte)
: callback function to write each array elementReadArray()
(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, ![
, builtinWriteArray()
(type): Write a data type, one array element at a timelang.IndexTemplateObject()
(template API): Returns element(s) from a data structurelang.IndexTemplateTable()
(template API): Returns element(s) from a tableThis 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 Fri May 19 22:45:48 UTC 2023 against commit 54b5f6754b5f67b250bbf7353e83c42ed187802584c3ae3.
Current version is 4.1.6140 which has been verified against 14045 tests cases.