lang.IndexTemplateTable()
(template API)Returns element(s) from a table
This is a template API you can use for your custom data types.
It should only be called from ReadIndex()
and ReadNotIndex()
functions.
This function ensures consistency with the index, [
, builtin when used with different Murex data types. Thus making indexing a data type agnostic capability.
Example calling lang.IndexTemplateTable()
function:
package generic
import (
"bytes"
"strings"
"github.com/lmorg/murex/lang"
)
func index(p *lang.Process, params []string) error {
make(chan []string, 1)
cRecords :=
go func() {
func(b []byte) {
err := p.Stdin.ReadLine(string(bytes.TrimSpace(b)), -1)
cRecords <- rxWhitespace.Split(
})if err != nil {
byte(err.Error()))
p.Stderr.Writeln([]
}close(cRecords)
}()
func(s []string) (b []byte) {
marshaller := byte(strings.Join(s, "\t"))
b = []return
}
return lang.IndexTemplateTable(p, params, cRecords, marshaller)
}
package lang
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/lmorg/murex/utils"
)
const (
iota + 1
byRowNumber =
byColumnNumber
byColumnName
5
maxReportedUnmatched =
)
var (
`^:[0-9]+$`)
rxColumnPrefixOld = regexp.MustCompile(`^[0-9]+:$`)
rxRowSuffixOld = regexp.MustCompile(`^\*[a-zA-Z]$`)
rxColumnPrefixNew = regexp.MustCompile(`^\*[0-9]+$`)
rxRowSuffixNew = regexp.MustCompile("you cannot mix and match matching modes")
errMixAndMatch = errors.New(
)
// IndexTemplateTable is a handy standard indexer you can use in your custom data types for tabulated / streamed data.
// The point of this is to minimize code rewriting and standardising the behavior of the indexer.
func IndexTemplateTable(p *Process, params []string, cRecords chan []string, marshaller func([]string) []byte) error {
if p.IsNot {
return ittNot(p, params, cRecords, marshaller)
}return ittIndex(p, params, cRecords, marshaller)
}
func charToIndex(b byte) int {
if b > 96 {
return int(b - 97)
}return int(b - 65)
}
func ittIndex(p *Process, params []string, cRecords chan []string, marshaller func([]string) []byte) (err error) {
var (
int
mode string
matchStr []int
matchInt []string
unmatched []int
unmatchedCount
)
defer func() {
if len(unmatched) != 0 {
1
p.ExitNum = if unmatchedCount > maxReportedUnmatched {
append(unmatched, fmt.Sprintf("...plus %d more", unmatchedCount-maxReportedUnmatched))
unmatched =
}"some records did not contain all of the requested fields:%s%s",
err = fmt.Errorf(
utils.NewLineString,
strings.Join(unmatched, utils.NewLineString))
}
}()
func(recs []string) {
errUnmatched :=
unmatchedCount++if unmatchedCount > maxReportedUnmatched {
return
}append(unmatched, strings.Join(recs, "\t"))
unmatched =
}
for i := range params {
switch {
case rxRowSuffixOld.MatchString(params[i]):
if mode != 0 && mode != byRowNumber {
return errMixAndMatch
}
mode = byRowNumberlen(params[i])-1])
num, _ := strconv.Atoi(params[i][:append(matchInt, num)
matchInt =
case rxRowSuffixNew.MatchString(params[i]):
if mode != 0 && mode != byRowNumber {
return errMixAndMatch
}
mode = byRowNumber1:])
num, _ := strconv.Atoi(params[i][append(matchInt, num-1) // Don't count from zero
matchInt =
case rxColumnPrefixOld.MatchString(params[i]):
if mode != 0 && mode != byColumnNumber {
return errMixAndMatch
}
mode = byColumnNumber1:])
num, _ := strconv.Atoi(params[i][append(matchInt, num)
matchInt =
case rxColumnPrefixNew.MatchString(params[i]):
if mode != 0 && mode != byColumnNumber {
return errMixAndMatch
}
mode = byColumnNumber1])
num := charToIndex(params[i][append(matchInt, num)
matchInt =
default:
if mode != 0 && mode != byColumnName {
return errMixAndMatch
}append(matchStr, params[i])
matchStr =
mode = byColumnName
}
}
switch mode {
case byRowNumber:
var (
true
ordered = int
last int
max
)// check order
for _, i := range matchInt {
if i < last {
false
ordered =
}if i > max {
max = i
}
last = i
}
if ordered {
// ordered matching - for this we can just read in the records we want sequentially. Low memory overhead
var i int
for {
recs, ok := <-cRecordsif !ok {
return nil
}if i == matchInt[0] {
_, err = p.Stdout.Writeln(marshaller(recs))if err != nil {
byte(err.Error()))
p.Stderr.Writeln([]
}if len(matchInt) == 1 {
0] = -1
matchInt[return nil
}1:]
matchInt = matchInt[
}
i++
}
else {
} // unordered matching - for this we load the entire data set into memory - up until the maximum value
var (
int
i make([][]string, max+1)
lines =
)for {
recs, ok := <-cRecordsif !ok {
break
}if i <= max {
lines[i] = recs
}
i++
}
for _, j := range matchInt {
_, err = p.Stdout.Writeln(marshaller(lines[j]))if err != nil {
byte(err.Error()))
p.Stderr.Writeln([]
}
}
return nil
}
case byColumnNumber:
for {
recs, ok := <-cRecordsif !ok {
return nil
}
var line []string
for _, i := range matchInt {
if i < len(recs) {
append(line, recs[i])
line = else {
} if len(recs) == 0 || (len(recs) == 1 && recs[0] == "") {
continue
}
errUnmatched(recs)
}
}if len(line) != 0 {
_, err = p.Stdout.Writeln(marshaller(line))if err != nil {
byte(err.Error()))
p.Stderr.Writeln([]
}
}
}
case byColumnName:
var (
int
lineNum make(map[string]int)
headings =
)
for {
var line []string
recs, ok := <-cRecordsif !ok {
return nil
}
if lineNum == 0 {
for i := range recs {
1
headings[recs[i]] = i +
}for i := range matchStr {
if headings[matchStr[i]] != 0 {
append(line, matchStr[i])
line =
}
}if len(line) != 0 {
_, err = p.Stdout.Writeln(marshaller(line))if err != nil {
byte(err.Error()))
p.Stderr.Writeln([]
}
}
else {
} for i := range matchStr {
col := headings[matchStr[i]]if col != 0 && col < len(recs)+1 {
append(line, recs[col-1])
line = else {
} if len(recs) == 0 || (len(recs) == 1 && recs[0] == "") {
continue
}
errUnmatched(recs)
}
}if len(line) != 0 {
_, err = p.Stdout.Writeln(marshaller(line))if err != nil {
byte(err.Error()))
p.Stderr.Writeln([]
}
}
}
lineNum++
}
default:
return errors.New("you haven't selected any rows / columns")
}
}
func ittNot(p *Process, params []string, cRecords chan []string, marshaller func([]string) []byte) error {
var (
int
mode make(map[string]bool)
matchStr = make(map[int]bool)
matchInt =
)
for i := range params {
switch {
case rxRowSuffixOld.MatchString(params[i]):
if mode != 0 && mode != byRowNumber {
return errMixAndMatch
}
mode = byRowNumberlen(params[i])-1])
num, _ := strconv.Atoi(params[i][:true
matchInt[num] =
case rxRowSuffixNew.MatchString(params[i]):
if mode != 0 && mode != byRowNumber {
return errMixAndMatch
}
mode = byRowNumber1:])
num, _ := strconv.Atoi(params[i][1] = true // Don't count from zero
matchInt[num+
case rxColumnPrefixOld.MatchString(params[i]):
if mode != 0 && mode != byColumnNumber {
return errMixAndMatch
}
mode = byColumnNumber1:])
num, _ := strconv.Atoi(params[i][true
matchInt[num] =
case rxColumnPrefixNew.MatchString(params[i]):
if mode != 0 && mode != byColumnNumber {
return errMixAndMatch
}
mode = byColumnNumber1])
num := charToIndex(params[i][true
matchInt[num] =
default:
if mode != 0 && mode != byColumnName {
return errMixAndMatch
}true
matchStr[params[i]] =
mode = byColumnName
}
}
switch mode {
case byRowNumber:
-1
i := for {
recs, ok := <-cRecordsif !ok {
return nil
}
if !matchInt[i] {
_, err := p.Stdout.Writeln(marshaller(recs))if err != nil {
byte(err.Error()))
p.Stderr.Writeln([]
}
}
i++
}
case byColumnNumber:
for {
recs, ok := <-cRecordsif !ok {
return nil
}
var line []string
for i := range recs {
if !matchInt[i] {
append(line, recs[i])
line =
}
}if len(line) != 0 {
p.Stdout.Writeln(marshaller(line))
}
}
case byColumnName:
var (
int
lineNum make(map[int]string)
headings =
)
for {
var line []string
recs, ok := <-cRecordsif !ok {
return nil
}
if lineNum == 0 {
for i := range recs {
headings[i] = recs[i]if !matchStr[headings[i]] {
append(line, recs[i])
line =
}
}if len(line) != 0 {
p.Stdout.Writeln(marshaller(line))
}
else {
} for i := range recs {
if !matchStr[headings[i]] {
append(line, recs[i])
line =
}
}
if len(line) != 0 {
p.Stdout.Writeln(marshaller(line))
}
}
lineNum++
}
default:
return errors.New("you haven't selected any rows / columns")
} }
*lang.Process
: Process’s runtime state. Typically expressed as the variable p
[]string
: slice of parameters used in [
/ ![
chan []string
: a channel for rows (each element in the slice is a column within the row). This allows tables to be stream-ablefunc(interface{}) ([]byte, error)
: data type marshaller functionReadArray()
(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 structureThis 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 Tue Dec 10 22:56:57 UTC 2024 against commit 60f05a260f05a227caf73dd5b3478e3cb3f4bb24e46745b.
Current version is 6.4.1005 (develop) which has been verified against tests cases.