github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/stdio/templates.go (about)

     1  package stdio
     2  
     3  // template functions for stdio.Io methods to call
     4  // (saves reinventing the wheel lots of times)
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"io"
    10  	"os"
    11  
    12  	"github.com/lmorg/murex/config"
    13  	"github.com/lmorg/murex/lang/types"
    14  )
    15  
    16  // ReadArray is a template function for stdio.Io
    17  func ReadArray(ctx context.Context, read Io, callback func([]byte)) error {
    18  	dt := read.GetDataType()
    19  
    20  	fnReadArray := readArray[dt]
    21  	if fnReadArray != nil {
    22  		return fnReadArray(ctx, read, callback)
    23  	}
    24  
    25  	return readArray[types.Generic](ctx, read, callback)
    26  }
    27  
    28  // ReadArrayWithType is a template function for stdio.Io
    29  func ReadArrayWithType(ctx context.Context, read Io, callback func(interface{}, string)) error {
    30  	dt := read.GetDataType()
    31  
    32  	fnReadArray := readArrayWithType[dt]
    33  	if fnReadArray != nil {
    34  		return fnReadArray(ctx, read, callback)
    35  	}
    36  
    37  	return readArrayWithType[types.Generic](ctx, read, callback)
    38  }
    39  
    40  // ReadMap is a template function for stdio.Io
    41  func ReadMap(read Io, config *config.Config, callback func(*Map)) error {
    42  	dataType := read.GetDataType()
    43  
    44  	fnReadMap := readMap[dataType]
    45  	if fnReadMap != nil {
    46  		return fnReadMap(read, config, callback)
    47  	}
    48  
    49  	return readMap[types.Generic](read, config, callback)
    50  }
    51  
    52  // WriteArray is a template function for stdio.Io
    53  func WriteArray(writer Io, dt string) (ArrayWriter, error) {
    54  	if writeArray[dt] != nil {
    55  		return writeArray[dt](writer)
    56  	}
    57  
    58  	return nil, fmt.Errorf("murex data type `%s` has not implemented WriteArray() method", dt)
    59  }
    60  
    61  // WriteTo is a template function for stdio.Io
    62  func WriteTo(std Io, w io.Writer) (int64, error) {
    63  	var (
    64  		total int64
    65  		i, n  int
    66  		p     = make([]byte, 1024*10)
    67  		err   error
    68  	)
    69  
    70  	for {
    71  		i, err = std.Read(p)
    72  
    73  		if err == io.EOF {
    74  			return total, nil
    75  		}
    76  
    77  		if err != nil {
    78  			return total, err
    79  		}
    80  
    81  		n, err = w.Write(p[:i])
    82  		total += int64(n)
    83  
    84  		if err != nil {
    85  			return total, err
    86  		}
    87  
    88  	}
    89  }
    90  
    91  // WriteToFromFile is a template function for stdio.Io
    92  func WriteToFromFile(f *os.File, w io.Writer) (int64, error) {
    93  	var (
    94  		total int64
    95  		i, n  int
    96  		p     = make([]byte, 1024*10)
    97  		err   error
    98  	)
    99  
   100  	for {
   101  		i, err = f.Read(p)
   102  
   103  		if err == io.EOF {
   104  			return total, nil
   105  		}
   106  
   107  		if err != nil {
   108  			return total, err
   109  		}
   110  
   111  		n, err = w.Write(p[:i])
   112  		total += int64(n)
   113  
   114  		if err != nil {
   115  			return total, err
   116  		}
   117  
   118  	}
   119  }